9***@qq.com
9***@qq.com
  • 发布:2024-07-25 18:49
  • 更新:2024-09-26 20:10
  • 阅读:2091

【报Bug】Vue3 使用 vue-i18n 报错 You are running the esm-bundler build of vue-i18n.

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10 LTSC 2019

HBuilderX类型: 正式

HBuilderX版本号: 4.24

手机系统: Android

手机系统版本号: Android 14

手机厂商: 小米

手机机型: 红米turbo3

页面类型: vue

vue版本: vue3

打包方式: 离线

项目创建方式: HBuilderX

示例代码:

//-------------main.js---------------------
import App from './App'

// 加载语言包
import Chinese from '@/common/locales/zh.json';
import English from '@/common/locales/en.json';

let i18nConfig = {
locale: 'en', // 设置默认语言
messages: {
'cn': Chinese,
'en': English,
}
}

// #ifndef VUE3
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
i18n,
...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
import { createI18n } from 'vue-i18n'
const i18n = createI18n(i18nConfig)
export function createApp() {
const app = createSSRApp(App)
app.use(i18n)
return {
app
}
}
// #endif

//-------------en.json---------------------
{
"welcome": "Welcome",
"language": "Language"
}

//-------------zh.json---------------------
{
"welcome": "欢迎",
"language": "语言"
}

//------------mainPage.json---------------------
<template>
<view>
<view>{{ $t('welcome') }}</view>
<button @click="changeLanguage('zh')">中文</button>
<button @click="changeLanguage('en')">English</button>
</view>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const changeLanguage = (lang) => {  
    i18n.global.locale.value = lang;  
};  

</script>

操作步骤:

编译后出现报错

预期结果:

预期不会报错

实际结果:

实际会提示:You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.

bug描述:

使用VUE3模板做 vue-i18n 国际化语言,在main.js中 注入i18n后报错:

You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.

2024-07-25 18:49 负责人:无 分享
已邀请:
5***@qq.com

5***@qq.com

@xiaopao
直接找到文件 HBuilderX/plugins/uniapp-cli-vite/node_modules/vue-i18n/dist/vue-i18n.esm-bundler.js

43行变为false 或下面的 console.warn 注释即可

BFC

BFC

You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.


你好, 这个输出是 warning警告,不是异常。 我这边也正常运行了你的demo。

//main.js 文件  

export const i18n = createI18n(i18nConfig)  
.....省略  

// mainPage文件  
<script setup>  
    import { ref , reactive , onMounted} from 'vue'  
    import {i18n} from '../main.js'  
    function changeLanguage(lang){  
        console.log(lang);  
        i18n.global.locale = lang;  
    };  
</script>
  • xiaopao

    请问如何配置可以消除这个警告?

    2024-09-19 17:39

  • w***@dingtalk.com

    同问啊~!!! 这个警告在哪里配置可以消除?

    网上说在根目录下创建vite.config.js :

    import { defineConfig } from 'vite';

    import uni from '@dcloudio/vite-plugin-uni';

    import vue from '@vitejs/plugin-vue';


    export default defineConfig({

    plugins: [uni(), vue()],

    define: {

    VUE_I18N_FULL_INSTALL: JSON.stringify(true),

    VUE_I18N_LEGACY_API
    : JSON.stringify(false),

    INTLIFY_PROD_DEVTOOLS: JSON.stringify(false),

    },

    })

    但是运行后会报 At least one <template> or <script> is required in a single file component.


    警告看的太难受了!!

    2024-09-20 10:43

  • 5***@qq.com

    回复 w***@dingtalk.com: 直接找到文件 HBuilderX/plugins/uniapp-cli-vite/node_modules/vue-i18n/dist/vue-i18n.esm-bundler.js


    43行变为false 或下面的 console.warn 注释即可

    2024-09-26 20:10

要回复问题请先登录注册