[英]Angular 15 - Consider using the @Inject decorator to specify an injection token
After using angular's factory provider and sending two arguments in factory.service.ts, I wanted to display 'factory' and a boolean value.使用 Angular 的工厂提供程序并在 factory.service.ts 中发送两个参数后,我想显示“工厂”和一个布尔值。
It seems to work fine in other angular versions, but a problem occurs in angular 15 version.它似乎在其他角度版本中工作正常,但在角度 15 版本中出现问题。
I don't know which part is the problem....不知道哪个部分有问题。。。。
home.component.ts主页.component.ts
import { Component, Inject, OnInit } from '@angular/core';
import { FactoryService } from '../factory.service';
import { LogService } from '../log.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
providers: [LogService,
{provide: 'another', useClass: LogService},
{provide: 'same',useExisting: LogService},
{provide: 'githubUrl',useValue: 'https://github.com/abcd'},
{provide: 'factory',useFactory: (logService) => {
return new FactoryService(logService, true);},
deps: [FactoryService]
}]
})
export class HomeComponent implements OnInit{
constructor(private log:LogService,
@Inject('another') private another,
@Inject('same') private same,
@Inject('githubUrl') private url,
@Inject('factory') private factory
) {
console.log("this.log === this.another", this.log === this.another)
console.log("this.log === this.same", this.log === this.same)
console.log(this.url)
}
ngOnInit(): void {
this.log.info('logservice');
this.factory.log();
}
}
factory.service.ts工厂.服务.ts
import { Inject, Injectable } from "@angular/core";
import { LogService } from "./log.service";
@Injectable()
export class FactoryService {
constructor(private logService: LogService, private isFactory: boolean) {}
public log() {
this.logService.info(`factory ${this.isFactory}`);
}
}
Error错误
Error: src/app/factory.service.ts:6:57 - error NG2003: No suitable injection token for parameter 'isFactory' of class 'FactoryService'.
Consider using the @Inject decorator to specify an injection token.
6 constructor(private logService: LogService, private isFactory: boolean) {}
~~~~~~~~~
src/app/factory.service.ts:6:68
6 constructor(private logService: LogService, private isFactory: boolean) {}
~~~~~~~
This type is not supported as injection token.
I've looked at the official documentation, but I don't know what the problem is.看了官方文档,不知道是什么问题。
As per the error message suggested, you should use @Inject
decorator for your custom parameter, else Angular will treat it as a dependency and try to resolve it.根据建议的错误消息,您应该为自定义参数使用
@Inject
装饰器,否则 Angular 会将其视为依赖项并尝试解决它。
Since you don't have such isFactory
provider, Angular throwed error.由于您没有这样
isFactory
提供程序,因此 Angular 抛出了错误。
Add @Inject decorator with named isFactory
添加名为
isFactory
的 @Inject 装饰器
......
constructor(private logService: LogService, @Inject('isFactory') private isFactory: boolean) {}
......
Provide the isFactory
value and remove second param in new FactoryService
提供
isFactory
值并删除new FactoryService
中的第二个参数
......
providers: [LogService,
{provide: 'another', useClass: LogService},
{provide: 'same',useExisting: LogService},
{provide: 'githubUrl',useValue: 'https://github.com/abcd'},
{
provide: 'isFactory',
useValue: true,
},
{provide: 'factory',useFactory: (logService) => {
return new FactoryService(logService);},
deps: [FactoryService]
},
]
})
The code is not tested.代码未经测试。
Refer to https://www.positronx.io/how-to-pass-parameters-to-angular-service-using-inject/ for more info.有关详细信息,请参阅https://www.positronx.io/how-to-pass-parameters-to-angular-service-using-inject/ 。
Simplest way to resolve is to use Injection tokens.最简单的解决方法是使用注入令牌。 If you use
factory
then these are tree-shakeable.如果你使用
factory
,那么这些是可摇树的。
const IS_FACTORY = new InjectionToken<boolean>('Is factory', {
providedIn: 'root', // optional
factory: () => true
});
To provide,提供,
providers: [{ provide: IS_FACTORY, useValue: false}],
And if you want to use in your component with type safety如果你想在你的组件中使用类型安全
const isFactory = inject(IS_FACTORY); // or the @Inject method
Second argument of inject
allows the usual resolution modifiers of optional, self, host etc. inject
的第二个参数允许使用常用的解析修饰符 optional、self、host 等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.