Vue3 + ts 国际化更改语言配置

mian.ts 中安装i18n依赖

import { createLanguage } from "vue-i18n";

const i18n = createLanguage ({
    locale: "zh-CN", //默认显示的语言
    messages,
});

createApp(App)
	.use(i18n)

在页面中使用

<template>
	<div>
		<div @click="changeLang">点我修改语言,当前语言:{{language}}</div>
	</div>
</template>
<script lang="ts">
import { defineComponent, getCurrentInstance, ref } from "vue";

export default defineComponent({
    setup() {
    	let language = ref('中文');
        const { proxy } = getCurrentInstance();
        const changeLanguage  = () => {
        	//zh-CN中文 en-US 英文 
        	proxy.$i18n.locale=proxy.$i18n.locale==="en-US"?"zh-CN":"en-US";
        	language.value = language.value === '中文'?'英文':'中文';
		}
        return { changeLanguage, language };
    },
});
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3和Typescript的使用方式主要有以下几种: 1. 使用Vue CLI创建项目时选择TypeScript作为语言 在创建Vue项目时,可以选择使用TypeScript作为项目的语言。这样可以直接在项目中使用TypeScript,并且会自动生成一些TypeScript相关的配置文件和代码结构。具体步骤如下: ``` vue create my-project ``` 在选择项目配置时选择"Manually select features",然后选择TypeScript即可。 2. 在已有的Vue项目中添加TypeScript支持 如果已经有一个Vue项目,也可以在其中添加TypeScript支持。具体步骤如下: - 安装TypeScript和相关依赖 ``` npm install --save-dev typescript ts-loader @vue/cli-plugin-typescript ``` - 配置TypeScript 在项目根目录下创建一个`tsconfig.json`文件,并添加以下内容: ```json { "compilerOptions": { "target": "esnext", "module": "esnext", "strict": true, "jsx": "preserve", "esModuleInterop": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "sourceMap": true, "baseUrl": ".", "paths": { "@/*": ["src/*"] }, "lib": ["esnext", "dom", "dom.iterable", "scripthost"] }, "include": ["src/**/*.ts", "src/**/*.tsx", "tests/**/*.ts", "tests/**/*.tsx"], "exclude": ["node_modules"] } ``` - 配置Vue插件 在`vue.config.js`文件中添加以下内容: ```js module.exports = { pluginOptions: { 'style-resources-loader': { preProcessor: 'scss', patterns: [] } }, configureWebpack: { resolve: { extensions: ['.ts', '.tsx', '.js', '.vue', '.json'] }, module: { rules: [ { test: /\.tsx?$/, loader: 'ts-loader', exclude: /node_modules/, options: { appendTsSuffixTo: [/\.vue$/] } } ] } } } ``` - 修改Vue组件文件后缀名 在Vue组件文件中,将`.vue`后缀名改为`.vue.ts`,这样可以直接在Vue组件中使用TypeScript。 3. 在Vue3中使用TypeScript 在Vue3中,可以使用以下方式来使用TypeScript: - 在Vue组件中使用TypeScript 在Vue3中,可以直接在Vue组件中使用TypeScript。具体步骤如下: - 在`setup()`函数中定义变量的类型 - 在`defineComponent()`函数中使用`defineProps()`和`defineEmits()`定义属性和事件的类型 - 在`template`中使用TypeScript语法 示例代码: ```html <template> <div>{{ count }}</div> </template> <script lang="ts"> import { defineComponent, defineProps, defineEmits } from 'vue' interface Props { msg: string count: number } export default defineComponent({ props: defineProps<Props>(), emits: defineEmits(['update:count']), setup(props) { const count = ref(props.count) return { count } } }) </script> ``` - 在Vue3中使用Composition API Vue3中的Composition API也支持使用TypeScript。具体步骤如下: - 使用`ref()`、`reactive()`等函数定义变量的类型 - 在`defineComponent()`函数中使用`defineProps()`和`defineEmits()`定义属性和事件的类型 示例代码: ```html <template> <div>{{ count }}</div> </template> <script lang="ts"> import { defineComponent, defineProps, defineEmits, ref } from 'vue' interface Props { msg: string count: number } export default defineComponent({ props: defineProps<Props>(), emits: defineEmits(['update:count']), setup(props) { const count = ref(props.count) return { count } } }) </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值