Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PublicRuntimeConfig variables inside nuxt.config.js?

Tags:

vue.js

nuxt.js

In my nuxt.config.js file I create some variables which are needed in multiple parts of my application. My main objective is this:

In this example I use the about.vue page. The name and url of a link are defined in the nuxt.config file. An object is created that stores both custom name as url of about. I want to replace the standard URL of homepage.com/about to homepage.com/custom-about. I should get custom-about from the $config.ABOUT.URL. I tried both $config.ABOUT.URL as this.ABOUT.URL but both won't work. How can I pass the variable created in publicRuntimeConfig to router inside the same nuxt.config.js file?

.env

ABOUT_NAME='Custom about'
ABOUT_URL='custom-about'

nuxt.config.js

publicRuntimeConfig: {
  PAGE_TITLE: process.env.PAGE_TITLE || "Company",

  ABOUT: {
    NAME: process.env.ABOUT_NAME || "About us",
    URL: process.env.ABOUT_URL || "/about"
  },
}
...
router: {
  extendRoutes: routesIn => {
    routesIn.forEach(r => {
      if (r.path.includes("/about")) {
      r.path = r.path.replace("/about","/" + this.ABOUT.URL);
    }
  }
},
like image 710
Jeffrey van Zwam Avatar asked Dec 06 '25 18:12

Jeffrey van Zwam


1 Answers

In my comment, there is a part with If you need this variable for a Nuxt module, but yeah, I'll edit it to basically add ... or anything at all in your nuxt.config.js file I guess.

Here, you should use this

r.path = r.path.replace("/about", "/", process.env.ABOUT_URL)
like image 162
kissu Avatar answered Dec 08 '25 23:12

kissu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!