vue3+pinia+ts全局化语言配置

本文介绍了如何在Vue应用中安装并配置vue-i18n实现多语言支持,包括创建语言文件、设置默认语言、在根组件中绑定和切换语言功能。
摘要由CSDN通过智能技术生成

1、安装

npminstall vue-i18n --S

2、创建实例

在src文件夹下创建一个language文件夹,用于创建i18n实例和语言包设置

其中,modules下存储不同的语言包,这里以中英文为例

en.ts:

export default {
  homeTitle: "home",
  aboutTittle: "about"
};

zh.ts

export default {
  homeTitle: "首页",
  aboutTittle: "关于"
};

其中,index文件下为:

import { createI18n } from "node_modules/vue-i18n/dist/vue-i18n";

import zh from "./modules/zh";
import en from "./modules/en";

const i18n = createI18n({
  locale: "zh", // 设置当前语言类型
  legacy: false, // 如果要支持compositionApi,这项必须设置为false
  messages: {
    zh,
    en
  }
})
export default i18n;

3、在main文件里引入

4、在根组件里绑定和配置

<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
import { useI18n } from "vue-i18n";

import {onMounted} from "vue"

const i18n  = useI18n();
onMounted(() => {
  i18n.locale.value = "zh"
});
const changeZh = () => {
  i18n.locale.value = "zh"
}
const changeEn = () => {
  i18n.locale.value = "en"
}
</script>

使用:

<RouterLink to="/">{{$t("homeTitle")}}</RouterLink>
<RouterLink to="/about">{{$t("aboutTitle")}}</RouterLink>
<el-button @click="changeZh">切换中文</el-button>
<el-button @click="changeEn">切换英文</el-button>

 

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中使用TypeScript结合Pinia来实现全局loading的方式如下: 首先,我们需要安装vue-router、pinia和axios,可以使用以下命令进行安装: ``` npm install vue-router@next pinia axios ``` 接下来我们创建一个store模块来管理全局的loading状态。在src/store目录下创建一个名为loading.ts的文件,代码如下: ```typescript import { store } from 'pinia'; export const useLoadingStore = store('loading'); export const loadingStore = useLoadingStore({ state: () => ({ isLoading: false, }), actions: { setLoading(loading: boolean) { this.isLoading = loading; }, }, }); ``` 然后在src/main.ts文件中注册pinia和创建一个全局的loading插件,代码如下: ```typescript import { createApp } from 'vue'; import { createPinia } from 'pinia'; import App from './App.vue'; import router from './router'; import axios from 'axios'; import { loadingStore } from './store/loading'; const app = createApp(App); const pinia = createPinia(); app.use(pinia); // 创建全局loading插件 app.config.globalProperties.$loading = { show() { loadingStore.setLoading(true); }, hide() { loadingStore.setLoading(false); }, }; // axios拦截器 axios.interceptors.request.use(function (config) { loadingStore.setLoading(true); return config; }, function (error) { return Promise.reject(error); }); axios.interceptors.response.use(function (response) { loadingStore.setLoading(false); return response; }, function (error) { loadingStore.setLoading(false); return Promise.reject(error); }); app.use(router); app.mount('#app'); ``` 最后,在需要使用loading的地方,可以通过以下方式来调用全局的loading状态: ```typescript import { defineComponent } from 'vue'; import { loadingStore } from './store/loading'; export default defineComponent({ methods: { fetchData() { loadingStore.setLoading(true); // 发起异步请求 // ... loadingStore.setLoading(false); }, }, }); ``` 以上就是使用Vue3和TypeScript结合Pinia实现全局loading的方法。我们首先在store模块中定义了一个loading状态,并提供了相应的方法来控制loading的显示和隐藏。然后在main.ts中创建了全局的loading插件,并通过axios的拦截器来控制loading的显示和隐藏。最后,在需要使用loading的地方调用相应的方法即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值