Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  197] [ 6]  / answers: 1 / hits: 21532  / 7 Years ago, sun, june 25, 2017, 12:00:00

This seems dumb, but I have it setup like this:



in config/index.js:



module.exports = {
API_LOCATION: 'http://localhost:8080/api/'
}


then in src/app.js I have:



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;


Then in src/components/home.vue, I have a script block that uses it like so:



<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>


This works but it strikes me as a bad idea to use window to handle an application configuration. What's the more canonical approach here?


More From » node.js

 Answers
8

Import it.



<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>


Or just the location.



<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>

[#57304] Friday, June 23, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austynp

Total Points: 505
Total Questions: 118
Total Answers: 106

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
austynp questions
;