vue ref如何获取子组件属性值
作者:魔力化
这篇文章主要介绍了vue ref如何获取子组件属性值。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
ref获取子组件属性值
父引入、注册组件并调用组件
引入、注册
<script>
  ....
  import CustomerModal from './modules/CustomerModal'
  export default {
    name: "CustomerList",
    mixins:[JeecgListMixin],
    components: {
      JDate,
      CustomerModal,
      JDictSelectTag
    },
    ...
  }
</script>调用组件
<customer-modal ref="modalForm" @ok="modalFormOk"></customer-modal> // ref属性值指定了从$refs中获取组件的名称
调用子组件的函数
this.$refs.modalForm.add();
调用子组件的属性
this.$refs.modalForm.title = "新增";
子组件更改属性
严格来说,Vue子组件不能随便更改父组件传递过来的属性,但是可以这样修改
父组件
<component-a :num.sync="number">这是子组件</component-a>
子组件
<template>
  <div>
    <p @click="change">子属性{{num}}</p>
  </div>
</template>
<script>
    export default {
        name: "ComponentA",
        props: {
          num: Number
        },
        methods: {
          change(){
            this.$emit('update:num', 10)
          }
        }
    }
</script>以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- Vue获取子组件实例对象ref属性的方法推荐
 - Vue父子组件数据双向绑定(父传子、子传父)及ref、$refs、is、:is的使用与区别
 - vue3 setup中父组件通过Ref调用子组件的方法(实例代码)
 - 详解Vue3 父组件调用子组件方法($refs 在setup()、<script setup> 中使用)
 - Vue3中使用setup通过ref获取子组件的属性
 - vue 父组件通过$refs获取子组件的值和方法详解
 - Vue通过ref父子组件拿值方法
 - vue之父子组件间通信实例讲解(props、$ref、$emit)
 - vue 使用ref 让父组件调用子组件的方法
 - vue如何通过ref调用router-view子组件的方法
 
