使用Nuxt.js和Vue-i18n重定向到同一页面但切换语言URL

27 篇文章 3 订阅
3 篇文章 0 订阅

使用Nuxt.js和Vue-i18n重定向到同一页面但切换语言URL

公司最近提出一个需求,就是当用户切换语言的时候,在路由中需要将其选中的语言加入到路由中
例如网址:

localhost/about

应该通过该方法(通过按特定按钮)重定向到:

localhost/bg/about

Nuxt文档中所建议的,用于使用Vue-i18n进行本地化https://nuxtjs.org/examples/i18n/

在 Nuxt 官网中也给出了国际化的例子,但是并不满足公司的这个需求,大家有兴趣可以去看下

Nuxt 官网 国际化的例子

components文件夹下面新建 LangSelect.vue文件

<template>
  <el-dropdown trigger="click" class="international" @command="handleSetLanguage">
    <div>
      <i class="el-icon-share">{{$t('home.changelang')}}</i>
    </div>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item :disabled="language==='zh'" command="zh">中文</el-dropdown-item>
      <el-dropdown-item :disabled="language==='en'" command="en">English</el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
export default {
  computed: {
    language() {
      return this.$store.state.locale;
    }
  },
  methods: {
    handleSetLanguage(language) {
      this.$i18n.locale = language;
      console.log(this.$i18n.locale);
      this.$store.commit("SET_LANG", language);
    //   console.log(this.$store.state.locale, "locale");
      var beforePath = this.$nuxt.$router.history.current.path;
      // -- Removing the previous locale from the url
      var changePath = this.$store.state.locale
      var result = "";
      result = beforePath.replace("/en", "");
      result = result.replace("/zh", "");
    this.$nuxt.$router.replace({ path: "/" + changePath + result });
      this.$message({
        message: "Switch Language Success",
        type: "success"
      });
    }
  }

};
</script>

middleware文件中新建i18n.js

export default function ({ isHMR, app, store, route, params, error, redirect }) {
    const defaultLocale = app.i18n.fallbackLocale
    // If middleware is called from hot module replacement, ignore it
    //如果从热模块替换调用中间件,请忽略它
    if (isHMR) { return }
    // Get locale from params
    const locale = params.lang || defaultLocale
    if (!store.state.locales.includes(locale)) {
      return error({ message: 'This page could not be found.', statusCode: 404 })
    }
    // Set locale
    store.commit('SET_LANG', locale)
    app.i18n.locale = store.state.locale

    if(route.fullPath == '/') {
      return redirect('/' + defaultLocale + '' + route.fullPath)
    }
  }
  

其他的配置都可以在 上面给出的官网链接中找到,这里就不在重复了

要在Nuxt.js使用vue-seamless-scroll,首先需要在plugins目录下创建一个vue-seamless-scroll.js文件,并在文件中导入Vue和SeamlessScroll组件,然后使用Vue.use(SeamlessScroll)进行注册。 接下来,在nuxt.config.js文件中的plugins配置项中添加如下内容: ```javascript plugins: [ '@/plugins/element-ui', '@/plugins/axios', { src: '@/plugins/vue-seamless-scroll', ssr: false }, ] ``` 这样就可以将vue-seamless-scroll插件引入到Nuxt.js项目中了。 然后,在需要使用滚动组件的文件中,使用`<vue-seamless-scroll>`标签,并传入相应的数据和类名等参数。例如: ```html <vue-seamless-scroll :data="runningData" :class-option="scrollOption" class="scroll-container"> <div class="flex-row" v-for="item in runningData" :key="item.id"> <span class="row-1 row-nomal">{{ item.mbShowName }}</span> <span class="row-2 row-nomal">{{ item.mbShowVal }}</span> <span class="row-3 row-nomal">{{ item.updateTime | dateFilter }}</span> </div> </vue-seamless-scroll> ``` 这样就可以在Nuxt.js使用vue-seamless-scroll组件了。 如果你在公司的基于Nuxt.js的项目中使用滚动组件后刷新页面出现"window is not defined"的错误,可能是因为滚动组件依赖于浏览器环境,而在服务器端渲染时无法访问到window对象。解决这个问题可以将ssr选项设置为false,如前面的配置所示。这样就可以避免在服务器端渲染时出现该错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [在 Nuxt使用滚动组件 vue-seamless-scroll](https://blog.csdn.net/weixin_44769310/article/details/116144924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值