Home > database >  How to use refs in vue js as querySelectAll
How to use refs in vue js as querySelectAll

Time:03-28

What I need is to change the color of links when scrolling up and down. How can I select Link 1/2/3 by using ref?

<nav >
   <ul>
     <li >
       <router-link to="/">Link 1 </router-link>
     </li>
     <li >
       <router-link to="/">Link 2  </router-link>
     </li>
     <li >
       <router-link to="/">Link 3 </router-link>
     </li>
       </ul>

</nav>

CodePudding user response:

You have access to your refs by using this.$refs.

example

const anchors = this.$refs["a"];

console.log(anchors);

CodePudding user response:

Use this method

this.$refs["n1"].$el.style.color = 'red'
this.$refs["n2"].$el.style.color = 'green'
this.$refs["n3"].$el.style.color = 'blue'
<nav >
    <ul>
        <li >
            <router-link to="/" ref="n1">Link 1 </router-link>
        </li>
        <li >
            <router-link to="/" ref="n2">Link 2  </router-link>
        </li>
        <li >
            <router-link to="/" ref="n3">Link 3 </router-link>
        </li>
    </ul>
</nav>

  • Related