gpt4 book ai didi

Angular 5,NullInjectorError : No provider for Service

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

你好,我正在尝试将 firestore 实现到我的 Web 应用程序中,当我将服务添加到构造函数时出现错误:

NullInjectorError: No provider for TesteventService!

我正在使用 Angular 5、angularfire2/firestore 和 typescript 2.7.1

testevent.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore } from 'angularfire2/firestore'

@Injectable()
export class TesteventService {

constructor(private afs: AngularFirestore) { }

addEvent(eventData){
this.afs.collection('testevent').add(eventData).then(() => {
console.log('Done');
})
}

getEvent(){
return this.afs.collection('testevent', ref => ref.orderBy('id')).valueChanges()
}
}

组件.ts

import { Component, OnInit } from '@angular/core';
import { TesteventService } from '../../providers/testevent.service';
import { AngularFirestore } from 'angularfire2/firestore';

export class CsvImportComponent implements OnInit {
data_rows = []

constructor(private event: TesteventService){})

当我从 TesteventSerivice 添加事件时,出现错误,但没有运行任何内容。

最佳答案

您需要在 app.module.ts 的 imports 下的 providers 下添加 TesteventService

providers: [
TesteventService
]

关于 Angular 5,NullInjectorError : No provider for Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49112005/

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