Home  >  Article  >  Web Front-end  >  Detailed explanation of using ref steps in vue2

Detailed explanation of using ref steps in vue2

php中世界最好的语言
php中世界最好的语言Original
2018-04-16 14:04:391565browse

This time I will bring you a detailed explanation of the steps for using ref in vue2. What are the precautions for using ref in vue2. The following is a practical case, let's take a look.

This article introduces the specific use of ref in vue2 and shares it with everyone. The details are as follows.

1. Let’s first define two components

html part

  <p id="app">
   <navbar ></navbar>
   <pagefooter ></pagefooter>
  </p>

jsPart

  Vue.component('navbar',{
    template:'<p>{{navs}}</p>',
    data:function () {
     return {
      navs:1
     }
    }
   });
  Vue.component('pagefooter',{
    template:'<p>{{footer}}</p>',
    data:function () {
     return {
      footer:11
     }
    }
   });

How to directly access the navs of navbar and the footer value of pagefooter here? This uses ref

2. Use of ref

Modify component

  <p id="app">
   <navbar ref="navbar"></navbar>
   <pagefooter ref="pagefooter"></pagefooter>
  </p>
   new Vue({
    el:'#app',
    created:function(){
     
    },
    mounted:function () {
     this.$refs.navbar.navs=222
     //ready,
     //这里怎么直接访问navbar的navs和pagefooter的footer值呢,
      console.log(this.$refs.navbar.navs);
      console.log(this.$refs.pagefooter.footer);
    }
   })

If you only use it on a common tag

<p ref="demo"></p>

His role is similar to:

document.querySelector('[ref=demo]');

The effect is the same

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:



The above is the detailed content of Detailed explanation of using ref steps in vue2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn