简体   繁体   English

如何在 Vuex 中存储非反应性数据?

[英]How to store non-reactive data in Vuex?

I have data, that loaded on page once before VueJS-application init, and this data will not change for all time while html-page will not reload (classic CGI application, not SPA).我有数据,在 VueJS-application init 之前在页面上加载一次,并且该数据不会一直更改,而 html-page 不会重新加载(经典的 CGI 应用程序,而不是 SPA)。 Data example:数据示例:

const nonReactiveObjectWithSomeNestedData = {
  a: 'a',
  b: {
    bb: 'bb',
    cc: { ccc: 'ccc' },
    dd: ['dd1', 'dd2']
  }
}

I am using this data in a few vue-components.我在一些 vue 组件中使用了这些数据。 It would be handy to store this data in Vuex namespaced module and to use Vuex-getters for wrapping same functionality for different vue-components.将此数据存储在 Vuex 命名空间模块中并使用 Vuex-getter 为不同的 vue 组件包装相同的功能会很方便。 Is there any way to store this data not inside vuex state (reactivity not needed) but with ability to access from vuex-module's getter?有什么方法可以不将这些数据存储在 vuex state (不需要反应性),而是可以从 vuex-module 的 getter 访问?

PS.附注。 Currently I am using storing non-reactive datainside vue-instance method, but now it is not enough, I need more global access to data (from two independent root-components).目前我正在使用在 vue-instance方法中存储非反应性数据,但现在还不够,我需要更多的全局数据访问(来自两个独立的根组件)。

Freeze the object before adding it to the store:在将对象添加到商店之前冻结对象:

Object.freeze(nonReactiveObjectWithSomeNestedData )

Vue won't make frozen objects reactive. Vue 不会使冻结的对象具有反应性。

Note: you should freeze object before it comes into Vuex mutation/action:注意:您应该在对象进入 Vuex 突变/操作之前冻结对象:

this.$store.dispatch('moduleName/setNonReactiveObject', 
    Object.freeze(nonReactiveObjectWithSomeNestedData));

Inside mutation function the payload-parameter will be already reactive.在变异函数中,payload-parameter 已经是反应性的。

One can also add a property _isVue to the object to avoid making it reactive.还可以向对象添加属性_isVue以避免使其具有反应性。

commit('setNonReactiveObject', Object.assign(data, { _isVue: true })

This approach can be useful to make data non-reactive when there's no way to control how the data is stored in the store.当无法控制数据在存储中的存储方式时,此方法可用于使数据非反应性。 For example when store data is completely replaced client-side to "hydrate" a server-side rendered website (eg Nuxt).例如,当存储数据完全替换客户端以“水合”服务器端呈现的网站(例如 Nuxt)时。

This is undocumented and might break in the future.这是未记录的,将来可能会中断。 At least the source code has a comment indicating that this property is used for this internally.至少源代码有一个注释,表明这个属性是在内部使用的。 And here is where the property is checked before observing the object. 是在观察对象之前检查属性的地方。

I was able to remove the "reactiveness" by doing something like this我能够通过做这样的事情来消除“反应性”

// create new variable and remove reactiveness, does a deep clone
var someNewNonReactiveVar = JSON.parse(JSON.stringify(someReactiveVar));

In mutation file you can deep clone the payload that you don't want to be reactive在变异文件中,您可以深度克隆您不想反应的有效负载

import _ from 'lodash';

    [mutationTypes.SET_INITIAL_STATE](state, payload) {
    // In order to make sure that this property is non-reactive deep clone it
    state.initialState = _.cloneDeep(payload)
}

This way the initialState or whatever property you're adding to the state, won't be reactive.这样,initialState 或您添加到状态的任何属性都不会是被动的。

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

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