【问题标题】:Language as param in Vue RouterVue Router 中作为参数的语言
【发布时间】:2022-01-14 14:31:24
【问题描述】:

我想在我的路径中有一个语言参数。比如路径前有en,打印en

如果我输入http://localhost:8080/en,它会给我en,但如果我输入http://localhost:8080/en/aaaa,它会返回我undefined 我怎样才能得到en

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

【问题讨论】:

    标签: javascript vue.js vue-router


    【解决方案1】:

    假设您计划为您的应用添加不同的语言支持,我建议您改用i18n,因为它会抽象出诸如为不同语言附加参数之类的东西。

    对于您为何收到undefined 的具体问题,这可能仅仅是因为/aaa 路线没有查看页面或路线。所以 Vue 只是对你想去的地方感到困惑。

    【讨论】:

      【解决方案2】:

      可以使用纯javascript获取当前url,通过split获取参数。

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

      【讨论】:

        猜你喜欢
        • 2021-05-17
        • 2019-12-19
        • 1970-01-01
        • 2019-01-24
        • 2017-04-21
        • 2020-05-07
        • 2021-08-07
        • 2019-07-29
        相关资源
        最近更新 更多