Skip to content
This repository was archived by the owner on Aug 8, 2022. It is now read-only.
This repository was archived by the owner on Aug 8, 2022. It is now read-only.

ref && v-for #357

Closed
Closed
@JackShell2020

Description

@JackShell2020

官方文档

  • ref 与 v-for 一起使用时,你得到的 ref 将是一个数组,其中包含镜像数据源的子组件。(3.x的文档)
  • 当 ref 和 v-for 一起使用的时候,你得到的 ref 将会是一个包含了对应数据源的这些子组件的数组。(2.x的文档)

我的问题

<template>
  <div>
    <p v-for="item of arr" :key="item" :ref="item">{{item}}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        'aaaa',
        'bbbb',
      ],
    };
  },
  mounted() {
    console.log(this.$refs.aaaa)
  },
};
</script>
  • ref=固定值 时: <p v-for="item of arr" :key="item" ref="myP">{{item}}</p>
    • 然后 console.log( this.$refs.myP ) 输出的应当是文档中说的数组
  • ref=不是固定值 时: <p v-for="item of arr" :key="item" :ref="item">{{item}}</p>
    • 然后 console.log( this.$refs.aaaa)
    • 在2.x版本中,输出的依然是数组(为什么)
    • 在3.x版本中,输出的确实当前节点(为什么)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions