vue学习日记——父子组件间数据传递

在 vue 中组件实例的作用域是相互独立的,所以子组件不能直接使用父组件中的数据。子组件要想获取父组件的数据,可以采用以下两种方式:

1、子组件中使用 (this.$parent.变量名) 调用,例:

//父组件
<template>
  <div>
    <child></child>
  </div>
</template>

<script>
import child from '@/components/child'
export default {
  name: 'father',
  data () {
    return {
      fatherMsg: 'father message'
    }
  },
  components: {
    child: child
  }
}
</script>
//子组件
<template>
    <div>{{msg}}</div>
</template>

<script>
export default {
  name: 'child',
  data () {
    return {
      msg: this.$parent.fatherMsg
    }
  }
}
</script>

2、在父组件中将需要传递的数据通过属性名绑定给子组件,在子组件中用 props 接收数据,例:

//父组件
<template>
  <div>
    <child :f-msg="fatherMsg"></child>//html 不区分大小写,使用' - '代替驼峰
  </div>
</template>

<script>
import child from '@/components/child'
export default {
  name: 'father',
  data () {
    return {
      fatherMsg: 'father message'  
      //如果值是对象,上面绑定属性可以写为 v-bind='fartherMsg'
    }
  },
  components: {
    child: child
  }
}
</script>
//子组件
<template>
    <div>{{fMsg}}</div>
</template>

<script>
export default {
  name: 'child',
  props: ['fMsg']
}
</script>
//子组件采用 props 接收数据也有几种不同的方式:
//方式1:
props: ['fMsg']
//方式2 :
props: {
  fMsg: String //指定期望接收的数据类型
}
//方式3:
props: {
  fMsg:{
    type: String,   //指定传入的类型
  default: "message" //指定默认的值
  }
}

同样,父组件要获取子组件的数据也有两种方法:
1、在父组件中给子组件绑定一个 ref 属性,这样父组件中就可以通过(this.$ref.(ref值).属性名)的方式获取数据了,例:

//父组件
<template>
  <div>
    <div @click="show">message :{{msg}}</div>
    <child ref="child"></child>
  </div>
</template>
<script>
import child from '@/components/child'
export default {
  name: 'father',
  data () {
    return {
      msg: ''        //不能在这里调用数据
    }
  },
  components: {
    child: child
  },
  methods: {
    show: function () {
      this.msg = this.$refs.child.cMsg
    }
  }
}
</script>
//子组件
<template>
    <div></div>
</template>
<script>
export default {
  name: 'child',
  data () {
    return {
      cMsg: 'child message'
    }
  }
}
</script>

2、在父组件中给子组件绑定事件,在子组件中通过(this.$emit(事件名, 参数)),向父组件传递数据,例:

//父组件
<template>
  <div>
    <div>message :{{msg}}</div>
    <child @getMsg="get"></child>
  </div>
</template>
<script>
import child from '@/components/child'
export default {
  name: 'father',
  data () {
    return {
      msg: ''
    }
  },
  components: {
    child: child
  },
  methods: {
    get: function (cMsg) {
      this.msg = cMsg
    }
  }
}
</script>
//子组件
<template>
    <div @click="setMsg">setMsg</div>
</template>
<script>
export default {
  name: 'child',
  data () {
    return {
      cMsg: 'child message'
    }
  },
  methods: {
    setMsg: function () {
      this.$emit('getMsg', this.cMsg)
    }
  }
}
</script>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,082评论 0 29
  • 此文基于官方文档,里面部分例子有改动,加上了一些自己的理解 什么是组件? 组件(Component)是 Vue.j...
    陆志均阅读 3,868评论 5 14
  • 澡堂子该是北方的特产了吧。 几十平的大间里,众饺子一锅蒸了,不分类别,只有性别。管它狐皮貂裘、玉翠金链子,赤裸了,...
    章鱼丸QAQ阅读 196评论 0 0
  • 天空飘起了蒙蒙细雨 喜悦莫名绕上了心头 想起昨夜的呓语 寂寞无言惹窗台 生命里面深藏的美好始终还在 只是想了很久的...
    田萍阅读 222评论 1 8