Vue 1-12 父子组件

1.  父子组件互相传递数据:  父--->子(props),  子--->父(this.$emit('自定义一个事件名',  数据))

  具体可查看: 微信小程序--自定义组件(https://www.cnblogs.com/leafchen/p/15459388.html)

 

2.  父组件直接获取子组件的方法或数据

  this.$refs

  使用:

    1. 父组件标签中, 在要调用的子组件标签上 增加ref属性

      <子组件标签  ref="自定义名称"></子组件标签>

    2. 父组件js中

    methods:{

      方法名(){

        this.$refs.自定义名称.子组件的方法  /   子组件的变量 ...

      }

    }

3.  子组件直接获取父组件的方法或数据(影响组件复用性,一般用不到)

  this.$parent

  this.$root

  使用:

    直接在子组件的js中使用

    methods:{

      方法1(){

        this.$parent.父组件中方法或数据(只能访问到上一级父组件中)

        或

        this.$root.父组件中方法或数据(直接访问根组件(Vue组件)中)

      }

    }

posted @ 2021-11-04 16:44  黑无常  阅读(60)  评论(0)    收藏  举报