简体   繁体   中英

language as param in vue router

I want to have my language in path as param.for example if there is "en" before the path, print "en". if I enter "http://localhost:8080/en" it will give me "en" but if I enter "http://localhost:8080/en/aaaa" it will return me "undefined" how can I get "en" anyway?

const routes = [
  {
    path: "/:lang?",
    children: [
      {
        path: home,
        component: home
      }
    ]
  }
]
router.beforeEach((to, _, next) => {
  console.log(to.params.lang);
}

Under the assumption that you are planning on adding different language support for your app, I would recommend instead using i18n because it will abstract away things like appending params for different languages.

To your specific question on why you are getting undefined , that could simply be because there is no view page or route for the /aaa route. So Vue is just confused on where you want to go.

You can use pure javascript to get the current url and get the parameters through a split.

let myListParams = window.location.href.split('/');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM