Vuejs2 通过vue编译包外部的configfile更改组件中的数据
我有一个带有vuejs应用程序的js文件 例如,在npm运行watch poll后,我想用一个配置文件更改标题Vuejs2 通过vue编译包外部的configfile更改组件中的数据,vuejs2,Vuejs2,我有一个带有vuejs应用程序的js文件 例如,在npm运行watch poll后,我想用一个配置文件更改标题 <div id="app"></div> <script src="/js/app.js"></script> 阿雷谢克: <template> <div> {{ title }} <adres-check standalone="true" showButton="
<div id="app"></div>
<script src="/js/app.js"></script>
阿雷谢克:
<template>
<div>
{{ title }}
<adres-check standalone="true" showButton="true"></adres-check>
</div>
</template>
<script>
export default {
data: function () {
return {
title: 'Test'
}
}
}
</script>
<style scoped>
</style>
Vue公司:
},computed: {
//your other computed properties
...mapGetters(["hello"])
}
模板:
{{ hello }}
或created()this.hello返回未定义的当然,您可以尝试以下操作(在
app.js
中):
例如,在您的config.js
中:
fetch("/js/config.js")
.then(res => res.text())
.then(eval)
.then(config => {
/*
Wait for the DOM to be fully loaded
and use the config for your app
*/
});
({
hello: "world"
});
尽管我不得不说,eval
确实是不安全的,而且通常是不好的做法。您可能需要重新思考应用程序的设计方式(例如,使用预处理器/传输程序以及捆绑程序/构建系统,通过他们建议的导入机制导入配置)
例如,如果配置正确,您可以
使用bundler的诸多优点之一是,您还可以使用Vue,并对应用程序拥有绝对控制权,而无需使用技巧和不安全的实用程序,如eval
编辑您似乎正在使用
vuex
,您可以使用配置文件在存储的状态下初始化属性,创建getter,只需在组件中使用Mapgetter
,即可访问所述属性
我强烈建议阅读更多关于如何使用vuex的信息
编辑2:动态加载如果我理解正确,您不希望静态加载配置文件,而是为每个页面加载配置文件(这样您就可以手动更改配置文件,并且仍然可以在不重新编译的情况下观察更改)。因此,除了getter之外,您还需要使用带有突变的操作:
//config.js
({
hello: "world"
})
//where you configure you store
import Vuex from "vuex"
const store = new Vuex.Store({
state: {
//your other states
hello: ""
},
getters: {
//your other getters
hello(state){ return state.hello; }
},
mutations: {
setHello(state, value){ state.hello = value; }
},
actions: {
loadConfig(context){
return fetch("path/to/config")
.then(r => r.text())
.then(eval)
.then(config => {
context.commit("setHello", config.hello);
return config;
});
/*
or if you use JSON
return fetch("path/to/config")
.then(r => r.json())
.then(config => {
context.commit("setHello", config.hello);
return config;
});
*/
}
}
});
//in a component
import { mapGetters } from "vuex"
export default {
//everything else
computed: {
//your other computed properties
...mapGetters(["hello"])
}
}
//app.js
import store from "path/to/store"
store.dispatch("loadConfig").then(_ => {
/*setup your app when the dom is fully loaded*/
});
一个缺点是我们重新使用eval
。但是,如果您可以在配置中使用JSON而不是普通js,那么会更安全。
现在您有了
loadConfig
操作,您也可以在需要重新加载配置时发送它,您只需访问存储即可。我已经更改了我想要的描述。例如,我想通过configfile.js更改标题。那么,在npm运行dev/watch poll之后,我该怎么做呢?是的,我正在使用vuex,在哪里可以阅读包含/导入配置的解决方案。jsI刚刚添加了一个示例我导入了Mapgetter,但是如何使用hello?!我真的鼓励您在配置文件中使用JSON而不是普通的JS,它可以消除很多安全问题
({
hello: "world"
});
//dev/config.js
export default {
hello: "world"
};
//dev/app.js
import config from "./config"
/*
Wait for the DOM to be fully loaded
and use the config for your app
*/
//where you configure you store
import config from "path/to/config"
import Vuex from "vuex"
const store = new Vuex.Store({
state: {
//your other states
...config
},
getters: {
//your other getters
hello(state){ return state.hello; }
}
});
//in a component
import { mapGetters } from "vuex"
export default {
//everything else
computed: {
//your other computed properties
...mapGetters(["hello"])
}
}
//config.js
({
hello: "world"
})
//where you configure you store
import Vuex from "vuex"
const store = new Vuex.Store({
state: {
//your other states
hello: ""
},
getters: {
//your other getters
hello(state){ return state.hello; }
},
mutations: {
setHello(state, value){ state.hello = value; }
},
actions: {
loadConfig(context){
return fetch("path/to/config")
.then(r => r.text())
.then(eval)
.then(config => {
context.commit("setHello", config.hello);
return config;
});
/*
or if you use JSON
return fetch("path/to/config")
.then(r => r.json())
.then(config => {
context.commit("setHello", config.hello);
return config;
});
*/
}
}
});
//in a component
import { mapGetters } from "vuex"
export default {
//everything else
computed: {
//your other computed properties
...mapGetters(["hello"])
}
}
//app.js
import store from "path/to/store"
store.dispatch("loadConfig").then(_ => {
/*setup your app when the dom is fully loaded*/
});