【问题标题】:No provider for simple component没有简单组件的提供者
【发布时间】:2017-11-28 04:06:55
【问题描述】:

直到 5 分钟前它还在工作,这毫无意义...... 这是组件

import { Component, OnInit } from '@angular/core';
import {Osobaa} from '../osobaa';
import { Osoba } from '../osoba';
import {OsobaService} from '../osoba.service';

@Component({
  selector: 'app-form-add',
  templateUrl: './form-add.component.html',
  styleUrls: ['./form-add.component.css']
})
export class FormAddComponent implements OnInit {
  osoba : Osobaa;
  constructor() { }

  ngOnInit() { }

}

这是主要的 app.module.ts

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import {FormsModule} from "@angular/forms";

import { AppComponent } from "./app.component";
import { HeaderComponent } from "./header/header.component";
import { LoginFormComponent } from "./login-form/login-form.component";
import { FooterComponent } from "./footer/footer.component";
import { DashboardComponent } from "./dashboard/dashboard.component";
import { RouterModule, Routes } from "@angular/router";
import {UserService} from './user.service';
import {AuthguardGuard} from './authguard.guard';
import { UserComponent } from "./user/user.component";
import { NotfoundComponent } from './notfound/notfound.component';
import { TableViewComponent } from './table-view/table-view.component';
import {HttpClientModule} from '@angular/common/http';
import {OsobaService} from './osoba.service';
import { HttpModule } from '@angular/http';
import { FormAddComponent } from './form-add/form-add.component';



const appRoutes: Routes = [
  {
    path: "",
    component: LoginFormComponent
  },
  {
    path: "add" , 
    component: FormAddComponent
  },
  {
  path: "table", component : TableViewComponent
  },
{
  path: "users/:name", 
  component : UserComponent
},
  {
    path: "dashboard",
    canActivate:[AuthguardGuard],
    component: DashboardComponent
  },
  {
    path: '**',
    component: NotfoundComponent
  }
];

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    LoginFormComponent,
    FooterComponent,
    DashboardComponent,
    UserComponent,
    NotfoundComponent,
    TableViewComponent,
    FormAddComponent
  ],
  imports: [BrowserModule, RouterModule.forRoot(appRoutes),HttpModule,FormsModule],
  providers: [UserService,AuthguardGuard,HttpClientModule,OsobaService],
  bootstrap: [AppComponent]
})
export class AppModule {}

控制台错误是:

错误错误:未捕获(承诺):错误:StaticInjectorError [FormAddComponent]: 静态注入器错误[FormAddComponent]: NullInjectorError:没有 FormAddComponent 的提供者! 错误:StaticInjectorError [FormAddComponent]: 静态注入器错误[FormAddComponent]: NullInjectorError: 没有 FormAddComponent 的提供者!

【问题讨论】:

    标签: angular angular-providers


    【解决方案1】:

    providers 中删除HttpClientModule 模块,它应该放在NgModuleimports 选项中。还可以尝试重新构建/重新运行应用程序。

    可能您正在尝试将组件作为可注入的方式注入到服务中。

    【讨论】:

    • 我做到了,不是那样的。
    • 如果有帮助,我会在 html 中的那个组件上使用 ngModel
    • 您是否尝试在某个构造函数中注入 FormAddComponent aas 依赖项?
    • 实际上它发生在我尝试加载另一个正在工作的组件并且我没有触摸它时,但错误提到了 FormAddComponent
    • 我发现了“错误”,我将 FormAddComponent 导入服务以发送对象以进行 post http 发送,但它以某种方式影响了另一个组件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2022-01-16
    • 2017-08-13
    • 1970-01-01
    • 2011-04-25
    • 2017-01-06
    • 2012-07-07
    相关资源
    最近更新 更多