Ionic3中的订阅不返回任何内容

时间:2018-09-29 20:13:09

标签: ionic3

我正在用Ionic3做一个食谱应用程序proyect,尝试从服务中获取数据时遇到了问题。

这是我的服务

export interface Recipe{
    id: number;
    name: string;
    image: string;
    time: number;
    ingredients: string[];
    elaboration: string[];
}

export interface Data{
    category: string;
    recipes: Recipe[];
}

@Injectable()
export class RecipeService{

    constructor(){

    }

    data(): Data[]{
         const data = [
               {"category": "desserts", "recipes": [Array of recipes here]},
               {"category": "desserts", "recipes": [Array of recipes here]},
               {"category": "desserts", "recipes": [Array of recipes here]}
            ];

         return data;
     }

    getData(): Subject<Data[]>{

        const data = new Subject<Data[]>();


        data.next(this.data());


        return data;
    }

我试图像这样在我的Page中获取此数据:

@Component({
  selector: 'page-recipes-categories',
  templateUrl: 'recipes-categories.html',
})
export class RecipesCategoriesPage implements OnInit, OnDestroy {

  private subscription: Subscription;
  public categories: string[];
  public data: Data[];

  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              private recipeService: RecipeService) {
  }

  ngOnInit(){


    this.subscription = this.recipeService.getData().subscribe((data: Data[]) => {
      this.data = data;
     })


  }

  ngOnDestroy(){

  }

}

当我创建console.log(this.data)时,我得到“未定义”。我还有其他类似的服务,并且工作正常。我真的不了解发生了什么。

PD:所有进口都可以。

0 个答案:

没有答案