vue.jsvuejs3refs

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

  • 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>