angularjs angular - ERROR NullInjectorError:没有提供程序

a0zr77ik  于 11个月前  发布在  Angular
关注(0)|答案(2)|浏览(118)

我试图从我的asp.net应用程序中列出所有类别,我收到这个nullinjector错误:

ERROR NullInjectorError: R3InjectorError(_AppModule)[CategoriaServiceList -> CategoriaServiceList]: 
  NullInjectorError: No provider for CategoriaServiceList!
    at NullInjector.get (core.mjs:5626:27)
    at R3Injector.get (core.mjs:6069:33)
    at R3Injector.get (core.mjs:6069:33)
    at ChainedInjector.get (core.mjs:15378:36)
    at lookupTokenUsingModuleInjector (core.mjs:4137:39)
    at getOrCreateInjectable (core.mjs:4185:12)
    at Module.ɵɵdirectiveInject (core.mjs:11996:19)
    at NodeInjectorFactory.CategoriasListComponent_Factory [as factory] (categorias-list.component.ts:10:37)
    at getNodeInjectable (core.mjs:4391:44)
    at createRootComponent (core.mjs:15644:35)

字符串
这是我的服务:

export class CategoriaServiceList {

  url: string = 'https://localhost:7239/api/Categorias/listarcategorias';
  list: Categoria[] = [];
  constructor(private http: HttpClient) { }

  listCategory() {
    this.http.get(this.url)
    .subscribe( {
      next: res => {
        this.list = res as Categoria[];
      },
      error: error => {
        console.log(error)
      }
      
    });
  }

}


``
这是我的组件:

import { Component, OnInit } from '@angular/core';
import { Categoria } from '../models/categoria.model';
import { CategoriaServiceList } from '../services/categoria.service';

@Component({
  selector: 'app-categorias-list',
  templateUrl: './categorias-list.component.html',
  styleUrl: './categorias-list.component.css'
})
export class CategoriasListComponent implements OnInit {

  constructor(public service: CategoriaServiceList) { }

  ngOnInit(): void {
    this.service.listCategory();
  }

}


在这里,我试图循环这个数组,以显示我的应用程序的组件的html类别,但错误prowa:

<table class="table table-hover">
            <thead>
              <tr>
                <th scope="col">Id</th>
                <th scope="col">Nome</th>
                <th scope="col">Url</th>
              </tr>
            </thead>
            <tbody>
              <tr *ngFor="let item of service.list">
                <td>{{ item.id }}</td>
                <td>{{ item.nome }}</td>
                <td>{{ item.url }}</td>
              </tr>
            </tbody>
  </table>

ohtdti5x

ohtdti5x1#

您需要在服务中提供@Injectable。
如果你以root身份提供服务,你就不需要在模块/组件上提供服务。否则你需要在组件上指定“CategoriasListComponent”。比如:

@Component({
  selector: 'app-categorias-list',
  templateUrl: './categorias-list.component.html',
  styleUrl: './categorias-list.component.css',
  providers: [CategoriaServiceList]
})

字符串

uttx8gqw

uttx8gqw2#

你很有可能

@Injectable({providedIn: 'root'})

字符串
为你服务

相关问题