当前位置:   article > 正文

VUE3语法-$refs和ref属性的使用_vue3 refs

vue3 refs

1、$refs和ref属性的使用

1、$refs:一个包含 DOM 元素和组件实例的对象,通过模板引用注册。

2、ref实际上获取元素的DOM节点

3、如果需要在Vue中操作DOM我们可以通过ref和$refs这两个来实现

总结:$refs可以获取被ref属性修饰的元素的相关信息。

1.1、$refs和ref使用-非组件环境

$refs f的使用至少需要写在mounted中,等待组件渲染结束,否则获取不到信息。

在下面的案例中,我们将template中的div加入属性ref=”comp2”,并打算在mounted中获取相关的DOM信息。

注意点:这是是没有使用组件的用法,后面有组件的用法。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://unpkg.com/vue@3"></script>
  9. </head>
  10. <body>
  11. <div id="app"></div>
  12. <script type="module">
  13. //实例化vue实例
  14. const { createApp } = Vue
  15. const app=createApp({
  16. mounted(){
  17. },
  18. template:`
  19. <div>
  20. <div ref="comp2" name="div111">hello vue</div>
  21. </div>
  22. `
  23. });
  24. //vue实例只作用于app这个DOM节点中
  25. app.mount('#app');//viewModel是组件帮助我们完成的
  26. </script>
  27. </body>
  28. </html>

1.1.1、获取名称为comp2的ref节点

核心代码:this.$refs.comp2

  1. mounted(){
  2. console.log(this.$refs.comp2)
  3. },

53a08a2d5b0b495bb01fb917b5174f46.png

1.1.2、获取名称为comp2节点中的值

核心代码:this.$refs.comp2.innerHTML

  1. mounted(){
  2. console.log(this.$refs.comp2)
  3. console.log(this.$refs.comp2.innerHTML)
  4. },

af761531d8c34d23a8e6aaf642ee36cf.png

1.1.3、获取名称为comp2节点中属性的值

核心代码:this.$refs.comp2.attributes.name

  1. mounted(){
  2. console.log(this.$refs)
  3. console.log(this.$refs.comp2.innerHTML)
  4. //获取属性name的值
  5. console.log(this.$refs.comp2.attributes.name)
  6. },

3fa9336a49514f639b606707b3db7592.png

1.2、$refs和ref使用-组件环境

在vue中定义了一个全局组件component2

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://unpkg.com/vue@3"></script>
  9. </head>
  10. <body>
  11. <div id="app"></div>
  12. <script type="module">
  13. //实例化vue实例
  14. const { createApp } = Vue
  15. const app=createApp({
  16. mounted(){
  17. console.log(this.$refs)
  18. console.log(this.$refs.comp2.innerHTML)
  19. console.log(this.$refs.comp2.attributes.name)
  20. },
  21. template:`
  22. <div>
  23. < component2 ref="comp" > </component2>
  24. </div>
  25. `
  26. });
  27. //定义一个全局组件
  28. app.component("component2",{
  29. methods:{
  30. event1(){
  31. console.log("======1======");
  32. }
  33. },
  34. data(){
  35. return {
  36. name:"晓春111"
  37. }
  38. },
  39. template:`<div name="div111">hello vue111111111</div> `
  40. });
  41. //vue实例只作用于app这个DOM节点中
  42. app.mount('#app');//viewModel是组件帮助我们完成的
  43. </script>
  44. </body>
  45. </html>

1.2.1、获取到子组件comp的节点

核心代码:this.$refs.comp

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. },

1ba784aa56ef49e58d1c6dc6be0b9e7b.png

1.2.2、获取到子组件comp的节点中定义的函数

核心代码:this.$refs.comp.event1

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. console.log(this.$refs.comp.event1)
  4. },

68cc2bb6db824d3b9e3e0867136f694a.png

1.2.3、获取到子组件comp的节点data中定义的属性值

核心代码:this.$refs.comp.name

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. console.log(this.$refs.comp.event1)
  4. console.log(this.$refs.comp.name)
  5. },

6d4145be1e854da5b79b6d925fe903f6.png

1.2.4、获取到子组件comp的节点中template的值

核心代码:this.$refs.comp.$el

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. console.log(this.$refs.comp.event1)
  4. console.log(this.$refs.comp.name)
  5. console.log(this.$refs.comp.$el)
  6. },

8a06f624a0c1450fad876b5ab91d7325.png

1.2.5、获取到子组件comp的节点中template中元素属性的值

核心代码:this.$refs.comp.$el.attributes.name

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. console.log(this.$refs.comp.event1)
  4. console.log(this.$refs.comp.name)
  5. console.log(this.$refs.comp.$el)
  6. console.log(this.$refs.comp.$el.attributes.name)
  7. },

7d70c29552104c6685f17b5d31ae6b20.png

1.2.6、获取到子组件comp的节点中template中元素的值

核心代码:this.$refs.comp.$el.innerHTML

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. console.log(this.$refs.comp.event1)
  4. console.log(this.$refs.comp.name)
  5. console.log(this.$refs.comp.$el)
  6. console.log(this.$refs.comp.$el.attributes.name)
  7. console.log(this.$refs.comp.$el.innerHTML)
  8. },

e864ef817a1742c4aceb6a85ec716c1a.png

1.2.7、获取到子组件comp的节点中template中元素的值

核心代码:this.$refs.comp.$data

$data能够获取我们定义在data中的参数。也就是

data(){

        return {

          name:"晓春111"

        } }

的值

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. console.log(this.$refs.comp.event1)
  4. console.log(this.$refs.comp.name)
  5. console.log(this.$refs.comp.$el)
  6. console.log(this.$refs.comp.$el.attributes.name)
  7. console.log(this.$refs.comp.$el.innerHTML)
  8. console.log(this.$refs.comp.$data)
  9. },

e563ba4e657541a4b81d2e22e9f32924.png

1.2.8、获取子组件comp中template自定义属性

核心代码:this.$refs.comp.$options

  1. mounted(){
  2. console.log(this.$refs.comp)
  3. console.log(this.$refs.comp.event1)
  4. console.log(this.$refs.comp.name)
  5. console.log(this.$refs.comp.$el)
  6. console.log(this.$refs.comp.$el.attributes.name)
  7. console.log(this.$refs.comp.$el.innerHTML)
  8. console.log(this.$refs.comp.$data)
  9. console.log(this.$refs.comp.$options)
  10. },

e3f29476f6974e3790f8d189b825966a.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/443891
推荐阅读
相关标签
  

闽ICP备14008679号