angular - Angular useValue and injection token
问题描述
In my angular app I have
{ provide: QUEUE, useValue: window.events }
in my providers. I was wondering how can I set QUEUE to use an empty array [] if window.events are undefined. I tried something like this and it didn't work.
{ provide: QUEUE, useValue: window.events || [] }
Here is what I have in QUEUE.injection-token.ts
import { InjectionToken } from '@angular/core';
export const \QUEUE = new InjectionToken<Array<AppEvent>>('QUEUE');
Thanks!
解决方案
window.events || []
check should be performed at runtime because window
doesn't exist at compilation time.
It likely should be:
export function queueFactory(): any[] {
return window.events || [];
}
...
providers: [ provide: QUEUE, useFactory: queueFactory }]
...
推荐阅读
- .net - 带有验证的文本框。即使输入的文本无效,如何更新目标属性?
- javascript - 如何在每个项目上运行同步 POST?
- php - Jquery Ajax - 在 Ajax 中发布巨大的字符串值
- sql - 在 Eloquent 数据库选择中将日期时间转换为时间戳
- uwp - 应用激活失败
- java - 如何使用 findAny() 而不返回 null
- ios - 如何在 iOS 中使用 Radius 和 rank=distance 搜索附近的地方
- php - 如何根据从数据库填充的下拉框中的值获取文本字段中的数据?
- parameters - 将变量传递给颤振中的void函数
- python - 如何创建自己的数据集以使用来自 Tensorflow 对象检测 API 的 Mask-RCNN 模型?