dcloudio / dcloudio/uni-app

i18n 国际化项目打包成微信小程序分包 $i18n undefined问题

Open
#3,968 4 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
41.6k
Forks
3.7k
Avg merge
7h 6m
Merged PRs (30d)
6

Description

**问题描述**
uniapp项目中引入了i18n项目
打包成微信小程序主体运行是正常的
但是如果打包成微信小程序分包的形式,让主包引入。进入分包后,会报错如下:
TypeError: Cannot read property '_t' of undefined
进入源码后发现,实际是this.$i18n返回undefined

**系统信息:**
- 发行平台: 微信小程序-分包
- sdk版本: [v2.0.1-alpha-36820221027001](https://github.com/dcloudio/uni-app/releases/tag/v2.0.1-alpha-36820221027001)

通过编写测试代码发现:
1.在分包情况下,main.js中输出的this.$i18n是有值的
2.但在具体页面上输出this.$i18n是没有值的
3.除了分包场景下,其他场景(H5,微信小程序主包)下均正常

debug了下,分包场景下,源码中这段里app.$vm是Unidefined,导致挂载失败
小程序场景是ok的

File: [src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js#L142](https://github.com/dcloudio/uni-app/blob/dev/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js#L142)

```
if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
const app = getApp();
if (app.$vm && app.$vm.$i18n) {
this._i18n = app.$vm.$i18n;
}
}
```
似乎可以通过以下fix去解决,需要维护同学看下
```
if (this.mpType === 'page' && typeof getApp === 'function') { // hack vue-i18n
const app = getApp()
if (app.$vm && app.$vm.$i18n) {
this._i18n = app.$vm.$i18n
} else {
this._i18n = vm.$options.i18n;
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.