gpt4 book ai didi

angular - 如何修复 'No provider for service' 错误?

转载 作者:太空狗 更新时间:2023-10-29 17:07:11 26 4
gpt4 key购买 nike

我得到一个错误:

"Exception: Call to Node module failed with error: Error: Uncaught (in promise): Error: No provider for ServiceModuleService! Error: No provider for ServiceModuleService!"

这是一些代码。

app.module.shared.ts:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { AppComponent } from './components/app/app.component'
import { ServiceModuleListComponent } from './components/service-module-list/service-module-list.component';
import { HeaderComponent } from './components/shared/header/header.component';
import { ServiceModuleService } from './components/shared/service-module.service';
import { ServiceModuleComponent } from './components/service-module/service-module.component';

export const sharedConfig: NgModule = {
bootstrap: [AppComponent],
declarations: [
AppComponent,
ServiceModuleListComponent,
HeaderComponent,
ServiceModuleComponent
],
imports: [
RouterModule.forRoot([
{ path: '', redirectTo: 'servicemodulelist', pathMatch: 'full' },
{ path: 'servicemodulelist', component: ServiceModuleListComponent },
{ path: '**', redirectTo: 'servicemodulelist' }
])
],
providers: [ServiceModuleService]
};

app.module.client.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { sharedConfig } from './app.module.shared';

@NgModule({
bootstrap: sharedConfig.bootstrap,
declarations: sharedConfig.declarations,
imports: [
BrowserModule,
FormsModule,
HttpModule,
...sharedConfig.imports
],
providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin },
sharedConfig.providers
]
})
export class AppModule {
}

应用程序组件.ts

import { Component } from '@angular/core';

@Component({
selector: 'app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
}

服务模块.component.ts:

import { Component, Input } from '@angular/core';
import { ServiceModule } from '../shared/service-module.type';

@Component({
selector: 'service-module',
templateUrl: './service-module.component.html'
})
export class ServiceModuleComponent {
@Input() serviceModule: ServiceModule

constructor() {

}
}

服务模块列表.component.ts:

import { Component, OnInit } from '@angular/core';
import { ServiceModule } from '../shared/service-module.type';
import { ServiceModuleService } from '../shared/service-module.service';
import { ServiceModuleComponent }from '../service-module/service-module.component';

@Component({
selector: 'service-module-list',
templateUrl: './service-module-list.component.html'
})
export class ServiceModuleListComponent implements OnInit {
serviceModules: ServiceModule[];

constructor(private serviceModuleService: ServiceModuleService) {
}

ngOnInit() {
this.serviceModuleService.getServiceModuleItems()
.then(serviceModuleItems => {
this.serviceModules = serviceModuleItems;
});
}
}

服务模块.service.ts:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/Rx';
import { ServiceModule } from './service-module.type';

@Injectable()
export class ServiceModuleService {
constructor(private http: Http) { }

getServiceModuleItems() {
return this.http.get('api/ServiceModuleItems')
.map(response => response.json() as ServiceModule[])
.toPromise();
}
}

服务模块.type.ts:

    export class ServiceModule {
idx: number;
applicationName: string;
isActive: boolean;
description: string;
}

基本上在这一点上,在启动时,应用程序必须显示我在 SQL 表上的条目列表,服务调用 API 来获取它,但现在我被错误消息困在了这个问题上。

最佳答案

我进一步研究了这个语法:

imports: [
BrowserModule,
FormsModule,
HttpModule,
...sharedConfig.imports
],

这里的 '...' 是 JavaScript 扩展运算符。它确保在此处添加导入的内容而不是数组本身。我的一位同事是这样解释的:

imports: [
a,
b,
c,
sharedConfig.imports
]

你最终得到(将数组放入)

imports: [a, b, c, [what_shared_config_has]]

而不是你想要的(数组中的值)

imports: [a, b, c, what_shared_config_has]

所以这也需要有传播运算符:

providers: [
{ provide: 'ORIGIN_URL', useValue: location.origin },
...sharedConfig.providers
]

关于angular - 如何修复 'No provider for service' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44465302/

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