简体   繁体   English

this.$refs 为空,带有 Vue 3 选项 API

[英]this.$refs empty with Vue 3 Options API

In Vue 2 I used to be able to access a property on component children (rendered inside a v-for loop using this.$refs and a dynamically-assigned:ref).在 Vue 2 中,我曾经能够访问子组件的属性(使用 this.$refs 和动态分配的:ref 在 v-for 循环中呈现)。

The same code in Vue 3 fails, and when I log out this.$refs the object is empty. Vue 3 中的相同代码失败,当我注销this.$refs时,object 为空。

Here I'm wanting to access an ' isOrderable ' property on all children.在这里,我想访问所有孩子的“ isOrderable ”属性。 The problem appears to be with :ref="product.id" being a variable.问题似乎在于:ref="product.id"是一个变量。 If I change it to ref="foobar" then I do get the last child in this.$refs.foobar .如果我将其更改为ref="foobar"那么我会得到this.$refs.foobar中的最后一个孩子。 But it vue2 me an array back containing all children components.但它 vue2 我一个包含所有子组件的数组。

  <script>
  import productItem from "./Product.vue";
  export default {
    props: ["products"],
    components: {
      'product-item': productItem
    }
    methods: {
      addAllProducts() {
        const orderable = this.products.filter((p) => this.$refs[p.id][0].isOrderable);
        ...
      }
    }
  }
  </script>

  <template>
    <form>
      <div v-for="product in products" :key="product.id">
        <product-item :product="product" :ref="product.id" />
      </div>
      <button @click="addAllProducts">Add All</button>
    </form>
  </template>

Obviously something changed in vue3 but I can't find any info about it.显然 vue3 发生了一些变化,但我找不到任何关于它的信息。 There's plenty of info on this.$refs , and but it all has to do with accessing refs from the composition API.关于this.$refs有很多信息,但这一切都与从 API 组合访问 refs 有关。

Any help appreciated.任何帮助表示赞赏。

In vue 3 they change how refs work with arrays, now you need to pass a function and have a state on your data to keep track of your refs https://v3.vuejs.org/guide/migration/array-refs.html#frontmatter-title . In vue 3 they change how refs work with arrays, now you need to pass a function and have a state on your data to keep track of your refs https://v3.vuejs.org/guide/migration/array-refs.html #frontmatter 标题

I don't know how your code is structured but maybe there is a better solution to your problem than using refs, if the logic that toggles if a product-item is orderable lives inside the product-item component you can have an event that emits when the orderable value is changed an update an array of orderableProducts with the id of each product, you can even use that in a v-model with the multiple v-models options of vue3.我不知道您的代码是如何构造的,但也许有比使用 refs 更好的解决方案来解决您的问题,如果在产品项目可订购时切换的逻辑存在于产品项目组件中,您可以发出一个事件当可订购值更改时,使用每个产品的 id 更新一个可订购产品数组,您甚至可以在具有 vue3 的多个 v-models 选项的 v-model 中使用它。 in that way you don't need to hold a reference of the dom just to filter by the ones that are orderable.这样你就不需要持有dom的引用来过滤那些可订购的。

Refs in Composition API (super-simple example)组合 API 中的参考(超级简单示例)

setup() {
    const {ref} = Vue
    const abc = ref(null)

    function doSomething() {
        abc.value.focus()
    }

    return {abc}
},

template: `
    <component ref="abc" />
`

Still, if you want to access ref element with function outside setup(), for example from router guard beforeRouteLeave function, then you use:尽管如此,如果您想在 setup() 外部使用 function 访问 ref 元素,例如从路由器保护 beforeRouteLeave function 中,那么您可以使用:

this.$refs.abc... (without value) this.$refs.abc... (没有价值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM