无法解析RecipesService

时间:2017-07-30 15:42:58

标签: angular typescript firebase ionic-framework firebase-realtime-database

当我使用fire base作为数据库运行ionic3应用程序时,我收到了该错误。

这里是recipe.ts文件的内容:

import {Recipe} from "../src/models/recipe";
import {Ingredient} from "../src/models/ingredient";
import {AuthService} from "./auth";
import {Http, Response} from "@angular/http";
import 'rxjs/Rx';



export class RecipesService {
  constructor (private authService:AuthService,
               private http:Http){}

  private  recipes: Recipe[] = [];

  addRecipe(title: string,
            description: string,
            difficulty: string,
            ingredients: Ingredient[]) {
    this.recipes.push(new Recipe(title, description, difficulty, ingredients));
    console.log(this.recipes) ;
  }

  getRecipes() {
    return this.recipes.slice();
  }

  updateRecipe(index: number,
               title: string,
               description: string,
               difficulty: string,
               ingredients: Ingredient[]) {
    this.recipes[index] = new Recipe(title, description, difficulty, ingredients);
  }

  removeRecipe(index :  number ) {
    this.recipes.splice(index, 1);
  }




  storeList(token: string) {
    const userId = this.authService.getActiveUser().uid;
    return this.http
      .put('https://fir-crud-69d01.firebaseio.com/' + userId + '/recipes.json?auth=' + token, this.recipes)
      .map((response: Response) => {
        return response.json();
      });
  }

  fetchList(token : string){
    const userId = this.authService.getActiveUser().uid;
    return this.http.get('https://fir-crud-69d01.firebaseio.com/' + userId +'/recipes.json?auth=' + token )
      .map((response: Response)=> {
        return response.json();
      })
      .do((recipes:Recipe[])=>{
        if(recipes){
          this.recipes= recipes;
        }else {
          this.recipes= []
        }

      });
  }
}

来自控制台的错误:

  

vendor.js:112012未捕获错误:无法解析RecipesService的所有参数:(?,?)。

在使用recipe.ts文件作为服务器将其连接到firebase

之前,它工作正常

1 个答案:

答案 0 :(得分:0)

<强>服务

import {Recipe} from "../src/models/recipe";
import {Ingredient} from "../src/models/ingredient";
import {AuthService} from "./auth";
import {Http, Response} from "@angular/http";
import 'rxjs/Rx';


    @Injectable() // you missed this 
    export class RecipesService {

在App module.ts

@NgModule({
    imports: .. ,
    declarations: ..,
    providers: [RecipesService], // add this service
    bootstrap: ..,
})

在组件中

constructor(private service : RecipesService){}