{"id":1363,"date":"2023-10-31T22:04:55","date_gmt":"2023-10-31T12:04:55","guid":{"rendered":"https:\/\/moneystock.net\/wp_e\/?p=1363"},"modified":"2023-10-31T22:13:09","modified_gmt":"2023-10-31T12:13:09","slug":"how-to-use-static-config-file-in-vue-js","status":"publish","type":"post","link":"https:\/\/moneystock.net\/wp_e\/2023\/10\/31\/how-to-use-static-config-file-in-vue-js\/","title":{"rendered":"How to use static config file in Vue JS"},"content":{"rendered":"<p><strong>Why use a static config file in Vue<\/strong><\/p>\n<p>If the config is inside Vue, the value in the config needs to be defined before the Vue compile. This means it&#8217;s not possible to change the config after the Vue site is deployed to a web server. To flexibly update config values, it can be helpful to have a static config file outside of Vue components.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to set up a static config file in Vue<\/strong><\/p>\n<p>1.Create config.json file in the public folder<\/p>\n<div>\n<pre>{\r\n  \u00a0 \"title\": \"Static Config!\"\r\n}<\/pre>\n<\/div>\n<p>2. Load config.json file before mounting the Vue app at main.js. The config value can be added to<\/p>\n<div>\n<div>app.config.globalProperties.YourVariableName<\/div>\n<\/div>\n<div>\n<div>\n<pre>import '.\/assets\/main.css'\r\n\r\nimport { createApp } from 'vue'\r\n\r\nimport App from '.\/App.vue'\r\n\r\nconst app = createApp(App);\r\n\r\nfetch('\/config.json?' + Date.now())\r\n\r\n\u00a0 \u00a0 .then(response =&gt; response.json())\r\n\r\n\u00a0 \u00a0 .then(config =&gt; {\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 app.config.globalProperties.pageTitle = config.title;\r\n\r\n\u00a0 \u00a0 \u00a0 \u00a0 app.mount('#app');\r\n\r\n\u00a0 \u00a0 });<\/pre>\n<\/div>\n<\/div>\n<p>3. Read YourVariableName globally in Vue templates or scripts.<\/p>\n<div>\n<pre>&lt;script&gt;\r\n\r\nexport default {\r\n\r\n\u00a0 data() {\r\n\r\n\u00a0 \u00a0 return {\r\n\r\n\u00a0 \u00a0 \u00a0 pageTitle: this.pageTitle,\r\n\r\n\u00a0 \u00a0 }\r\n\r\n\u00a0 }\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;template&gt;\r\n\r\n\u00a0 &lt;div class=\"greetings\"&gt;\r\n\r\n\u00a0 \u00a0 &lt;h2&gt;Config called in template: {{ this.pageTitle }}&lt;\/h2&gt;\r\n\r\n\u00a0 \u00a0 &lt;br&gt;\r\n\r\n\u00a0 \u00a0 &lt;h2&gt;Config called in script: {{ pageTitle }}&lt;\/h2&gt;\r\n\r\n\u00a0 &lt;\/div&gt;\r\n\r\n&lt;\/template&gt;<\/pre>\n<\/div>\n<div><\/div>\n<div>Sample Code: <a href=\"https:\/\/github.com\/rocker8942\/vue-static-config\">rocker8942\/vue-static-config (github.com)<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Why use a static config file in Vue If the config is inside Vue, the value in the config needs to be defined before the Vue compile. This means it&#8217;s not possible to change the config after the Vue site is deployed to a web server. To flexibly update config values, it can be helpful&hellip; <a class=\"more-link\" href=\"https:\/\/moneystock.net\/wp_e\/2023\/10\/31\/how-to-use-static-config-file-in-vue-js\/\">Continue reading <span class=\"screen-reader-text\">How to use static config file in Vue JS<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[666,79],"tags":[],"class_list":["post-1363","post","type-post","status-publish","format-standard","hentry","category-vue","category-web-development","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/comments?post=1363"}],"version-history":[{"count":3,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1363\/revisions"}],"predecessor-version":[{"id":1366,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/1363\/revisions\/1366"}],"wp:attachment":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/media?parent=1363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/categories?post=1363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/tags?post=1363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}