首页 > 解决方案 > Vue 可以读取属性

问题描述

在我的项目中,我使用 axios 和状态管理。我有模型的存储和单独文件。在组件中然后安装我运行

      this.$store.dispatch('getComputer2', this.$route.params.id);

并使用 v-model 进行绑定。如果我从另一个页面打开页面按下按钮

                 <router-link tag="button" :to="{ name: 'computerEdit', params: { id: row.item.id } }" class="btn btn-danger">Edit</router-link>

没有错误。但是,如果按 F5 或在控制台中打开页面直接链接,我会看到错误: [Vue 警告]:渲染错误:“TypeError:无法读取 null 的属性‘名称’”

在组件中我使用计算属性:

      computed: {
          computer: {
            get() {
                return this.$store.state.computer.computer;
            },
            set() {
                this.$store.dispatch("addComputer", this.computer);
            }
        }

有什么区别?

附加代码:

接收数据的动作

            const state = {
             computer: null
               };       

             getComputer2 ({commit}, id) {
    axios.get('/actives/GetComputer/' + id)
        .then(res => {
            let computer = res.data;
            console.log(computer)
            commit('SET_COMPUTER', computer);
        })

而这个观点:

            <b-form-group id="input-group-2" label="PC Name:" label-for="input-2">
            <b-form-input
                    id="input-2"
                    v-model="computer.name"
                    required
                    placeholder="Enter name"
            ></b-form-input>
        </b-form-group>

标签: vue.jsaxios

解决方案


推荐阅读