脚本宝典收集整理的这篇文章主要介绍了八种vue实现组建通信的方式,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<Son :datas="fData"></Son> <script> import Son From '@/components/son' export default{ name:'Father', components:{Son}, data(){ return{ fData:'我是父组件向子组件传递的值-props方式' } } } </script>
子组件props
接受的参数名称,要与父组件传递时定义的属性名一致
<template> <div>我是父组件的数据:{{fData}}</div> <div @click=changeData>我是父组件传递修改后的数据:{{mydata}}</div> </template> <script> export default{ name:'Son', props:{ fData:{ tyPE:String, default:'' } } data(){ mydata:this.fatherData }, methods:{ changeData(){ this.mydata += '改变数据' } }, } </script>
注意:
Vue
的单向数据流机制,如果直接修改那父组件的值就被“污染”了。(props
是单向绑定的(只读属性):当父组件的属性变化时,将传导给子组件,但是反过来不会)报错信息大概是:vue使用prop通信出错:
Avoid mutating a prop directly since the value will be overwrITten whenever the parent
props:{ fData:{ type:[String,Number], default:'' } }
<el-button @click="handleEmit">改变父组件</el-button> <script> export default{ name:'Son', methods:{ handleEmit(){ this.$emit('triggerEmit','子组件的数据') } } } </script>
父组件(子组件发送的事件名称,要和父组件接受的事件名称一致)
<Son @triggerEmit="changeData"></Son> <script> import Son from '@/components/son' export default{ name:'Father', components:{Son}, methods:{ changeData(name){ console.LOG(name) // => 我是来自子组件的数据 } } } </script>
$emit与props结合 兄弟组件传值
<childA :myName="name"></ChildA> <ChildB :myName="name" @changeName="editName"></ChildB> export default{ data() { return { name: '数据你好' } }, methods: { editName(name){ this.name = name } } }
子组件B改变,接收数据
<p>姓名:{{ myName }}</p> <button @click="changeName">修改姓名</button> <script> export default{ props: { myName:String }, methods: { changeName() { this.$emit('changeName', '新数据名称') } } } </script>
子组件A接收数据
<p>姓名:{{ newName }}</p> <script> export default{ props: { myName:String } } </script>
非父子组件或更多层级间组件间传值,在Vue中通过单独的事件中心来管理组件间的传值
import Vue from "vue" const bus=new Vue() export default bus
需要改变数据的组件中定义调用
<template> <div> <div>我是通信组件A</div> <button @click="changeName">修改姓名</button> </div> </template> <script> import bus from "@/utils/Bus.js"; export default { components: {}, data() { return {}; }, mounted() { console.log(bus); }, methods: { changeName() { bus.$emit("editName", "数据集!"); }, }, }; </script> <style lang='scss' scoped> </style>
另外一个组件中同样引入bus.js文件,通过$on监听事件回调
<template> <div> <span>名称:{{name}}</span> <div>我是通信组件B</div> </div> </template> <script> import bus from "@/utils/Bus.js"; export default { components: {}, data() { return {name}; }, mounted() { bus.$on("editName", (name) => { this.name=name console.log(name); // }); }, methods: {}, }; </script> <style lang='scss' scoped> </style>
子组件---this.$parent可以获取到父组件的方法、data的数据等,并可以直接使用和执行
<template> <div>我是子组件</div> </template> <script> export default{ name:"Son", data(){ return{ sonTitle: '我是子组件的数据' } }, methods:{ sonHandle(){ console.log('我是子组件的方法') } }, created(){ console.log(this.$parent) console.log(this.$parent.fatherTitle) // => 我是父组件的数据 this.$parent.fantherHandle() // => 我是父组件的方法 } } </script>
父组件 --- 获取子组件实例的,并且获取的实例是一个数组形式,this.$children[0]才可以获取某个组件实例,并调用组件方法和数据
<template> <div> <Son>我是父组件</Son> </div> </template> <script> import Son from './son.vue' export default{ name: 'father', components:{ Son }, data(){ return{ fatherTitle: '我是父组件的数据' } }, methods:{ fantherHandle(){ console.log('我是父组件的方法') } }, mounted(){ console.log(this.$children) console.log(this.$children[0].sonTitle) // => 我是子组件的数据 this.$children[0].sonHandle() // => 我是子组件的方法 } } </script>
ref被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。
父组件使用 $refs 获得组件实例
<template> <div> <Son ref="son"></Son> </div> </template> <script> import Son from './son.vue' export default{ name: 'father', components:{ Son }, mounted(){ console.log(this.$refs.son) /*组件实例*/ } } </script>
provide/inject
详解
/*父组件*/ export default{ provide: { return{ provideName: '贩卖前端仔' } } }
至此provideName
这个变量可以提供给它其下的所有子组件,包括曾孙、孙子组件等,只需要使用 inject 就能获取数据
/*子组件*/ export default{ inject: ['provideName'], created () { console.log(this.provideName) // => "贩卖前端仔" } }
prop
传递给组件一样slot-scope
子元素<template> <div> <div class="isSon"> <slot :info='arrList'></slot> </div> </div> </template> <script> export default { components: {}, data() { return {arrList:[1,'aa','张三']}; }, mounted() { }, methods: { }, }; </script>
父元素
<template> <div> <SonG> <span slot-scope="props"> <ul> aa <li v-for="item in props.info" :key="item"> {{item}} </li> </ul> </span> </SonG> </div> </template> <script> import SonG from '../components/SonG.vue' export default { components:{ SonG }, data () { return { } } } </script>
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { }, mutations: { }, actions: { }, modules: { } })
到此这篇关于八种vue
实现组建通信的方式的文章就介绍到这了,更多相关vue
实现组建通信的方式内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本宝典!
以上是脚本宝典为你收集整理的八种vue实现组建通信的方式全部内容,希望文章能够帮你解决八种vue实现组建通信的方式所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。