【问题标题】:Is it possible to pass a reactive property in VueJS?是否可以在 VueJS 中传递反应属性?
【发布时间】:2020-03-29 17:59:17
【问题描述】:

我从另一个开发人员那里接管了一个代码。他使用了一个 Vuex 属性 POLL 来初始化一些组件。我想使用不同的方法——在上层获取对象并将其作为下游属性传递给组件。该对象是从后端以异步方法获取的。我认为 Vue 反应性稍后会初始化实际值。但我得到一个错误:

[Vue warn]: Property or method "poll" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <PollHeading> at src/components/molecules/PollHeading.vue
   <CompletePoll> at src/components/organisms/CompletePoll.vue
     <Home> at src/views/Home.vue

首页.vue

<Poll v-if="this.$store.getters.LATEST_POLL" :item="this.$store.getters.LATEST_POLL" />
... 
created() {
 this.$store.dispatch('GET_LATEST_POLL');
},

CompletePoll.vue

<PollHeading :item="item"/>
... 
props: {
  item: Object,
},

PollHeading.vue

props: {
  item: Object,
},

我是不是走错了方向,而原来的开发者是对的?或者有没有办法解决我的方法?他曾经这样做:

PollHeading.vue(我改名为 CompletePoll.vue)

computed: {
    poll() {
      return {
        poll: this.$store.getters.POLL,
      };
    },

【问题讨论】:

  • 你试过mapGetters吗?像这样的东西:computed: { ...mapGetters(['POLL']) }.

标签: vue.js vuex


【解决方案1】:

问题 1)

[Vue warn]: Property or method "poll" is not defined on the instance but referenced during render. 表示你在.vue 文件的template 块中声明了这个,但它没有在script's data property or computed 中声明。我没有看到您使用 poll 变量的特定 .vue 文件。

问题 2) 当你在 tmeplate 块中使用变量时,你不应该使用 this

所以不要这样:&lt;Poll v-if="this.$store.getters.LATEST_POLL" :item="this.$store.getters.LATEST_POLL" /&gt;

写信:&lt;Poll v-if="$store.getters.LATEST_POLL" :item="$store.getters.LATEST_POLL" /&gt;

问题 3) https://github.com/literakl/mezinamiridici/blob/master/spa/src/components/molecules/PollHeading.vue 在此文件上,您在 template 中使用 poll,但您没有在任何地方创建该变量。你的道具名称是item 而不是poll。所以将 poll 更改为 item in template 在该文件中的任何位置阻止。

【讨论】:

  • 你好,完整的源代码在那里:github.com/literakl/mezinamiridici/tree/master/spa/src
  • 后面有一个未定义的错误:ypeError: Cannot read property 'poll' of undefined in /src/components/molecules/PollHeading.vue?vue&type=template
  • 更新了我的答案。
  • 你说得对,我也注意到了。愚蠢的旧代码。我修复了它,重新编译了它,但它仍然存在。如何指示 vue 打印错误行?在没有给出上下文的情况下很难定位。
  • 您应该使用sourcemaps 来查看实际的文件和错误行。错误应该已经消失了。
猜你喜欢
  • 2020-04-01
  • 2021-10-12
  • 2018-09-09
  • 2019-01-30
  • 1970-01-01
  • 2021-12-30
  • 2021-07-01
  • 2020-07-04
  • 2019-04-03
相关资源
最近更新 更多