【问题标题】:No provider for servce 'VehicleService'没有“车辆服务”服务提供商
【发布时间】:2018-10-23 07:17:07
【问题描述】:

我收到错误:

处理请求时发生未处理的异常。

NodeInvocationException:未捕获(承诺):错误:没有提供程序 车辆服务!错误:没有 VehicleService 的提供者!在 注入错误 (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:12066:90) 在 noProviderError (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:12104:12) 在 ReflectiveInjector_.module.exports.ReflectiveInjector_.throwOrNull (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:13546:19) 在 ReflectiveInjector.module.exports.ReflectiveInjector_.getByKeyDefault (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:13585:25) 在 ReflectiveInjector.module.exports.ReflectiveInjector_.getByKey (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:13517:25) 在 ReflectiveInjector.module.exports.ReflectiveInjector_.get (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:13386:21) 在 resolveNgModuleDep (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:20430:25) 在 NgModuleRef_.module.exports.NgModuleRef_.get (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:21512:16) 在 resolveDep (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:22015:45) 在创建类 (C:\Users\andrewkp\source\repos\Complexus.Inventory.UI\Complexus.Inventory.UI\ClientApp\dist\vendor.js:21868:35) 错误:未捕获(承诺):错误:没有 VehicleService 的提供者! 错误:没有 VehicleService 的提供者!

这是我使用 VehicleService 的组件:

import { Component, Inject, Input } from '@angular/core';
import { VehicleService } from '../../Services/VehicleService';
import { Vehicle } from '../../Models/Vehicle';

@Component({
    selector: 'addVehicle',
    templateUrl: './add.component.html'
})
export class AddComponent {
    @Input() public vehicle: Vehicle;

    constructor(private vehicleService: VehicleService) {
        this.vehicle = new Vehicle();
    }

    public AddVehicle(): void {
        this.vehicleService.AddVehicle(this.vehicle);
    }
}

我的车辆服务:

import { HttpModule, Http } from '@angular/http';
import { Injectable, Inject } from '@angular/core';
import { Vehicle } from '../Models/Vehicle';
import { ConfigService } from './ConfigService';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class VehicleService {

    constructor(private httpClient: Http, private configService: ConfigService) { }

    public AddVehicle(vehicle: Vehicle) {
        try {
            //do some manipulation: CylinderCapactiy = Enginesize / CylinderVariant
            vehicle.CylinderCapacity = vehicle.EngineCapacity / vehicle.CylinderVariant;
            var response = this.httpClient.post(this.configService.AddVehicleEndPoint, vehicle);
        }
        catch (err) {
            console.log(err);
        }
    }
}

还有我的 app.browser.module.ts 文件:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppModuleShared } from './app.shared.module';
import { AppComponent } from './components/app/app.component';
import { VehicleService } from './Services/VehicleService';
import { StatService } from './Services/StatService';
import { ConfigService } from './Services/ConfigService';
import { HttpModule, Http } from '@angular/http';

@NgModule({
    bootstrap: [ AppComponent ],
    imports: [
        BrowserModule,
        AppModuleShared,
        HttpModule,
        Http
    ],
    providers: [
        StatService,
        ConfigService,
        VehicleService,
        { provide: 'BASE_URL', useFactory: getBaseUrl }
    ]
})
export class AppModule {
}

export function getBaseUrl() {
    return document.getElementsByTagName('base')[0].href;
}

这是我的 app.shared.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './components/app/app.component';
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { AddComponent } from './components/Vehicle/add.component';
import { CounterComponent } from './components/counter/counter.component';

@NgModule({
    declarations: [
        AppComponent,
        NavMenuComponent,
        CounterComponent,
        FetchDataComponent,
        HomeComponent,
        AddComponent
    ],
    imports: [
        CommonModule,
        HttpModule,
        FormsModule,
        RouterModule.forRoot([
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'counter', component: CounterComponent },
            { path: 'fetch-data', component: FetchDataComponent },
            { path: 'add-vehicle', component: AddComponent },
            { path: '**', redirectTo: 'home' }
        ])
    ]
})
export class AppModuleShared {
}

为什么会出现这个错误?

【问题讨论】:

  • 车辆组件属于哪个模块?
  • @AmadouBeye 我更新了我原来的问题。它属于 NgModule
  • @Vikas 你是什么意思
  • 检查是否在两个模块中错误地提供了VehcleService?
  • 尝试将 VehicleService 导入您的根模块。

标签: angular visual-studio-2017 .net-core


【解决方案1】:

我没有看到您的组件在您的 app.browser.module.ts 中的声明下注册

如果属于同一个模块,将providers下的组件添加为,

@NgModule({
  declarations: [AppComponent]
})

如果它来自不同的模块,也将它添加到声明和导出下。也使用 HttpClientModule 代替 Http

编辑

如上所述,您需要在 Shared.Module.ts

内的声明和导出下添加 AppComponent
@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [AppComponent],
  exports: [AppComponent]
})

【讨论】:

  • 问题中没有提到VehicleComponent。这如何回答这个问题?
  • VehicleComponent 应该是您调用 VehicleService 的组件
  • @AmadouBeye 在帖子中该服务正在AppComponent 中使用。我错过了什么吗? VehicleService 似乎是问题所在。
  • @R.Richards OP 之前没有提到组件的名称,更新了它
  • @Sajeetharan 现在这更有意义了。谢谢!!我以为我疯了。 :)
猜你喜欢
  • 2017-05-26
  • 2019-10-21
  • 2017-09-22
  • 2014-07-08
  • 2016-08-23
  • 1970-01-01
  • 2020-02-04
  • 2020-09-21
  • 2022-01-19
相关资源
最近更新 更多