选择元素以使用 nuxt-i18n 在 nuxt 中获取语言环境

yoa*_*azz 6 html-select vue.js nuxt.js

我使用 nuxt-i18n nuxt-i18n 文档链接在我的网站上获取不同的语言环境,如下所示:

<nuxt-link v-for="locale in $i18n.locales"
             v-if="locale.code !== $i18n.locale"
             :key="locale.code"
             :to="switchLocalePath(locale.code)"
             class="locales white--text"
  >{{ locale.code }}
  </nuxt-link>
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但我想将此代码转换为在选择元素中呈现:

<select v-model="selected" class="locales white--text" @change=" ??? ">
    <option disabled value="">{{ $i18n.locale }}</option>
    <option v-for="locale in $i18n.locales" :key="locale.code">{{ locale.code }}</option>
  </select>
Run Code Online (Sandbox Code Playgroud)

Locales 字符串看起来不错,但我没有找到在更改时启动 switchLocalePath 函数的解决方案。使用 nuxt (vue.js) 有没有合适的方法来做到这一点?

Enr*_*cio 8

在这里,下拉列表和 onChange 方法:

 <select v-model="selectedValue" @change="onChange(selectedValue)">
        <option disabled value>Please select one</option>
        <option
          v-for="(locale, index) in $i18n.locales"
          :key="index"
          :value="locale.code"
        >{{locale.name}}</option>
      </select>
Run Code Online (Sandbox Code Playgroud)
 methods: {
    onChange(event) {
      this.$router.replace(this.switchLocalePath(event));
    }
  }
Run Code Online (Sandbox Code Playgroud)

如果你想检查工作,我已经构建了一个在这里工作的 CodeSandox Nuxt:

https://codesandbox.io/embed/codesandbox-nuxt-1bhug?fontsize=14&hidenavigation=1&theme=dark