问题
I'm getting a No Provider for FirebaseService error even after bootstrapping the FirebaseService in the bootstrap(app, [providers]) method.
I followed Pascal's guide for injecting a service within a service and it worked when I injected the Http service with the HTTP_PROVIDERS bootstrapped, but I receive a No providers for FirebaseService error after changing it to my own service.
I can inject both providers individually by removing the injection of the FirebaseService just fine.
Even if I do contructor(@Inject(FirebaseService) firebase: FirebaseService){} it won't work, but my understanding is that adding the @Injectable() decorator should have the same effect.
login-page.ts
import {Component} from '@angular/core';
import {UserService} from '../../Services/user.service';
import {FirebaseService} from '../../services/firebase.service';
import { UserModel } from '../../export';
@Component({
moduleId: 'app/PAGES/login-page/',
selector: 'login-page',
templateUrl: 'login-page.html',
styleUrls: ['login-page.css'],
providers: [ UserService]
})
export class LoginPage {
constructor(private userService: UserService) {}
user:UserModel = new UserModel();
hello: string = "You got yourself a login page, sir";
dostuff() {
console.log(this.user);
// this.userService.createUser(this.user);
}
}
main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { FirebaseService } from './SERVICES/firebase.service';
bootstrap(AppComponent, [
FirebaseService
]);
UserService
import { Injectable, Inject } from '@angular/core';
import {FirebaseService} from './firebase.service';
import { UserModel } from '../export';
@Injectable()
export class UserService {
constructor(private firebaseService: FirebaseService ) {}
public createUser(user: UserModel): any {
console.log(user);
// this.firebaseService.set(user)
}
public getUser(username: string): any {
// return this.firebaseService.get("users/" + username);
}
}
FirebaseService
@Injectable()
export class FirebaseService {
public get(path: string): any {
console.log(path);
}
public set(objectToSave: any) {
console.log(objectToSave);
}
}
回答1:
I made plunk and it's working and inject firebase service into users service.
main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { App } from './app';
import { FirebaseService } from './firebase.service';
bootstrap(App, [
FirebaseService
]);
app.ts
//our root app component
import {Component} from '@angular/core'
import {UserService} from './user.service';
@Component({
selector: 'my-app',
providers: [UserService],
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
`,
directives: []
})
export class App {
constructor(private userService: UserService) {
this.userService.createUser({h: 1});
this.name = 'Angular2 (Release Candidate!)'
}
}
user.service.ts
import { Injectable, Inject } from '@angular/core';
import {FirebaseService} from './firebase.service';
import { UserModel } from '../export';
@Injectable()
export class UserService {
constructor(private firebaseService: FirebaseService ) {}
public createUser(user: UserModel): any {
console.log(this.firebaseService)
console.log(user);
// this.firebaseService.set(user)
}
public getUser(username: string): any {
// return this.firebaseService.get("users/" + username);
}
}
firebase.service.ts
import {Injectable} from '@angular/core'
@Injectable()
export class FirebaseService {
public get(path: string): any {
console.log(path);
}
public set(objectToSave: any) {
console.log(objectToSave);
}
}
http://plnkr.co/edit/RLVT9dvfqH9W2BvE0qMG?p=preview
回答2:
I just learned that the import statement paths are case-sensitive.
I had import {UserService} from '../../services/user.service'; while the directory was ../../SERVICES/user.service.
Where's the facepalm emoji?
回答3:
you are not inserting UserService in main.ts. try adding this as well :
import { bootstrap } from '@angular/platform-browser-dynamic';
import { PLATFORM_DIRECTIVES, provide } from '@angular/core';
import { AppComponent } from './app.component';
import { ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router';
import { UxButton, UxText } from './export';
import { FirebaseService } from './SERVICES/firebase.service';
import { UserService } from './SERVICES/user.service';
bootstrap(AppComponent, [
FirebaseService,
UserService //<-- add this
,ROUTER_DIRECTIVES
,ROUTER_PROVIDERS
,provide(PLATFORM_DIRECTIVES, {useValue: UxButton, multi: true})
,provide(PLATFORM_DIRECTIVES, {useValue: UxText, multi: true})
]);
your login-page.ts page is dependent on UserService so it needs to get bootstraped as well.
来源:https://stackoverflow.com/questions/37649401/how-do-you-inject-a-custom-service-into-another-custom-service