Estou querendo mapear as lista de perguntas e jogá-las na propriedade perguntas do meu objeto JSON, mas ai no prompt dá esse erro:
"ERROR in src/app/form/perguntas/perguntas.component.ts(19,5): error TS2322: Type 'Subscription' is not assignable to type 'Pergunta[]'.
Property 'includes' is missing in type 'Subscription'."
Esse é meu perguntas component:
import { Component, OnInit } from '@angular/core';
import { Pergunta } from '../perguntas/pergunta.model'
import { PerguntasService } from '../perguntas.service'
@Component({
selector: 'app-perguntas',
templateUrl: './perguntas.component.html',
styleUrls: ['./perguntas.component.css']
})
export class PerguntasComponent implements OnInit {
perguntas: Pergunta[]
constructor(private perguntasService: PerguntasService) { }
//ass
ngOnInit() {
this.perguntas = this.perguntasService.perguntas()
.subscribe(perguntas => this.perguntas = perguntas)
}
}
Aqui é o meu serviço:
import { Injectable } from '@angular/core';
import { Pergunta } from './perguntas/pergunta.model'
import { PerguntasComponent } from './perguntas/perguntas.component'
import { Http} from '@angular/http'
import {Observable} from 'rxjs/Observable'
import 'rxjs/add/operator/map'
import { MEAT_API} from '../app.api'
@Injectable()
export class PerguntasService{
constructor(private http:Http){}
perguntas(): Observable<Pergunta[]>{
return this.http.get(`${MEAT_API}/perguntas`)
.map( response => response.json())
}
}
e meu app modules:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { ModalModule } from 'ngx-bootstrap/modal';
import { FormsModule } from '@angular/forms';
import { ROUTES } from './app.routes';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { FormComponent } from './form/form.component';
import { PerguntasComponent } from './form/perguntas/perguntas.component';
import { OpcoesComponent } from './form/perguntas/opcoes/opcoes.component';
import { PerguntasService } from './form/perguntas.service';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
FormComponent,
PerguntasComponent,
OpcoesComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(ROUTES),
ModalModule.forRoot()
],
providers: [PerguntasService],
bootstrap: [AppComponent]
})
export class AppModule { }