国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

使用vue 國際化i18n 實現多實現語言切換功能

2024-05-06 15:28:50
字體:
來源:轉載
供稿:網友

安裝

npm install vue-i18n

新建一個文件夾 i18n ,內新建 en.js zh.js index.js 三個文件

準備翻譯信息

en.js

export default { home: { helloworld: "hello workd !" }};

zh.js

export default { home: { helloworld: "你好世界" }};

index.js

創建Vue-i18n實例

import Vue from "vue";import VueI18n from "vue-i18n";import enLocale from "./en";import zhLocale from "./zh";Vue.use(VueI18n);const i18n = new VueI18n({ locale: localStorage.lang || "zh", messages: { en: {  ...enLocale }, zh: {  ...zhLocale } }});export default i18n;

i18n 掛載到 vue 根實例

main.js

import Vue from "vue";import App from "./App.vue";import router from "./router";import store from "./store";import i18n from "./assets/i18n/index";Vue.config.productionTip = false;Vue.prototype.$i18n = i18n;new Vue({ router, store, i18n, render: h => h(App)}).$mount("#app");

簡單的使用

about.vue

<template> <div class="about"> <h1>{{ $t("home.helloworld") }}</h1> <button @click="changeLang()">切換英文</button> <p>{{hi}}</p> </div></template><script>export default { data: function() { return {}; }, computed: { hi() {  return this.$t("home.helloworld"); } }, methods: { changeLang() {  this.$i18n.locale = "en"; } }};</script>

注意:

比如說上面的hi 你要通過這種形式來寫的時候,不能放在data 里面,因為當語言切換的時候 他是不會變的 ,要寫在computed內

總結

以上所述是小編給大家介紹的使用vue 國際化i18n 多實現語言切換功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對錯新站長站網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 大新县| 闻喜县| 凤城市| 德阳市| 婺源县| 丘北县| 余庆县| 泸州市| 五原县| 镇坪县| 涟水县| 石门县| 城固县| 策勒县| 墨玉县| 安仁县| 突泉县| 金沙县| 江源县| 哈密市| 佛山市| 洛南县| 甘孜县| 五莲县| 顺义区| 娄底市| 日喀则市| 米易县| 改则县| 奉化市| 深圳市| 北碚区| 佛冈县| 兴安县| 扬中市| 信宜市| 宾阳县| 东莞市| 罗甸县| 宜兰市| 锦州市|