Home > Web Front-end > JS Tutorial > body text

Detailed explanation of using ref steps in vue2

php中世界最好的语言
Release: 2018-04-16 14:04:39
Original
1583 people have browsed it

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>
Copy after login

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
     }
    }
   });
Copy after login

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);
    }
   })
Copy after login

If you only use it on a common tag

<p ref="demo"></p>
Copy after login

His role is similar to:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template