我正在使用Loopback SDK生成器来生成NgRx存储/效果模块。我自定义了库以支持所需的主要更改。
然后,我编写了自定义效果来监听GuardNotFound动作并重定向到404页面,但在构建应用程序时无法正常工作。
这是我的 custom-effects.ts文件
@Injectable()
export class GuardFailEffect {
constructor(private action$: Actions, private router: Router) {
}
@Effect({ dispatch: false })
redirect$ = this.action$.pipe(
ofType('[Campaign] Guard Fail', '[LoopbackAuth] Auth Guard fail'),
tap(() => {
this.router.navigateByUrl('/404');
})
);
}
export const CustomEffects= copy(LoopbackEffects); <--- array of effects classes generated from SDK
CustomEffects.push(GuardFailEffect); <--- inserting my custom effect
//function to deep copy the array of Classes aka objects
export function copy(o) {
let output, v, key;
output = Array.isArray(o) ? [] : {};
for (key in o) {
v = o[key];
output[key] = typeof v === 'object' && v !== null ? copy(v) : v;
}
return output;
}
App.module.ts
imports : [
EffectsModule.forRoot(CustomEffects)
]
在构建应用程序时出现此错误
ERROR in src\app\app.module.ts(89,27): Error during template compile of 'AppModule'
Function calls are not supported in decorators but 'copy' was called in 'CustomEffects'
'CustomEffects' calls 'copy'.
=> 如果我不深度复制数组,则在使用推功能时会出现错误
[ts]
Argument of type 'typeof GuardFailEffect' is not assignable to parameter of type 'typeof LoopbackAuthEffects ... 5 more ... | typeof SubscriptionEffects'.
Type 'typeof GuardFailEffect' is not assignable to type 'typeof SubscriptionEffects'.
Types of parameters 'router' and 'subscription' are incompatible.
Type 'SubscriptionApi' is not assignable to type 'Router'.
Property 'rootComponentType' is missing in type 'SubscriptionApi'.
class GuardFailEffect
有人可以指出我正确的方向吗?
答案 0 :(得分:0)
在您的情况下,我将使用forRoot
模式来构造要导入的模块。这应该允许您运行自定义代码。
@NgModule({
})
export class CustomEffectsModule {
static forRoot() {
const customEffects= this.copy(LoopbackEffects);
customEffects.push(GuardFailEffect)
return EffectsModule.forRoot(customEffects);
}
private static copy(o) {
let output = Array.isArray(o) ? [] : {};
for (let key in o) {
let v = o[key];
output[key] = typeof v === 'object' && v !== null ? copy(v) : v;
}
return output;
}
}
imports : [
CustomEffectsModule.forRoot()
]