The method of using ref to call sub-components in vue3.0

Foreword:

      Compared with the ref in vue2.0, the use of ref in vue3.0 has also changed a lot. Here is a summary of his specific usage.

view 2.0

Parent component:

template

 <Table ref="eleTable" @handle="handleFun"></Table>

methods:

this.$refs.eleTable.子组件的方法名+()

this.$refs.eleTable.子组件的属性名

vue 3.0

Parent component:

template

 <Table ref="eleTable" @handle="handleFun"></Table>

In the script:

import {  ref  } from 'vue'
setup() { 
   //ref方法
    const eleTable = ref()  //eleTable是页面ref后面对应的名字
    const clickSon = () => {
      eleTable.value.changeShowText() //调用子组件的方法
      let arr = eleTable.value.tableData //获取子组件 setup 里面定义的变量
    }
}

Get the data of the sub-component

Subassembly:

template:

<el-button type="success" style="margin:10px 0">{
   
   {conts}}</el-button>

script:

import {  ref  } from 'vue'
setup(){
    const tableData = [{
      date: '2016-05-02',
      name: '王小虎',
      address: '上海市普陀区金沙江路 1518 弄'
    }]


    const conts = ref("我是子组件"); //我是子组件是默认展示的数据
    const changeShowText = () =>{
      conts.value = "22222222222";
    }

    return {
      //数据
      tableData,
      conts,
      //函数方法
      changeShowText

    }
}

Okay, it's over here

Guess you like

Origin blog.csdn.net/qq_41619796/article/details/114291319