'How to use refs in vue js as querySelectAll
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 class="header-nav">
<ul>
<li class="tablet-sm-hide">
<router-link to="/">Link 1 </router-link>
</li>
<li class="tablet-sm-hide">
<router-link to="/">Link 2 </router-link>
</li>
<li class="tablet-sm-hide">
<router-link to="/">Link 3 </router-link>
</li>
</ul>
</nav>
Solution 1:[1]
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 class="header-nav">
<ul>
<li class="nav">
<router-link to="/" ref="n1">Link 1 </router-link>
</li>
<li class="nav">
<router-link to="/" ref="n2">Link 2 </router-link>
</li>
<li class="nav">
<router-link to="/" ref="n3">Link 3 </router-link>
</li>
</ul>
</nav>
Solution 2:[2]
You have access to your refs by using this.$refs.
example
const anchors = this.$refs["a"];
console.log(anchors);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GMKHussain |
| Solution 2 | Maik Lowrey |
