gpt4 book ai didi

Angular InjectionToken 抛出 'No provider for InjectionToken'

转载 作者:搜寻专家 更新时间:2023-10-30 21:26:58 26 4
gpt4 key购买 nike

我目前正在学习新的 Angular 框架,我正在尝试制作一个动态搜索栏,它接受一个服务名称作为参数,以便它动态解析一个服务来查询后端服务。

为此,我使用了一个 Injector,并在 ngOnInit 期间加载服务。这在使用基于字符串的提供程序时工作正常,但是我的 IDE 指出它已被弃用,我应该使用一个我似乎无法理解的 InjectionToken

我希望下面的代码能够工作,因为删除了 InjectionToken 的所有实例并用直接字符串文字替换它们:

我试着查看以下文档,但我不太理解它,因为我觉得我完全按照它说的做了,但它一直告诉我它不起作用:https://angular.io/guide/dependency-injection-providers

谁能告诉我我做错了什么?
谢谢

模块声明

// app.module.ts
@NgModule({
declarations: [
AppComponent,
SearchBarComponent
],
imports: [
BrowserModule,
HttpClientModule,
AppRoutingModule
],
providers: [
{
provide: new InjectionToken<ISearchable>('CustomerService'), // <-- doesn't work; 'CustomerService' <-- works
useValue: CustomerService
}
],
bootstrap: [AppComponent]
})
export class AppModule { }

搜索栏组件:

// search-bar.component.ts
@Component({
selector: 'search-bar',
templateUrl: './search-bar.component.html',
styleUrls: ['./search-bar.component.sass']
})
export class SearchBarComponent implements OnInit {

@Input()
source: string;

private searcher: ISearchable;

constructor(private injector: Injector) {}

ngOnInit() {
// error: Error: No provider for InjectionToken CustomerService!
let token = new InjectionToken<ISearchable>(this.source);
this.searcher = this.injector.get<ISearchable>(token);

// this works, but it's deprecated and will probably break in the future
// this.searcher = this.injector.get(this.source);
console.log(this.searcher);
}
}

使用搜索栏:

<!-- app.component.html -->
<div class="row justify-content-center mb-2">
<div class="col-8">
<search-bar title="Customers" source="CustomerService"></search-bar>
</div>
</div>

编辑:这是一个错误的例子:

https://stackblitz.com/edit/angular-3admbe

最佳答案

在官方angular仓库上询问后,原来是一个简单的解决方案。您无需将服务名称作为字符串传递,而是通过组件将标记传递到另一个组件的 View 中。

全局定义注入(inject) token

我在服务本身的同时进行了此操作,以使其更易于跟踪。

@Injectable()
export class CustomerService implements ISearchable { ... }

export const CUSTOMER_SERVICE = new InjectionToken<ISearchable>('CustomerService');

在您的应用提供商中注册注入(inject) token

import {CUSTOMER_SERVICE, CustomerService} from "./services/customer/customer.service";


@NgModule({
declarations: [ ... ],
imports: [ ... ],
providers: [
{
provide: CUSTOMER_SERVICE, // That's the token we defined previously
useClass: CustomerService, // That's the actual service itself
}
],
bootstrap: [ ... ],
})
export class AppModule { }

通过 View 将 token 传递给您的其他组件

// In your component
import {CUSTOMER_SERVICE} from "./services/customer/customer.service";


@Component({
selector: 'app-root',
template: '<app-search-bar [source]="searcher"></app-search-bar>'
})
export class AppComponent
{
searcher = CUSTOMER_SERVICE;
}

您现在可以从其他组件动态导入服务

@Component({
selector: 'app-search-bar',
templateUrl: './search-bar.component.html',
styleUrls: ['./search-bar.component.sass'],
})
export class SearchBarComponent implements OnInit
{
@Input()
source: InjectionToken<ISearchable>;

private searcher: ISearchable;

constructor(private injector: Injector) {}

ngOnInit()
{
this.searcher = this.injector.get<ISearchable>(this.source);
}

search(query: string)
{
this.searcher.search(query).subscribe(...);
}
}

关于 Angular InjectionToken 抛出 'No provider for InjectionToken',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53317225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com