作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
这看起来很蠢,但我是这样设置的:
在 config/index.js
中:
module.exports = {
API_LOCATION: 'http://localhost:8080/api/'
}
然后在 src/app.js
我有:
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource';
Vue.use(VueRouter);
Vue.use(VueResource);
const App = require("./app.vue");
const home = require("./components/home.vue");
const config = require('../config');
window.config = config;
然后在 src/components/home.vue
中,我有一个使用它的脚本 block :
<script>
module.exports = {
data: function() {
return {
obj: null
}
},
created: function() {
this.$http.get(config.API_LOCAITON + '/call').then(res => {
// Do some business
}, res => {
// Handle some error
});
}
}
</script>
这行得通,但我觉得使用 window
来处理应用程序配置不是个好主意。这里更规范的方法是什么?
最佳答案
导入它。
<script>
import config from "../config"
module.exports = {
data: function() {
return {
obj: null
}
},
created: function() {
this.$http.get(config.API_LOCATION + '/call').then(res => {
// Do some business
}, res => {
// Handle some error
});
}
}
</script>
或者只是位置。
<script>
import { API_LOCATION } from "../config"
module.exports = {
data: function() {
return {
obj: null
}
},
created: function() {
this.$http.get(API_LOCATION + '/call').then(res => {
// Do some business
}, res => {
// Handle some error
});
}
}
</script>
关于javascript - vuejs 配置 : using a global variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44750008/
我是一名优秀的程序员,十分优秀!