Good afternoon.
I was wondering whether it is possible to add a config.json file to a Vue CLI 3 project that can be read at runtime, both during development and production.
The config.json file will contain some strings the app can use to change content dynamically, such as the video files that are displayed to a user and the IP address of a printer.
// config.json
{
"brand": "eat",
"printerIP": "192.168.0.4"
}
I have attempted to place the file in the assets folder and the public folder. I have tried importing it to the script lang="ts" element of App.vue using both import and require statements. I am using the vue-property-decorator.
// App.vue (<script lang="ts">)
const config = require('../public/config.json');
OR
import config from '../public/config.json';
I have accessed the values both by processing it like an API using axios and simply accessing the variables using, for example, config.brand.
// App.vue (<script lang="ts">)
import axios from 'axios';
@Provide() private brand: string = "";
axios
.get('.config.json')
.then(response => {
this.brand = response.data.brand;
})
OR
this.brand = config.brand;
Unfortunately, the data is only read at build time and compiled into minified JavaScript. This means the app is not updated even if the user updates the variables in config.json after the app is built. I need to be able to access config.json in the build files, update a value and then have the app read the new value at runtime without the need to build the project again. I was wondering if it is possible to do this.
Any help you can provide will be greatly appreciated.
Reading the JSON file We can read a (local) JSON file by importing it into the vue component and rendering the data into the dom by looping through it. In the above code, we first imported the users. json file as usersData then assigned it to the users data property.
In order to use TypeScript in this component, you will need to add a lang attribute to the script tag of your component. The value of that attribute should be ts . When using TypeScript in single-file Vue components, the Vue library must be imported so you can extend from it.
Actually, you can also implement TypeScript one file at the time with Vue (if you add it to an existing project). You take one of your Vue files and add the lang="ts" inside the script tag. Then you modify your component to use the Class API and fix the potential errors TypeScript found. It's really easy!
You're right in thinking that you should put runtime configurations under the public/
folder, but don't import them. The import would get resolved during build time and its result would go through webpack. Instead, use fetch
to load them via HTTP.
Vue designates the public
folder as the place to put static assets that should bypass webpack and simply by copied to dist/
. You can access them during runtime with an HTTP request like this:
const getRuntimeConfig = async () => {
const runtimeConfig = await fetch('/config');
return await runtimeConfig.json()
}
If you want to use those runtime configurations in multiple Vue components without doing multiple fetches, then do one fetch in the main Javascript file that drives your Vue app (e.g. src/main.js
) and share the result via Vue mixins, like this:
Vue.mixin({
data() {
return {
// Distribute runtime configs into every Vue component
BRAND: BRAND,
PRINTER_IP: json.printerIP
}
},
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With