简体   繁体   English

如何在 vue 中正确存储非反应性和 static 数据?

[英]How to properly store non-reactive and static data in an vue?

Greetings to the community.向社区问好。 This is my first question on the StackOverflow.这是我在 StackOverflow 上的第一个问题。

I use vue.js-v2 and webpack.我使用 vue.js-v2 和 webpack。 I need to have immutable data available for child components.我需要为子组件提供不可变数据。 The components are loaded via the vue-router.组件通过 vue-router 加载。 Different pages need the same data.不同的页面需要相同的数据。

I import data from a JSON file and integrate it into vue with a "main.js":我从 JSON 文件导入数据,并使用“main.js”将其集成到 vue 中:

//main.js
import Vue from 'vue'
import ...
import myData from './path/file.json' // [{},{},{}...]
Vue.prototype.$storage = myData
new Vue({ router, .....

Then I use in different components something like:然后我在不同的组件中使用,例如:

//pageX.vue
...
this.componentVar = this.$storage.filter((x) => x.name === 'needName')

And somehow it works.不知何故,它起作用了。 I worry if I'm doing the right thing.我担心我是否做对了。 I am afraid of duplicating data in components or something like that, because my knowledge of javascript is deplorable:(我害怕在组件中复制数据或类似的东西,因为我对 javascript 的了解令人遗憾:(

You have to use Vuex to store that data.. wheater immutable or not a state of an app has to a single absolute source of truth and no alternative compared to vuex can help you in this case您必须使用 Vuex 来存储该数据.. 应用程序的 state 是否具有单一的绝对事实来源,在这种情况下,与 vuex 相比,没有其他选择可以帮助您

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

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