Umi: 开启dynamicImport: true,不写 namespace 的 model 在 build 之后被注册为 undefined

Created on 20 Nov 2018  ·  16Comments  ·  Source: umijs/umi

我写的目录结构是:

├── pages
│   └── martin
│       ├── index.js
│       └── models
│           └── martin.js

model 里没有写 namespace,然后在 index.js 里调用 export default connect(({ martin }) => ({ martin }))(Page);

yarn start 的时候可以看到正常的效果,但在 yarn build 后得到的 martin 是 undefined。且调用 window.g_app._store.getState() 可以看到结果为 { ..., "undefined": [我真正的martin] }
如果在 model 里写上 namespace,则 build 后的结果正确。

文档上的介绍是:

  • 按目录约定注册 model,无需手动 app.model
  • 文件名即 namespace,可以省去 model 导出的 namespace key

所以想知道这是一个 bug,还是有哪些步骤被我遗漏了?

type(bug)

All 16 comments

https://github.com/umijs/umi-examples/tree/master/with-dva
这个项目试了一下,没有这个问题

可以给一个demo

https://github.com/masquevil/umi-demo
umi 的版本是 2.2.7

试了一下 with-dva,因为 list/models/a.js 里面是有 namespace 的,如果去掉 namespace,则也会在 umi build 时变成 undefined,umi dev 时正常。(加了dynamicImport: true

@masquevil 一样的操作,还是可以的。你不可以的demo,发给我看看?

@xiaohuoni

~/code/umi-examples/with-dva(master)$ git diff
diff --git a/with-dva/.umirc.js b/with-dva/.umirc.js
@@ -4,6 +4,7 @@ export default {
     ['umi-plugin-react', {
       antd: true,
       dva: true,
+      dynamicImport: true,
       routes: {
         exclude: [
           /model\.(j|t)sx?$/,
diff --git a/with-dva/src/pages/list/models/a.js b/with-dva/src/pages/list/models/a.js
@@ -1,5 +1,4 @@
 export default {
-  namespace: 'a',
   state: 'a',
   reducers: {},
 };
~/code/umi-examples/with-dva(master)$ 

修改这两行,然后执行 umi buildserve dist,在浏览器里访问,点击 “go to list” 会看到:

  1. 最后一行的 test: a | b 变成了 test: | b
  2. console 里看 window.g_app._store.getState().undefined 会发现结果是 "a"

我试试,我刚刚是设置了,最外层的 disableDynamicImport: true,

是有这个问题,开启了动态加载之后,页面级别的 model 没有默认加上文件名作为 namespace

另外开发阶段 model 的动态加载是关闭的,所以 dev 是复现不了,感觉 dev 的情况也应该打开动态加载。

什么时候能发新版本解决这个问题 = =,快准备上生产了

我也是这个问题,搞了半天,才明白

急用可以上 umi@beta

@sorrycc 那个beta? 我试了beta.6也不行

dynamicImport: false 或者不设置可解决。

umi 2.3.2 遇到同样问题,使用 ant design pro

umi-plugin-react 插件配置了

dynamicImport: {
    loadingComponent: './components/PageLoading/index',
},

页面级 model 不写 namespace 会出现这个问题,

手动指定 namespace 可解决。

不设置 dynamicImport 即可,build 出来就是可以的

umi 2.3.2 遇到同样问题,使用 ant design pro

umi-plugin-react 插件配置了

dynamicImport: {
    loadingComponent: './components/PageLoading/index',
},

页面级 model 不写 namespace 会出现这个问题,

手动指定 namespace 可解决。

请问下这个问题现在解决了吗? 我这里发现还是需要手动指定 namespace

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhanchengkun picture zhanchengkun  ·  3Comments

nguyenhuutinh picture nguyenhuutinh  ·  3Comments

liuxinqiong picture liuxinqiong  ·  3Comments

ddzy picture ddzy  ·  3Comments

sorrycc picture sorrycc  ·  4Comments