原文地址:http://blog.gdfengshuo.com/2017/07/10/19html
组件是 vue.js 最强大的功能之一,而组件实例的做用域是相互独立的,这就意味着不一样组件之间的数据没法相互引用。如何传递数据也成了组件的重要知识点之一。vue
组件与组件之间,还存在着不一样的关系。父子关系与兄弟关系(不是父子的都暂称为兄弟吧)。git
原文做者:林鑫,做者博客:https://github.com/lin-xin/bloggithub
父子关系便是组件 A 在它的模板中使用了组件 B,那么组件 A 就是父组件,组件 B 就是子组件。vuex
// 注册一个子组件 Vue.component('child', { data: function(){ return { text: '我是father的子组件!' } }, template: '<span>{{ text }}</span>' }) // 注册一个父组件 Vue.component('father', { template: '<div><child></child></div>' // 在模板中使用了child组件 })
直接使用 father 组件的时候:app
<div id="app"> <father></father> </div>
页面中就会渲染出 :我是father的子组件!ide
father 组件在模板中使用了 child 组件,因此它就是父组件,child 组件被使用,因此 child 组件就是子组件。模块化
两个组件互不引用,则为兄弟组件。this
Vue.component('brother1', { template: '<div>我是大哥</div>' }) Vue.component('brother2', { template: '<div>我是小弟</div>' })
使用组件的时候:spa
<div id="app"> <brother1></brother1> <brother2></brother2> </div>
页面中就会渲染出 :
我是大哥
我是小弟
子组件想要使用父组件的数据,咱们须要经过子组件的 props 选项来得到父组件传过来的数据。如下我使用在 .vue 文件中的格式来写例子。
在父组件 father.vue 中引用子组件 child.vue,把 name 的值传给 child 组件。
<template> <div class="app"> // message 定义在子组件的 props 中 <child :message="name"></child> </div> </template> <script> import child from './child.vue'; export default { components: { child }, data() { return { name: 'linxin' } } } </script>
在子组件 child.vue 中的 props 选项中声明它期待得到的数据
<template> <span>Hello {{message}}</span> </template> <script> export default { // 在 props 中声明获取父组件的数据经过 message 传过来 props: ['message'] } </script>
那么页面中就会渲染出:Hello linxin
当父组件的 name 发生改变,子组件也会自动地更新视图。可是在子组件中,咱们不要去修改 prop。若是你必需要修改到这些数据,你可使用如下方法:
方法一:把 prop 赋值给一个局部变量,而后须要修改的话就修改这个局部变量,而不影响 prop
export default { data(){ return { newMessage: null } }, props: ['message'], created(){ this.newMessage = this.message; } }
方法二:在计算属性中对 prop 进行处理
export default { props: ['message'], computed: { newMessage(){ return this.newMessage + ' 哈哈哈'; } } }
prop 是单向绑定的:当父组件的属性变化时,将传导给子组件,可是不会反过来。修改子组件的 prop 值,是不会传回给父组件去更新视图的。那么子组件要如何去与父组件通信呢?
那就是自定义事件。经过在父组件 $on(eventName) 监听自定义事件,当子组件里 $emit(eventName) 触发该自定义事件的时候,父组件执行相应的操做。
好比在父组件中控制一个弹框子组件的显示,在子组件中按下关闭以后,告诉父组件去隐藏它,而后父组件就执行操做隐藏弹框。
<template> <div class="app"> // hide 为自定义事件,名字能够本身随便起,不能有大写字母,可使用短横线 // @hide 监听子组件触发 hide 事件,则会执行 hideDialog 方法 <dialog :is-show="show" @hide="hideDialog"></dialog> <button @click="showDialog">显示弹框</button> </div> </template> <script> import dialog from './dialog.vue'; export default { components: { dialog }, data() { return { show: false } }, methods: { showDialog() { this.show = true; }, hideDialog() { this.show = false; } } } </script>
在子组件 dialog.vue 中:
<template> <div class="dialog" v-show="isShow"> <p>这里是弹框子组件</p> <button @click="toHide">关闭弹框</button> </div> </template> <script> export default { // 驼峰式命名的 prop 须要转换为相对应的短横线隔开式 is-show props: ['isShow'], methods: { toHide(){ // $emit 方法触发父组件的监听事件 this.$emit('hide'); } } } </script>
这样就实现了父子组件之间的相互通信。
上面的例子都是创建在父子关系的组件上,可是对于其余层级的关系,实现起来就比较繁琐了。那么这时候 Vuex 就能更好的帮你在各个组件间实时通信了。关于 Vuex,可查看个人另外一篇文章:Vuex 模块化实现待办事项的状态管理
组件通信并非必定要使用必需要使用 Vuex,对于一些简单的数据传递,prop 也能够完成。本文主要是对组件传参的一些基础知识点的记录,实战能够参考 notepad 这个例子,使用 prop 实现子组件的显示与隐藏,使用 vuex 来实现组件间的数据状态管理。