VUE 组件通信总结
1.prop 父组件传递给子组件,即通过VUE本身具有的Props属性来传递值
Child组件
<template>
<span>{{message}}</span>
</template>
<script>
export default {
props: ['message'],
data () {
return {}
}
}
</script>
Father组件
<template>
<div>
<child message='via props'></child>
</div>
</template>
<script>
import child from './child'
export default {
components: {
child
}
}
</script>
运行结果
2.非Prop传递数据
Child
<template>
<span @click="getData()">{{message}}</span>
</template>
<script>
export default {
props: ['message'],
data () {
return {}
},
methods: {
getData () {
console.log(this.$el.attributes.noprop.value)
}
}
}
</script>
Father
<template>
<div>
<child noProp='not via props' message='via props'></child>
</div>
</template>
<script>
import child from './child'
export default {
components: {
child
}
}
</script>
运行结果
3.v-on v-emit 子组件传给父组件
Child
<template>
<span @click="emitTest()">{{message}}</span>
</template>
<script>
export default {
props: ['message'],
data () {
return {}
},
methods: {
emitTest () {
console.log('This is children"s message')
this.$emit('onTest', '123')
}
}
} </script>
Father
<template>
<div>
<child v-on:onTest='onTestFunc' message='via props'></child>
</div>
</template>
<script>
import child from './child'
export default {
components: {
child
},
methods: {
onTestFunc (id) {
console.log('This is father"s message' + id)
}
}
}
</script>
运行结果
4.空vue实例作为事件总线,非父子组件通信
5.Vuex
https://cn.vuejs.org/v2/guide/state-management.html
6.作用域插槽
child
<template>
<div>
<slot message="This is a children message"></slot>
</div>
</template>
<script>
export default {
}
</script>
Father
<template>
<div>
<child>
<template scope="scope">{{scope.message}}</template>
</child>
</div>
</template>
<script>
import child from './child'
export default {
components: {
child
}
}
</script>
运行结果
7.$parent,$children
$children,即在父组件中,可以利用this.$children[x]来访问子组件的data属性,如果你觉得按数组当有多个子组件时难以维护,也可以用$ref,
首先你的给子组件做标记。demo :<firstchild ref="one"></firstchild>
然后在父组件中,通过this.$refs.one就可以访问了这个自组件了,包括访问自组件的data里面的数据,调用它的函数
Child
<template>
<div></div>
</template>
<script>
export default {
data () {
return {
childMsg: 'this is a child msg'
}
}
}
</script>
Father
<template>
<div>
<child></child>
</div>
</template>
<script>
import child from './child'
export default {
components: {
child
},
mounted () {
console.log(this.$children[0].childMsg)
}
}
</script>
$parent
Child
<template>
<div></div>
</template>
<script>
export default {
mounted () {
console.log(this.$parent.fatherMsg)
}
}
</script>
Father
<template>
<div>
<child></child>
</div>
</template>
<script>
import child from './child'
export default {
components: {
child
},
data () {
return {
fatherMsg: 'this is a fatherMsg'
}
}
}
</script>
VUE 组件通信总结的更多相关文章
- vue组件通信的几种方式
最近用vue开发项目,记录一下vue组件间通信几种方式 第一种,父子组件通信 一.父组件向子组件传值 1.创建子组件,在src/components/文件夹下新建一个Child.vue 2.Child ...
- vue 组件通信
组件 组件之间的数据是单向绑定的. 父组件向子组件通信 是通过子组件定义的props属性来实现.通过props定义变量与变量类型和验证方式. props简化定义 在简化定义中,变量是以数组的方式定义. ...
- vue组件通信那些事儿
一.说说通信 通信,简言之,交流信息.交流结束后,把信息放在哪里,这是一个值得思考的问题.vue中保存状态有2种方式,组件内的data属性和组件外的vuex的state属性. 1.用state的场景 ...
- vue组件通信&&v兄弟组件通信eventbus遇到的问题(多次触发、第一次不触发)
组件通讯包括:父子组件间的通信和兄弟组件间的通信.在组件化系统构建中,组件间通信必不可少的 (vuex以后再说). 父组件--> 子组件 1. 属性设置 父组件关键代码如下: <templ ...
- vue 组件通信传值
父子组件通信: 子组件 <template> <div> <h3 @click="alerrt"> 我是子组件一</h3> < ...
- Vue组件通信之Bus
关于组件通信我相信小伙伴们肯定也都很熟悉,就不多说了,对组件通信还不熟悉的小伙伴移步这里. 在vue2.0中 $dispatch 和 $broadcast 已经被弃用.官方文档中给出的解释是: 因为基 ...
- Vue组件通信的几种方法
上一节说到,vue.js是允许子组件通过props接受父组件的信息,但是不允许父组件通过props接受子组件的信息 1. $emit()和on 当子组件需要向父组件传递数据时,就要用到自定义事件. 使 ...
- vue组件通信之父子组件通信
准备工作: 首先,新建一个项目,如果这里有不会的同学,可以参考我转载过的文章 http://www.cnblogs.com/Sky-Ice/p/8875958.html vue 脚手架安装及新建项目 ...
- vue组件通信之非父子组件通信
什么顺序不顺序的.. 先来说说非父子组件通信. 首先,我们先来了解下vue中的 1.$emit 触发当前实例上的事件,附加参数都会传给监听器回调. 2.$on 监听当前实例上的自定义事件.事件可以 ...
随机推荐
- Yaf 完全精通
bugs 这样 _Bootstrap 的话,会导致严重的后果,cpu 100%
- Java 异常与反射 总结
1.异常 异常,简单来说,就是一个程序执行过程中发生的不正常情况的事件.它发生在程序的运行期间,干扰了正常的指令流程.如果没有处理异常,那么出现异常之后,程序会停止运行.异常分为运行异常和非运行异常. ...
- jquery复制值到剪切板(clipboard.js)
引入一个clipboard.js文件即可使用,下载地址:https://github.com/zenorocha/clipboard.js <script type="text/jav ...
- App性能测试之启动时间(安卓)手动+脚本
这个测试可以使用adb工具,adb的安装方式 测试策略 安装后首次启动 常规冷启动 热启动(一般这个都很少测试) 针对1和2的测试方法 步骤1:在cmd中输入如下命令 adb logcat * > ...
- webapi使用swagger出现“Cannot read property 'parameters' of null”
前端时间在webapi项目使用swagger来提供接口文档及测试工具,按网上方法(http://wmpratt.com/swagger-and-asp-net-web-api-part-1)配置好之后 ...
- hive -help hive命令行执行sql参数
在shell命令行执行 hive -help 结果如下: -d,--define <key=value> Variable substitution to apply to Hive co ...
- 12Js_原型对象
对象描述: 1. 每个对象中都有一个_proto_属性. JS世界中没有类(模具)的概念,对象是从另一个对象(原型)衍生出来的,所以每个对象中会有一个_proto_属性指向它的原型对象.(参考左上角的 ...
- Must Know Tips/Tricks in Deep Neural Networks
Must Know Tips/Tricks in Deep Neural Networks (by Xiu-Shen Wei) Deep Neural Networks, especially C ...
- 使用jenkins自部署Coding项目
下载安装jenkins 下载地址:https://jenkins.io/download/ 安装后通过主机的8080端口进行程序设置,插件安装默认的就好 Jenkins项目目录:C:\Program ...
- python爬虫学习之查询IP地址对应的归属地
话不多说,直接上代码吧. import requests def getIpAddr(url): response = requests.get(url) response.encoding=resp ...