-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
icejs 中 store 加载时机过前问题 #3630
Comments
问题以 basic-store 为demo,进行代码部分修改,以呈现问题: 在 + import { IRootDispatch, APP_MODE } from 'ice';
+ console.log('store: APP_MODE === ', APP_MODE); 在 + import { APP_MODE } from 'ice';
+ console.log('app APP_MODE ===' APP_MODE); 我们在项目的根目录下执行 可以看到有两个问题:
在 export const APP_MODE = (global as any).__app_mode__ || process.env.APP_MODE; 不妨试下在 + console.log(process.env.APP_MODE); 你会发现,这里打印出来的值为 分析问题一我们都知道,ES Module 是静态编译的。import 的命令会在代码运行前,被 JS 引擎静态编译分析,具有提升作用,会优先于模块中其他内容执行的。比如下面的一个例子: // a.js
console.log('a.js')
import { foo } from './b';
// b.js
export let foo = 1;
console.log('b.js');
// 执行结果:
// b.js
// a.js 框架的入口为应用的 // src/app.ts
import { createApp, IAppConfig, APP_MODE } from 'ice';
console.log('app APP_MODE ===' APP_MODE);
const appConfig = {};
createApp(appConfig); 在 // 此处省略代码
import store from './store/index';
export { store }; 在 问题二在解第二个问题时,先看下一般情况使用 ES Module: // a.js
console.log('a');
import { foo } from './b.js';
console.log(foo);
// b.js
const foo = 2;
console.log('b');
export { foo };
// 输出内容
// b
// a
// 2 在执行 下面我们再看一下 ES Module 循环依赖的情况: // a.js
import { bar } from './b';
console.log('a.js');
console.log(bar);
export const foo = 'foo';
// b.js
import { foo } from './a';
console.log('b.js');
console.log(foo);
export const bar = 'bar';
// 执行 a.js 后,输出结果为:
// b.js
// undefined
// a.js
// bar 由于在执行 解决办法是导出 getFoo 函数,因为函数有提升作用: // a.js
import { bar } from './b';
console.log('a.js');
console.log(bar);
export const function getFoo() { return 'foo' };
// b.js
import { foo, getFoo } from './a';
console.log('b.js');
console.log(getFoo());
export const bar = 'bar';
// 执行 a.js 后,输出结果为:
// b.js
// foo
// a.js
// bar 回到问题二,我们现在的模块依赖关系是: src/app.ts --> .ice/index.ts --> .ice/store/index.ts --> src/models/counter.ts --> ./ice/index.ts 这里面形成了模块循环依赖,因此, 总结如果不使用自定义创建 store 的方式,由框架自动创建 store,是会有模块循环依赖的问题。如果使用自定义创建 store 实例方式使用状态管理,则没有上面的问题。模块依赖关系如下: |
|
目测已有的实现是存在问题的。可以通过以下三种方式获取到正确的
结论:3 |
发布 [email protected] 已修复 |
发布 [email protected] 版本已解决 |
store 加载时机过前,导致在 model 中最初拿到的 APP_MODE 的值是 undefined。应该是在 config > createApp > other(model/router/...)
The text was updated successfully, but these errors were encountered: