Egg + umi + dva 发布后报 Uncaught (in promise) TypeError 开发的时候没有问题!为什么?
发布于 5 年前 作者 thomas0836 4003 次浏览 来自 问答

复现仓库 下载后

$ npm i 
$ npm run dev

分别在 http://localhost:8000/http://127.0.0.1:3101/ 进行访问

屏幕快照 2018-11-01 下午5.42.55.png 屏幕快照 2018-11-01 下午5.43.33.png

问题代码在 /services/base.js

import { request, config } from '@/utils'
export async function getI18n(lang) {
  let path = '/i18n'
  if (lang) {
    path = `/i18n/${lang}`
  } 
  console.log(`async function getI18n() lang = ${lang}`)
  return request(`${config.api.base}/${encodeURIComponent(path)}`)
}

/services/base.js

class App extends Component {
 state = { initDone: false };

 componentDidMount() {
   this.loadLocales();
 }

 loadLocales() {
   const currentLocale = getCurrentLocale()
   getI18n(currentLocale).then(res => {
     // init method will load CLDR locale data according to currentLocale
     intl.init({
       currentLocale,
       locales: {
         [currentLocale]: res.data
       }
     })
   }).then(() => {
     // After loading CLDR locale data, start to render
     this.setState({ initDone: true });
   });
 }

 render() {
   const { initDone } = this.state
   const { children } = this.props
   return (
     initDone &&
     <LocaleProvider locale={getLocale()}>
       { children }
     </LocaleProvider>
   );
 }
}

export default App;

getI18n(currentLocale).then(res => { 这里在8000端正常,在3101端却 res 为空,请问是什么问题?

以上项目是模仿 egg-ant-design-pro 来做的

3 回复

还不是很懂 model 的使用,我这个获取到其他服务器内的i18n.json 然后再修改 state 内的 initDone 为true 让页面加载。 尝试过使用model 来写,但是也是不行,刚接触这个还不熟。

有大神指导一下吗?

你这个 locale 是前端的 mock ,当然只有 8000 有数据了。你用最新的 https://github.com/eggjs/egg-ant-design-pro 的版本改吧~

回到顶部