The value from the vue parent component, select the DOM element as the key name of the child component $refs, and print $refs [incoming value] to display undefined

The requirement for

is: an address list, click the letter in the alphabet on the right, and the list jumps to the corresponding initials section.

The structure of the

component is an alphabetical component in the root component, and there is a dynamic component in the root component, component, which switches the following city list components according to the switch between the overseas destination button and the domestic destination button.

The method adopted by

is that when the letter is clicked, the trigger event transmits the e.target.innerHTML to the root component through $emit ("letterChange", e.target.innerHTML), and then the root component transmits the data to the component dynamic component, and finally receives it with props in the overseas destination subcomponent and the domestic destination subcomponent, and uses watch to listen for the change of the data, obtains this.$refs [this.letter] [0] when the change occurs, and calculates the distance from the top. Then set the scrolltop, to scroll to the specified location. The list in DOM is pre-bound to ref with a circular alphabet.

//
<template>
  <div class="destination">
    <letter-list @change="letterChange"></letter-list> //
    <destination-header></destination-header>
    <destination-list :letter="letter"></destination-list> //
  </div>
</template>


/
  <template>
  <section class="del-list">
    <div class="tab-contain" :class="{fixed: isfixed}">
      <ul class="tab">
          <li class="tab-item"
          v-for="item of tabs"
          :key="item.id"
          :class="{ active: currentTab===item.id}"
          @click="currentTab = item.id"
          >{{item.value}}</li>
          <span class="line" :class="{transition:currentTab==="internal-list"}"></span>
      </ul>
    </div>
    <component :is="currentTabComponent" :letter="letter"></component> //
  </section>
</template>


//
<template>
  <div class="abroad-list">
      <div class="hot">
        <div class="title"></div>
        <ul class="hot-recommend">
          <li v-for="item in hotRecommend" :key="item.id">
            <a :href="item.jumpInfo.jumpH5Url">
              <img :src="item.images[0]">
              <div><span>{{item.title}}</span>

{{item.subTitle}}

</div> </a> </li> </ul> </div> <div class="all-dest"> <div class="title"></div> <ul class="all-dest-list"> <li class="dest" v-for="items of allDest" :key="items.title" :ref="items.title"> //titlerefABCD...Z <div class="title">{{items.title}}</div> <ul class="dest-items"> <li v-for="item of items.list" //list :key="item.id"> <a :href="item.jumpInfo.jumpH5Url">{{item.title}}</a> </li> </ul> </li> </ul> </div> </div> </template>

the problem is that the values passed now can be printed out in watch

console.log(this.letter) //A  B  C  D  E...

but when used in this.$refs, the undefined, is as follows:

consol.log(this.$refs[this.letter]) //undefined

plus array index

consol.log(this.$refs[this.letter][0]) //Error in callback for watcher "letter": "TypeError: Cannot read property "0" of undefined"

vue directly reported an error "Error in callback for watcher" letter ":" TypeError: Cannot read property"0" of undefined "

write down the key name of $refs directly, like this

console.log(this.$refs.A[0]) //<li>...</li>
console.log(this.$refs.B[0]) //<li>...</li>
console.log(this.$refs.G[0]) //<li>...</li>
...

can print out the corresponding element content.

what is even more bizarre is

console.log(typeof this.letter) //string

but I use

console.log(this.letter instanceof String) //false

what on earth is this this.letterd?! I"m freaking out. I"ve been doing it all day.
used to pass the clicked data directly into the store of vuex, and then get it in the list component .mapState and listen with watch, but it"s still the same problem.

seek the answer of the Great God

Jun.03,2021

clipboard.png


clipboard.png

clipboard.png

clipboard.png

$refs

clipboard.png

trim

clipboard.png

this problem cannot be found through console printing. I hope it works for you!


try using this.$nextTick (). Please refer to ide/reactivity.html-sharp%E5%BC%82%E6%AD%A5%E6%9B%B4%E6%96%B0%E9%98%9F%E5%88%97" rel=" nofollow noreferrer "> https://cn.vuejs.org/v2/guide.


     //   
       <div class="all-dest">
        <div class="title"></div>
          <ul class="all-dest-list">
            <li class="dest"
            v-for="items, key) of allDest"
            :key="key"
            :ref="key"> //titlerefABCD...Z
              <div class="title">{{key}}</div>

I happen to see the question of Baidu today that I write down for you the way I think is right.

const elm = this.$refsthis.letter
console.log (elm)

try it this way. Use a const definition to see if it is still wrong

.
Menu