分配给Vuex计算属性时出错,但它没有设置器

Error in Vuex Computed property was assigned to but it has no setter(分配给Vuex计算属性时出错,但它没有设置器)
本文介绍了分配给Vuex计算属性时出错,但它没有设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示群邀请列表。 此组件有时按预期显示,有时根本不显示。此错误返回:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Computed property "groupInvites" was assigned to but it has no setter.

found in

---> <InviteList> at src/components/Invites/InviteList.vue
       <UserProfile> at src/views/UserProfile.vue
         <App> at src/components/App.vue
           <Root>

以下是生成错误的it组件:

<template>
  <div class="">
    <h4 mt-10 class="text-warning">Your Current Invites:</h4>
    <table class="table">
      <thead>
        <tr>
          <th>Group Name</th>
          <th>Invited By</th>
          <th>Date</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(invite, i) in groupInvites" :key="`${i}-${invite.id} `">
          <td>
            <router-link :to="`/groups/${invite.group_id}`" class="lightbox">
              {{ invite.group_name }}
            </router-link>
          </td>
          <td>{{ invite.sender_id }}</td>
          <td>{{ moment(invite.created_at).strftime("%A, %d %b %Y %l:%M %p") }}</td>
          <td scope="row">
            <a class="btn btn-success mr-3"  @click="acceptInvite(invite)">
              Join Group
            </a>
            <a flat color="grey" @click="deleteInvite(invite.id)">
              <i class="fa fa-trash " ></i>
            </a>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import moment from 'moment-strftime';
import InvitesService from '../../services/InvitesService';
import UsersService from '../../services/UsersService';

export default {
  name: "InviteList",
  components: {
    // NewInvite
  },
  props: {
    // user: {
    //   type: Object,
    //   required: true
    // },
  },
  computed: {
    user() {
      return this.$store.state.auth.user
    },
    groupInvites() {
      return this.$store.state.invites.groupInvites;
    }
  },
  mounted() {
    this.getInvites();
  },
  methods: {
    async getInvites () {
      console.log('in invitelist, getting invites for user: ', this.user.id)
      this.groupInvites = await this.$store.dispatch("getGroupInvites", this.user);
    },
    async getUser (id) {
      this.sender = await UsersService.getUserById({
        id: id
      });
    },
    deleteInvite: async function (id) {
      if(confirm("Do you really want to reject this invite?")) {
        let response = await InvitesService.deleteInvite(id)
        if (response.status === 200) {
          console.log('In Invite.vue, invite deleted, about to emit');
          this.$emit('invite-deleted');
        }
      }
      this.getInvites();
    },
    async acceptInvite(invite) {
      let result = await InvitesService.acceptInvite(invite.invitation_code)
      this.$emit("membership-created");
      console.log('this is the membership created: ', result.data)
      // window.analytics.track('Group Joined', {
      //   title: this.group.name,
      //   user: this.$store.getters.user.username
      // });
      this.getInvites();
    },
    moment: function (datetime) {
      return moment(datetime);
    }
  }
};
</script>

另外,这里是存储模块:

import InvitesService from '@/services/InvitesService'

export const state = {
  groupInvites: []
}

export const mutations = {
  setGroupInvites(state, groupInvites) {
    state.groupInvites = groupInvites;
  }
} 

export const actions = {
  getGroupInvites({ commit }, user) {
    console.log('in store. getting invites, for user: ', user.id)
    InvitesService.getAllUserInvitation(user.id)
      .then(resp => {
        console.log('in store, getGroupInvites,this is groupInvites: ', resp.data);
        commit('setGroupInvites', resp.data);
      });
  }
}

export const getters = {

}
顺便说一句,getGroupInvites被调用了两次。以下是console.logs:

in invitelist, getting invites for user:  9
invites.js?d00b:16 in store. getting invites, for user:  9
InvitesService.js?109c:10 in service getting all user invites for user:  9
invites.js?d00b:16 in store. getting invites, for user:  undefined
InvitesService.js?109c:10 in service getting all user invites for user:  undefined

请注意,第二次循环时未定义用户。

推荐答案

如果您使用computed setter定义了计算值,则可以为其赋值,但是您没有也可能不需要这样做。因此此行是错误的,并抛出错误,因为它试图这样做:

this.groupInvites = await this.$store.dispatch("getGroupInvites", this.user);

但这没有问题,因为this.groupInvites已经从getGroupInvites操作填充的相同状态(state.groupInvites)中反应性地获取了它的值,所以它也是不必要的。将该行更改为:

this.$store.dispatch("getGroupInvites", this.user);

并允许计算进行自我更新。

这篇关于分配给Vuex计算属性时出错,但它没有设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

initialising a global mixin in weex我使用 weex create awesome-project 初始化了一个 weex 和 vue 应用程序。在 entry.js 文件中,我试图注册一个全局 mix...
Correctly setup Eslint Airbnb with VScode in Vue project这是我没有 linting 的 Vuejs 代码。在我运行 之后 npm run lint --fix代码是这样的但我再次做...
TypeError: Cannot read property 'get' of undefined, Axios, Vue.JS尝试使用 axios 从 api 访问获取请求时,我从 Vue 收到这个奇怪的错误,我收到 TypeErro...
How to call a vue.js function on page load我有一个帮助过滤数据的功能。当用户更改选择时,我正在使用 v-on:change 但我还需要在用户选择数据之前调用该函...
How can I call method in the component from view? (vue.js 2)我的看法是这样的:[cc lang=javascript] England ...
Bind vuex state and mutations to checkbox component properties in TypeScript-based Vue问题将复选框创建为 Vue 组件,其中:复选框组件内不允许有逻辑:...