每当加载新脚本时,Angular 13 Window.ng。
更新:
我注意到,每当我们尝试加载组件/模块时,它都会寻找window.ng.ɵcompilerFacade,并期望它采用V13投诉格式。但是在两者之间,如果我加载任何Web组件(使用元素构建),那么它将替换窗口。我正在使用V13用于外壳应用程序,并在加载V11小部件(内置角元素)的内部。不确定如何进一步进行。我的应用程序将具有不同版本的混合和匹配。
原始:
我有一个路由器守卫,在激活组件之前加载一些脚本。自Angular 7到11以来,这一直很好地工作。我最近升级到13个,面临一些奇怪的问题。
canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
const result: Promise<boolean> = new Promise((resolve) => {
if (!myElementExists) {
this.loadApp(appObj).then(() => {
// success(script loaded)
resolve(true);
}, () => {
// error - file not found / loading error
this.router.navigate(['/error']);
resolve(true);
});
} else {
// success(script already loaded)
resolve(true);
}
});
return result;
}
private async loadApp(url): Promise<void> {
await Utils.loadScript(`../${url}`);
}
这基本上是在移至组件之前加载一些外部脚本。现在,在Angular 13的情况下,发生的事情是 - 我会遇到以下错误:
core.mjs:6494 ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'Directive')
TypeError: Cannot read properties of undefined (reading 'Directive')
at XXXComponent.get (core.mjs:24383:1)
at getFactoryDef (core.mjs:1406:1)
at configureViewWithDirective (core.mjs:10484:1)
at instantiateRootComponent (core.mjs:10191:1)
at createRootComponent (core.mjs:12297:1)
at ComponentFactory.create (core.mjs:21654:1)
at ViewContainerRef.createComponent (core.mjs:22918:1)
at RouterOutlet.activateWith (router.mjs:2521:40)
at ActivateRoutes.activateRoutes (router.mjs:2161:40)
at router.mjs:2111:18
at resolvePromise (zone.js:1211:1)
at resolvePromise (zone.js:1165:1)
at zone.js:1278:1
at _ZoneDelegate.invokeTask (zone.js:406:1)
at Object.onInvokeTask (core.mjs:25595:1)
at _ZoneDelegate.invokeTask (zone.js:405:1)
at Zone.runTask (zone.js:178:1)
at drainMicroTaskQueue (zone.js:585:1)
at ZoneTask.invokeTask [as invoke] (zone.js:491:1)
at invokeTask (zone.js:1648:1)
当我进一步检查时,在v13的core.mjs中,在AddDirectiveFactoryDef()下,有一些更改可以从立面上获取编译器。 compiler.factorytarget作为未定义的返回。
const compiler = getCompilerFacade({ usage: 0 /* Decorator */, kind: 'directive', type });
有趣的是,在Canactivate()中,如果我跳过LoadScript逻辑并返回Promise Oriperaway,则其工作方式也同样好。有时,在同一getCompilerFacade中,它也恰好发生在“ ngmodule”中。我猜这与该区域有关。
我该如何从这里开始?
Update:
I noticed that whenever we try to load a component/module it looks for window.ng.ɵcompilerFacade and expects it to be in v13 complaint format. But in between, if I load any web-component(built using elements) then it is replacing the window.ng.ɵcompilerFacade which results in routing errors. I am using V13 for a shell application and inside that loading a V11 widget(built in angular elements). Not sure how to proceed further. My application will have mix and match of different versions.
Original:
I have a routerguard that loads some scripts before activating the component. This has been working fine since Angular 7 to 11. I recently upgraded to 13 and facing some weird issues.
canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
const result: Promise<boolean> = new Promise((resolve) => {
if (!myElementExists) {
this.loadApp(appObj).then(() => {
// success(script loaded)
resolve(true);
}, () => {
// error - file not found / loading error
this.router.navigate(['/error']);
resolve(true);
});
} else {
// success(script already loaded)
resolve(true);
}
});
return result;
}
private async loadApp(url): Promise<void> {
await Utils.loadScript(`../${url}`);
}
This basically loads some external scripts before moving to the component. Now, with Angular 13 what happens is - I am getting the following error:
core.mjs:6494 ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'Directive')
TypeError: Cannot read properties of undefined (reading 'Directive')
at XXXComponent.get (core.mjs:24383:1)
at getFactoryDef (core.mjs:1406:1)
at configureViewWithDirective (core.mjs:10484:1)
at instantiateRootComponent (core.mjs:10191:1)
at createRootComponent (core.mjs:12297:1)
at ComponentFactory.create (core.mjs:21654:1)
at ViewContainerRef.createComponent (core.mjs:22918:1)
at RouterOutlet.activateWith (router.mjs:2521:40)
at ActivateRoutes.activateRoutes (router.mjs:2161:40)
at router.mjs:2111:18
at resolvePromise (zone.js:1211:1)
at resolvePromise (zone.js:1165:1)
at zone.js:1278:1
at _ZoneDelegate.invokeTask (zone.js:406:1)
at Object.onInvokeTask (core.mjs:25595:1)
at _ZoneDelegate.invokeTask (zone.js:405:1)
at Zone.runTask (zone.js:178:1)
at drainMicroTaskQueue (zone.js:585:1)
at ZoneTask.invokeTask [as invoke] (zone.js:491:1)
at invokeTask (zone.js:1648:1)
When I checked further, in core.mjs of V13, under addDirectiveFactoryDef() there are some changes to get compiler from facade. compiler.factoryTarget is returned as undefined.
const compiler = getCompilerFacade({ usage: 0 /* Decorator */, kind: 'directive', type });
Interesting thing is, in the canactivate() if I skip the loadscript logic and return a promise rightaway, it's working just as fine. Sometimes it happens to 'NgModule' as well, in the same getCompilerFacade. I am guessing this is something to do with the Zone.
How do I proceed from here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是打开AOT。
就我而言,我的shell应用程序(v13)和小部件应用程序(v11)均已编译。因此,在运行时,Angular执行了使用窗口命名空间的编译。有2个不同版本的Angular访问相同的名称空间,它破裂了。我现在打开了我的shell应用程序的AOT,它清除了window.ng的依赖性。
现在情况正常。
Solution is to turn on AOT.
In my case, both my shell app(V13) and widget app(V11) are JIT compiled. So at runtime, angular does that compiling for which it uses window.ng namespace. With 2 different versions of Angular accessing the same namespace, it broke. I now turned on aot for my shell app, it cleared it's dependency with window.ng.
Things are working fine now.