vue 项目的一大亮点就是组件化. 使用组件可以极大地提高项目中代码的复用率, 减少代码量. 但是使用组件最大的难点就是父子组件之间的通信.
子通信父
父组件
- <template>
- <div class="parent">
我是父组件
- <!-- 父组件监听子组件触发的 say 方法, 调用自己的 parentSay 方法 -->
- <!-- 通过: msg 将父组件的数据传递给子组件 -->
- <children :msg="msg" @say="parentSay"></children>
- </div>
- </template>
- <script>
- import Children from './children.vue'
- export default {
- data () {
- return {
- msg: 'hello, children'
- }
- },
- methods: {
- // 参数就是子组件传递出来的数据
- parentSay(msg){
- console.log(msg) // hello, parent
- }
- },
- // 引入子组件
- components:{
- children: Children
- }
- }
- </script>
子组件
- <template>
- <div class="hello">
- <div class="children" @click="say">
我是子组件
<div>
父组件对我说:{{msg}}
- </div>
- </div>
- </div>
- </template>
- <script>
- export default {
- // 父组件通过 props 属性传递进来的数据
- props: {
- msg: {
- type: String,
- default: () => {
- return ''
- }
- }
- },
- data () {
- return {
- childrenSay: 'hello, parent'
- }
- },
- methods: {
- // 子组件通过 emit 方法触发父组件中定义好的函数, 从而将子组件中的数据传递给父组件
- say(){
- this.$emit('say' , this.childrenSay);
- }
- }
- }
- </script>
子组件使用 $emit 方法调用父组件的方法, 达到子通信父的目的.
父通信子
父组件
- <!--html-->
- <template>
- <!-- 父组件触发 click 方法 -->
- <div class="parent" @click="say">
我是父组件
- <!-- 通过 ref 标记子组件 -->
- <children ref="child"></children>
- </div>
- </template>
- <script>
- import Children from './children.vue'
- export default {
- data () {
- return {
- msg: "hello,my son"
- }
- },
- methods: {
- // 通过 $refs 调用子组件的 parentSay 方法
- say(){
- this.$refs.child.parentSay(this.msg);
- }
- },
- // 引入子组件
- components:{
- children: Children
- }
- }
- </script>
子组件
- <template>
- <div class="hello">
- <div class="children">
我是子组件
<div>
父组件对我说:{{msg}}
- </div>
- </div>
- </div>
- </template>
- <script>
- export default {
- data () {
- return {
- msg: ''
- }
- },
- methods: {
- // 父组件调用的 JavaScript 方法 parentSay
- parentSay(msg){
- this.msg = msg;
- }
- }
- }
- </script>
父组件通过 $refs 调用子组件的方法.
以上就是父子组件通信的方式, 父子组件传递数据直接用 props, 或者使用 $emit 和 $refs 依靠事件来传递数据.
父子组件通信提升篇
上文中, 子通信父是在子中触发点击事件然后调用父组件的方法, 父通信子是在父中触发点击事件调用子组件的方法. 但是实际情况中可能存在子通信父时子组件不允许有点击事件而事件在父中或者父通信子时点击事件在子组件中.
子通信父时击事件在父组件
这种情况其实很常见, 例如提交一个表单时表单的内容为子组件, 而保存按钮在父组件上. 此时点击保存按钮想要获取子组件表单的值. 这种情况下已经不单单是子通信父和父通信子了, 需要将两者结合在一起使用才能完成整个通信过程.
实现的思路是在父组件中点击事件时, 先通过父子通信调用子组件的方法, 然后在子组件中的该方法里使用子父通信调用父组件的另一个方法并将信息传递回来. 以下是代码演示:
父组件
- <template>
- <div class="parent" @click="getData">
我是父组件
- <!-- 父组件监听子组件触发的 transData 方法, 调用自己的 transData 方法 -->
- <!-- 通过 ref 标记子组件 -->
- <children ref="child" @transData="transData"></children>
- </div>
- </template>
- <script>
- import Children from './children.vue'
- export default {
- data () {
- return {
- msg: 'hello, children'
- }
- },
- methods: {
- getData(){
- // 调用子组件的 getData 方法
- this.$refs.child.getData();
- },
- // 参数就是子组件传递出来的数据
- transData(msg){
- console.log(msg) // hello, parent
- }
- },
- // 引入子组件
- components:{
- children: Children
- }
- }
- </script>
子组件
- <template>
- <div class="hello">
- <div class="children">
我是子组件
<div>
子组件的数据:{{childrenSay}}
- </div>
- </div>
- </div>
- </template>
- <script>
- export default {
- data () {
- return {
- childrenSay: 'hello, parent'
- }
- },
- methods: {
- // 子组件通过 emit 方法触发父组件中定义好的函数, 从而将子组件中的数据传递给父组件
- getData() {
- this.$emit('transData' , this.childrenSay);
- }
- }
- }
- </script>
另一种情况思路也和这个一样, 基础就在与父通信子和子通信父的灵活运用.
转评赞就是最大的鼓励
来源: https://segmentfault.com/a/1190000020197420