vue动态添加了:在此中没有显示v-for中的参考。$ refs

发布于 2025-01-24 20:21:06 字数 1682 浏览 2 评论 0原文

我是vue.js的新手,我有一个vue.js组件,当单击“添加元素”时,基本上显示了数据数组中的数字,代码如下:

<template>
  <div>
    <button @click="updateCounter">Add elements</button>
    <div
      v-for="number in numsArray"
      :key="number"
      :ref="`${number}`"
    >
      <div>
        <p>{{ number }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import { SocketUpdate as socketUp } from "./components/Data";

export default {
  name: "App",
  data() {
    return {
      numsArray: [],
      counter: 0,
    };
  },
  mounted() {
    console.log("REFS when mounted::", this.$refs);
  },
  beforeUpdate() {
    console.log("REFS before updated::", this.$refs);

  },
  updated() {
    console.log("REFS updated::", this.$refs);
  },
  methods: {
    updateCounter() {
      this.counter++;
      const copyOfArray = [...this.numsArray];
      copyOfArray.push(this.counter);
      this.numsArray = copyOfArray;
      console.log(this.numsArray);
    }
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

但是,1。单击按钮时并将新元素添加到numsArray中,并显示在V-For Div中,:ref =“ $ {number} ” “ 从不与各个元素关联,因为尝试打印this。$ refs在已安装,更新或挂钩之前的数组;该数组为空的(无参考),这意味着这些元素是在DOM中创建的,这些数字是可见的,但是与它们相关的参考文献永远不会创建或添加到this中。$ refs array。和2。console.log(“ refs ...”,this。$ refs);仅在单击按钮时显示在pertupdate中,然后在安装架上时显示组件通过pertupdate,而不是在更新的情况下传递,如果数据元素(在这种情况下为NUMSARRAY)已更新,您是否知道如何调用更新的挂钩?

感谢您!

I'm new to vue.js, and I have a vue.js component that basically shows numbers from a data array when the button "Add elements" is clicked, the code is the following:

<template>
  <div>
    <button @click="updateCounter">Add elements</button>
    <div
      v-for="number in numsArray"
      :key="number"
      :ref="`${number}`"
    >
      <div>
        <p>{{ number }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import { SocketUpdate as socketUp } from "./components/Data";

export default {
  name: "App",
  data() {
    return {
      numsArray: [],
      counter: 0,
    };
  },
  mounted() {
    console.log("REFS when mounted::", this.$refs);
  },
  beforeUpdate() {
    console.log("REFS before updated::", this.$refs);

  },
  updated() {
    console.log("REFS updated::", this.$refs);
  },
  methods: {
    updateCounter() {
      this.counter++;
      const copyOfArray = [...this.numsArray];
      copyOfArray.push(this.counter);
      this.numsArray = copyOfArray;
      console.log(this.numsArray);
    }
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>

However, 1. when the button is clicked and new elements are added to the numsArray and displayed in the v-for div, the :ref="${number}" is never associated to the respective element, because when trying to print the this.$refs array in the mounted, updated or beforeUpdated hooks; that array empty (no refs) which means that the elements are created in the DOM, those numbers are visible, but the refs related to them are never created nor added to the this.$refs array. And 2. The console.log("REFS...", this.$refs); is only shown in the beforeUpdate when the button is clicked, and once in the mounted, so hows possible that the component passed through the beforeUpdate and not in the updated, if the data elements (numsArray in this case) were updated, do you know how exactly is the updated hook is invoked?

Thanks you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文