【问题标题】:How to set initial values for data from vuex?如何为来自 vuex 的数据设置初始值?
【发布时间】:2018-12-06 09:31:21
【问题描述】:

我的目标是创建一个“编辑帐户”表单,以便用户可以修改其帐户数据。我想以已经填充了用户数据(即用户名、电子邮件、地址...

然后用户可以修改表单中的数据并提交将更新其用户信息的表单。

我正在使用 v-model 将表单输入绑定到我的数据中名为 accountInfo 的对象,如下所示:

data() {
    return {
        accountInfo: {
                firstName: ''
        }
    }
}

下面是我的模板中的表单输入示例:

<input v-model.trim="accountInfo.firstName" type="text" class="form-control" id="first-name" />

对象中键的值当前是空字符串,但我希望这些值来自一个名为 userProfile 的对象,它是 vuex 中的一个状态属性。

在我的“编辑帐户”组件中,我通过导入映射 vuex 状态:

import { mapState } from "vuex";

然后在计算属性中使用以下内容

computed: {
    ...mapState(["userProfile"])
}

我想做的不是将空字符串作为 accountInfo 的值,而是从 vuex 映射的 userProfile 计算属性中为它们分配值,如下所示:

data() {
        return {
            accountInfo: {
                    firstName: this.userProfile.fristName,
            }
        }
    }

这将为我的表单提供所需的初始数据,但不幸的是这不起作用,可能是因为数据在生命周期中比计算属性更早呈现。

完整代码:

EditAccount.vue

<template>
    <div class="container-fluid">
        <form id="sign_up_form" @submit.prevent>
            <div class="form-row">
                <div class="form-group col-md-6">
                    <input v-model.trim="signupForm.firstName" type="text" class="form-control" id="first_name" />
                </div>
            </div>
        </form>
    </div>
</template>

<script>
import { mapState } from "vuex";
import SideBar from "../common/SideBar.vue";

export default {
  name: "EditAccount",
  computed: {
    ...mapState(["userProfile"])
  },
  data() {
    return {
      accountInfo: {
        firstName: this.userProfile.firstName
      }
    };
  }
};
</script>

store.js:

export const store = new Vuex.Store({
    state: {
        userProfile: {firstName: "Oamar", lastName: "Kanji"}
    }
});

【问题讨论】:

标签: javascript vue.js vuex v-model


【解决方案1】:

你是对的,计算是在调用初始 data 函数后评估的。

快速修复

在 cmets 中,@Jacob Goh mentioned 如下:

$store 应该在调用数据函数之前准备好。因此,firstName: this.$store.state.userProfile.firstName 应该可以正常工作。

export default {
  name: 'EditAccount',
  data() {
    return {
      accountInfo: {
        firstName: this.$store.state.userProfile.firstName
      }
    }
  }
};

真的需要计算吗?

参见@bottomsnap's answer,其中设置初始值可以在mounted 生命周期挂钩中完成。

使用您的代码,它看起来像这样:

import { mapState } from 'vuex';

export default {
  name: 'EditAccount',
  computed: {
    ...mapState(['userProfile'])
  },
  data() {
    return {
      accountInfo: {
        firstName: ''
      }
    }
  }
  mounted() {
    this.accountInfo.firstName = this.userProfile.firstName;
  }
};

虽然它可能会在没有值的情况下渲染一次,并在安装后重新渲染。

容器与展示

我解释了Vue's communication channels in another answer,但这里有一个简单的例子来说明你可以做什么。

将 Form 组件视为表示逻辑,因此它不需要了解商店,而是将配置文件数据作为道具接收。

export default {
    props: {
        profile: {
            type: Object,
        },
    },
    data() {
        return {
            accountInfo: {
                firstName: this.profile.firstName
            }
        };
    }
}

然后,让父处理业务逻辑,从存储中获取信息,触发操作等。

<template>
    <EditAccount :profile="userProfile" :submit="saveUserProfile"/>
</template>
<script>
import { mapState, mapActions } from "vuex";

export default {
    components: { EditAccount },
    computed: mapState(['userProfile']),
    methods: mapActions(['saveUserProfile'])
}
</script>

虽然Jacob is not wrong saying that the store is readythis.$store.state.userProfile.firstName 会起作用,但我觉得这更像是一个解决设计问题的补丁,可以通过上述解决方案轻松解决。

【讨论】:

    【解决方案2】:

    像以前一样将您的输入与 v-model 绑定:

    <div id="app">
        <input type="text" v-model="firstName">
    </div>
    

    使用mounted生命周期钩子设置初始值:

    import Vue from 'vue';
    import { mapGetters } from 'vuex';
    
    new Vue({
          el: "#app",
          data: {
            firstName: null
          },
          computed: {
            ...mapGetters(["getFirstName"])
          },
          mounted() {
            this.firstName = this.getFirstName
          }
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-04
      • 2017-06-09
      • 1970-01-01
      • 2022-12-18
      • 2020-10-11
      • 2016-10-16
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多