简体   繁体   English

如何在 vuex 中将 object 键/值对设置为初始 state

[英]how to set object key/value pair to initial state in vuex

In my application, I am creating an object to store key/value pair for static texts and passing to the initial state.在我的应用程序中,我正在创建一个 object 来存储 static 文本的键/值对并传递给初始 state。 But, it is showing an error.但是,它显示错误。

Here is my helpers.js :这是我的helpers.js

    export const translation = Object.freeze({
        edit: 'Edit',
        please_select_text: 'Please select a row to edit',
        done: 'Done',
        role: 'Role',
});

Now I am importing this in my store module and passing like this:现在我在我的商店模块中导入它并像这样传递:

const defaultState = () => ({
       localized: translation. // like that i am passing
  
});

In my component, I am using this state like this:在我的组件中,我正在使用这个 state,如下所示:

{{localized.edit}}



...mapState(module_name, ['localized']),

But, it is showing error.但是,它显示错误。 So, how can I use it properly for static texts?那么,如何将它正确地用于 static 文本?

This doesn't seem like something that should live in your store as it's just a helper constant, not state .这似乎不应该存在于您的商店中,因为它只是一个辅助常量,而不是state

In your component simply import translation from '@/path/helpers.js在您的组件中,只需import translation from '@/path/helpers.js

Then use translation.edit .然后使用translation.edit

Additionally if you did want it to live in your store then you're trying to use it as translation.此外,如果您确实希望它存在于您的商店中,那么您正在尝试将其用作translation. instead of how it should be: translation .而不是应该如何: translation (Without the . ) (没有.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM