vue 子组件接收父组件数据的方法
父组件

<template>
  <div>
     <Comp :name="dy" :title="title" :id="id"></Comp>
  </div>
</template>
<script>
import Comp from '@/views/dyState/child/comp'
export default {
  components: { Comp1, Comp2 },
  data() {
    return {
      name: "1",
      title: "2",
      id: "3"
    }
  },
}
</script>

子组件

export default {
  props: {
    name: '',
  },
  mounted() {
    console.log(this.$attrs)
    console.log(this.name)
  },
}

子组件打印接收数据结果:
console.log(this.$attrs) // { title:“2”,id:“3” }
console.log(this.name) // 1

标题

总结:父组件通过自定义属性传递数据给子组件,子组件接收时:
(1),子组件用this. a t t r s 全 部 接 收 , 获 取 到 的 是 所 有 数 据 对 象 ( 2 ) , 子 组 件 中 既 有 p r o p s , 也 有 t h i s . attrs全部接收,获取到的是所有数据对象 (2),子组件中既有props,也有this. attrs(2),propsthis.attrs,则子组件接收到的数据为props接收对应数据,剩下的在this. a t t r s 对 象 中 ; 如 果 父 组 件 只 传 递 了 一 个 数 据 , 则 t h i s . attrs对象中;如果父组件只传递了一个数据,则this. attrs;this.attrs接收到的数据为空## 标题
(3),子组件全部用props一个一个接收

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐