Angular 4错误:类型'()=>中缺少属性'includes';任何' [英] Angular 4 Error : Property 'includes' is missing in type '() => any'

查看:89
本文介绍了Angular 4错误:类型'()=>中缺少属性'includes';任何'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用角度为4且可观察的同时出现错误.

I am getting error while using angular 4 and observable.

/Users//backend/src/app/app.component.ts(15,55):类型'()=> any'不能分配给类型'State []'. /Users//backend/src/app/app.component.ts(15,55):类型'()=> any'不能分配给类型'State []'. 类型()=>任何"中缺少属性包含".

/Users//backend/src/app/app.component.ts (15,55): Type '() => any' is not assignable to type 'State[]'. /Users//backend/src/app/app.component.ts (15,55): Type '() => any' is not assignable to type 'State[]'. Property 'includes' is missing in type '() => any'.

我在做什么错

我的模特

export class State {
  id: number;
  state: string;
  code: string;
}

我的服务

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/toPromise';

import {environment} from '../../../environments/environment';

@Injectable()

export class StateService {
  private baseUrl: string = environment.baseUrl;

  constructor( private http: Http) {}

  /**
   * Get all States
   */
  GetStates() {
   return this.http.get(this.baseUrl + 'v1/states')
      .map((res: Response) => res.json);
     // .do( data => console.log(data));
  }
}

我的组件

import { Component, OnInit } from '@angular/core';
import {StateService} from './shared/services/state.service';
import {State} from './shared/models/state';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styles: []
})
export class AppComponent implements OnInit {
   states: State[] ;
  constructor(private stateService: StateService) {}

  ngOnInit() {
    this.stateService.GetStates().subscribe(states => this.states = states );
  }
}

App.Module

App.Module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import 'rxjs/add/operator/map';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { StateService } from './shared/services/state.service';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AppRoutingModule
  ],
  providers: [
    StateService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

推荐答案

有两个问题.

首先,您没有调用json响应,只是引用了该方法.您需要将res.json更改为res.json():

First, you aren't invoking a json response, you are simply referencing the method. You need to change res.json to res.json():

.map((res: Response) => res.json());

第二,您没有为订阅结果声明类型.您应该明确指定结果的类型:

Second, you aren't declaring a type for the result of your subscribe. You should explicitly specify the type of the result:

this.stateService.GetStates().subscribe((states: State[]) => this.states = states);

由于您的订阅中的参数的类型为any,除非像我上面那样预先指定类型,或者在分配之前进行强制转换,否则Typescript编译器将让您知道这些类型无效.

Since the parameter from your subscribe is of type any, unless you specify a type up front as I have above, or cast it before assignment, the typescript compiler will let you know that the types are invalid.

这篇关于Angular 4错误:类型'()=>中缺少属性'includes';任何'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆