fix: locale api is undefined before react app mounted #1810
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
类似
formatMessage
这样的 API 在react-intl
在用法是需要通过 context 然后在组件中通过this.formatMessage
来使用。在umi-plugin-locale
中为了更方便,通过封装了一层之后使得用户可以直接调用 formatMessage 来使用,但是有一个问题是必须要 react app 挂载之后 formatMessage 才能实际可用。也就是说如果直接在模块初始化部分使用的话是无效了,最初是有一个空的方法做容错处理,后来在 #1730 这个 PR 中去掉了容错的方法,会导致如果再模块初始化阶段拿到的方法是 undefined。
在这个 PR 中重新加上了容错的方法,并且在模块初始化调用的时候给出警告提示。另外去掉了 #1730 这个 PR 中添加的 locale 这些非方法的内容,如果这些内容需要用到的话建议还是类似提供
getLocale
这样的方法来调用。