美文网首页
Vue之ref的使用

Vue之ref的使用

作者: 刘_小_二 | 来源:发表于2021-07-15 13:24 被阅读0次

1.基本用法,本页获取Dom元素节点

<template>
    <div>
    <div ref="refClass">元素内容</div>
    <button @click="handleTextDom">获取div节点</button>
  </div>
</template>

<script>
    export default {
    methods: {
      handleTextDom(){
        console.log(this.$refs.refClass);
      }
    }
  }
</script>
// 输出
<div元素内容></div>

ref除了可以获取本页面的dom元素,还可以拿到 子组件中 的data 和 去调用子组件中的方法.

2.获取子组件的Data

/// 子组件
<template>
    <div>{{msg}}</div>
</template>
<script>
export default {
  data(){
    return {
      msg: "我是子组件"
    }
  }
}
</script>
/// 父组件
<template>
    <div>
    <comment-Item ref="childData"></comment-Item>
    <button @click="handleTextDom">获取子组件data</button>
  </div>
</template>
<script>
import commentItem from '../../components/comment_item.vue'
export default {
  components:{
    commentItem
  },
  methods: {
    handleTextDom(){
      console.log(this.$refs.childData.msg);
    }
  }
}
</script>
/// 输出
我是子组件

3.父组件调用子组件中的方法

/// 子组件
<template>
    <div></div>
</template>

<script>
    export default {
    methods: {
      open(){
        console.log('子组件 - 方法被调用~');
      }
    }
  }
</script>
/// 父组件
<template>
    <comment-Item ref="childData"></comment-Item>
    <button @click="handleTextDom">获取子组件中的方法</button>
</template>
<script>
    import commentItem from '../../components/comment_item.vue'
  export default {
    components:{
      commentItem
    },
    methods:{
      handleTextDom(){
        // 调用子组件中的open()方法
        this.$refs.childData.open()
      }
    }
  }
</script>
/// 输出
子组件 - 方法被调用~

4.子组件调用父组件中的方法

(操作逻辑:子组件触发一个自定义事件,然后父组件监听这个事件)

/// 子组件
<script>
    export default {
    methods: {
      open(){
        // 子组件调用父组件事件
        this.$emit("fatherClick")
      }
    }
  }
</script>
/// 父组件
<template>
    <div>
    <comment-Item ref="childData" @fatherClick="handelClick"></comment-Item>
    <button @click="handleTextDom">获取子组件中的方法</button>
  </div>
</template>

<script>
import commentItem from '../../compoents/comment_item.vue'
  export default {
    components : {
      commentItem
    },
    methods: {
      handleTextDom(){
        this.$refs.childData.open()
      },
      handleClick(){
        console.log('我是父组件方法')
      }
    }
  }
</script>

子组件的方法, 触发了一个自定义事件, 这个自定义事件 触发了 父组件中的方法。

/// 输出
我是父组件方法

相关文章

  • vue - ref

    vue - ref 说明 ref 是英文单词 reference,代表引用. 在 vue 中, ref 的使用分四...

  • Vue之ref的使用

    1.基本用法,本页获取Dom元素节点 ref除了可以获取本页面的dom元素,还可以拿到 子组件中 的data 和 ...

  • Vue3 Ref 与 Reactive的区别

    Vue3 在组件中使用ref()或reactive()都可以创建响应式数据 Vue2 中操作 Vue3 ref r...

  • Vue3.x ref属性

    获取DOM或者组件实例可以使用ref属性,写法和vue2.0需要区分开。 vue2.0的方式操作ref----数组...

  • Vue.js中ref ($refs)用法举例总结

    看Vue.js文档中的ref部分,自己总结了下ref的使用方法以便后面查阅。 一、ref使用在外面的组件上 HTM...

  • 13-Vue特殊属性-ref

    一、Vue特殊特性 Vue的特殊属性主要有:key、ref、is、slot,ref是Vue特殊属性之一 ref:被...

  • 何时使用ref?何时使用reactive?

    一般情况下vue3有两种定义模板数据的方式ref和reactive,但是何时使用ref?何时使用reactive?...

  • vue中的ref在遍历中调用子组件方法报错

    vue中的ref在遍历中调用子组件方法报错,如下 是因为当 ref 和 v-for 一起使用的时候,得到的 ref...

  • Vue的ref的使用

    一.什么是ref ? ref用来辅助我们获取DOM元素或组件的引用实例对象,每个vue的组件实例上,都包含一个re...

  • vue3父组件调用子组件里面的方法

    与vue2一样定义ref js中定义ref的变量并且返回 在setup语法糖中使用 子组件

网友评论

      本文标题:Vue之ref的使用

      本文链接:https://www.haomeiwen.com/subject/bwwppltx.html