在 useFactory 中访问 DOCUMENT
access DOCUMENT in useFactory
我想在工厂中注入@Inject(DOCUMENT)。如何注入它,因为它不是服务,因此我无法在 deps 中添加它。
// import { DOCUMENT } from '@angular/common';
providers: [
{
provide: APP_INITIALIZER,
deps: [Document],
useFactory: (): any => (document: Document) => {
console.log(document);
},
multi: true,
},
],
操场:https://stackblitz.com/edit/angular-ivy-dll1cp?file=src%2Fapp%2Fapp.module.ts
我什至尝试过一项服务,但我无法读取 getBody();
providers: [
{
provide: APP_INITIALIZER,
deps: [DocumentService],
useFactory: (): any => (document: DocumentService) => {
console.log(document.getBody());
},
multi: true,
},
],
谢谢
您几乎完成了正确的变体。 DOCUMENT 是一个令牌,应该在 deps 数组中。然后文档对象将被注入工厂回调
import { DOCUMENT } from '@angular/common';
providers: [
{
provide: APP_INITIALIZER,
deps: [DOCUMENT],
useFactory: (document: Document): any => () => {
console.log(document);
},
multi: true,
},
],
我想在工厂中注入@Inject(DOCUMENT)。如何注入它,因为它不是服务,因此我无法在 deps 中添加它。
// import { DOCUMENT } from '@angular/common';
providers: [
{
provide: APP_INITIALIZER,
deps: [Document],
useFactory: (): any => (document: Document) => {
console.log(document);
},
multi: true,
},
],
操场:https://stackblitz.com/edit/angular-ivy-dll1cp?file=src%2Fapp%2Fapp.module.ts
我什至尝试过一项服务,但我无法读取 getBody();
providers: [
{
provide: APP_INITIALIZER,
deps: [DocumentService],
useFactory: (): any => (document: DocumentService) => {
console.log(document.getBody());
},
multi: true,
},
],
谢谢
您几乎完成了正确的变体。 DOCUMENT 是一个令牌,应该在 deps 数组中。然后文档对象将被注入工厂回调
import { DOCUMENT } from '@angular/common';
providers: [
{
provide: APP_INITIALIZER,
deps: [DOCUMENT],
useFactory: (document: Document): any => () => {
console.log(document);
},
multi: true,
},
],