uni-app中vue组件父子值传递
一、父组件向子组件传递数据(props)
<template>
<view class="container" style="background: #0062CC;"> <view class="child"> hi {{showModal}}</view>
</view>
</template> <script>
export default {
props: {
showModal: {
type: String,
default: 'hello'
}
}, data() {
return { };
},methods:{ }
}
</script> <style> </style>
child
<template>
<view>
<child :showModal="showModal"></child> </view>
</template> <script>
import child from "../../components/child.vue" export default {
components: {
child
},
data() {
return {
showModal: " parent say"
};
},
methods: { }
}
</script> <style> </style>
parent
二、子组件向父组件传递数据(emit)
<template>
<view>
<child :showModal="showModal" @changes="childClick"></child>
<view>{{parentValue}}</view>
</view>
</template> <script>
import child from "../../components/child.vue" export default {
components:{
child
},
data() {
return {
showModal:" parent say",
parentValue:''
};
},methods:{
childClick(e){
console.info(e);
this.parentValue=e;
} }
}
</script> <style> </style>
parent
<template>
<view class="container">
<button @tap="childClick" >点击我 </button>
<view class="child"> hi {{showModal}}</view>
</view>
</template> <script>
export default {
props: {
showModal: {
type: String,
default: 'hello'
}
}, data() {
return {
childdata:'child value'
};
},methods:{
childClick(){
console.info(this.childdata);
this.$emit("changes",this.childdata); } }
}
</script> <style> </style>
child
三、子组件与父组件数据同步(.sync)
<template>
<view class="container" style="background: #0062CC;">
<button @tap="childClick" >点击我 </button>
<view class="child"> hi {{showModal}}</view>
<view>psync同步(child):{{syncDate}}</view>
</view>
</template> <script>
export default {
props: {
showModal: {
type: String,
default: 'hello'
},
syncDate: {
type: String,
default: 'hello'
}
}, data() {
return {
childdata:'child value'
};
},methods:{
childClick(){
console.info(this.childdata);
this.$emit("changes",this.childdata); } }
}
</script> <style> </style>
child
<template>
<view>
<child :syncDate.sync='syncDate' :showModal="showModal" @changes="childClick"></child> <view class="parent" style="background: #09BB07;">
<view>emit传值:{{parentValue}}</view>
<input :value="syncDate" v-model="syncDate" style="background: #808080;" />
<view>psync同步(parent):{{syncDate}}</view>
</view>
</view>
</template> <script>
import child from "../../components/child.vue" export default {
components: {
child
},
data() {
return {
showModal: " parent say",
parentValue: '',
syncDate: ' p syncDate'
};
},
methods: {
childClick(e) {
console.info(e);
this.parentValue = e;
} }
}
</script> <style> </style>
parent
uni-app中vue组件父子值传递的更多相关文章
- vue组件之间值传递四种方法汇总
1.父组件获取子组件的数据和方法 $refs 子组件: <template> <div class="header"> <h3>{{ zz }} ...
- Java中不得不谈的值传递和地址传递
个人的一些认识,希望能对初学Java的你,或者困惑于方法参数传递的你祈祷一丝帮助! 下面是一些作者的个人观点,如果有错,欢迎各位大牛指出错误,灰常感谢您的观看与支持... -------------- ...
- uni app中使用自定义图标库
项目中难免会用到自定义图标,那在uni app中应该怎么使用呢? 首先, 将图标目录放在static资源目录下: 在main.js中引入就可以全局使用了 import '@/static/icon-o ...
- vue组件父子组件传递引用类型数据
今天在写分页功能时,发现父子组件传值时,子组件监听不到父组件中数据的变化,传递的是一个引用类型的数据 其原因是引用类型共用一个内存地址,父子组件用的是同一个对象,故子组件监听不到变化,此时就需要做一个 ...
- vue 组件间数据传递
父组件向子组件传值 方法一: 子组件想要使用父组件的数据,需要通过子组件的 props 选项来获得父组件传过来的数据. 1.父组件parent.vue中代码: <template> < ...
- 第七十四篇:Vue组件父子传值
好家伙, 1.组件之间的关系 在项目开发中,组件之间的最常见关系分为如下两种: (1)父子关系 (2)兄弟关系 2.父子之间的数据共享 (1)父->子共享数据 父组件向子组件共享数据需要使用自定 ...
- vue 组件之间数据传递(七)
1.props:父组件 -->传值到子组件 app.vue是父组件 ,其它组件是子组件,把父组件值传递给子组件需要使用 =>props 在父组件(App.vue)定义一个属性(变量)sex ...
- vue组件父子间通信之综合练习--假的聊天室
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Java中传参的值传递和引用传递问题(转)
今天遇到了一个java程序,需要用参数来返回值(虽然最后用另一种方法实现了),在网上看到这样一篇文章,很受启发. 本文章来自于http://hi.baidu.com/xzhilie/blog/item ...
随机推荐
- General-Purpose Operating System Protection Profile
1 Protection Profile Introduction This document defines the security functionality expected to be ...
- MinGW开发工具的安装(还有visual-mingw)
MinGW是Minimalist GNU for Windows的缩写,是把linux下的GNU开发工具包移植到windows的项目之一.和Cygwin不一样的是,MinGW不提供linux的posi ...
- matlab 高级函数 —— circshift、squeeze
circshift:顾名思义,循环移动,循环的意义在于,移出的数据不丢失,而是来到队列的首部位置,也即其实是将原始序列视为一种圆环. 1. 基本用法 默认为右移. Y = circshift(A,K) ...
- C#性能优化:延迟初始化Lazy
1. 概述 我们创建某一个对象需要很大的消耗,而这个对象在运行过程中又不一定用到,为了避免每次运行都创建该对象,这时候延迟初始化(也叫延迟实例化)就出场了. 延迟初始化出现于.NET 4.0,主要用于 ...
- 微信公众平台开发(1) 通用的工具类CommonUtil
1.通用的调用微信的方法 /** * * @param requestUrl 接口地址 * @param requestMethod 请求方法:POST.GET... * @param output ...
- 一个2013届毕业生(踏上IT行业)的迷茫(2)
初中的时光是一段艰辛,但幸福的时光,在这一段时光中同样我遇到了我人生中第二个贵人.记得在小学毕业的那个暑假里,我知道上了初中会开一门叫做英语的课程,那时候在我们那里有好多上过初中.高中的在我们小学开英 ...
- 在当前页获取父窗口中母版页中的服务器控件的ID
parent.document.getElementById("ctl00_ContentPlaceHolder1_txt_name").value=""; A ...
- Eclipse中设置自定义文档签名
今天第一次认真学习eclipse的使用,看到自定义文档签名,步骤如下: 1.点击window->preferences->java->Code Style->Code Temp ...
- Linux 下非 root 用户安装 theano(配置 GPU)
非 root 用户,安装 Python 第三方的包,尤其像 theano,存在大量的依赖项,存在的主要问题,是安装各个包时的权限问题.所幸,存在这样一个集成工具,叫 anaconda,其已经内置了许多 ...
- WPF异常捕获,并使程序不崩溃!
原文:WPF异常捕获,并使程序不崩溃! 在.NET中,我们使用try-catch-finally来处理异常.但,当一个Exception抛出,抛出Exception的代码又没有被try包围时,程序就崩 ...