Angular 2编译错误

时间:2017-01-10 06:06:35

标签: angular

在编译角度2应用时遇到了一些错误

app / components / user.component.ts(45,11):错误TS7006:参数'hobby'隐式具有'any'类型。 app / components / user.component.ts(49,14):error TS7006:参数'i'隐含有'any'类型。

import { Component } from '@angular/core';
import {PostService} from '../services/post.service';

@Component({
  moduleId:module.id,
  selector: 'user',
  templateUrl: 'user.component.html',
  providers:[PostService]
})
export class UserComponent  { 
   name: string;
   email: string;;
   address:address;
   hobbies:string[];
   showHobbies:boolean;
   posts:Post[]

  constructor(private postService : PostService){ //everytime load 
    //console.log("hear it is constructor....");
    this.name = 'Jck';
    this.email = 'jck@jsk.com';
    this.address = {
        street: 'Saga',
        city: 'SOP',
        state: 'GuO'
    }
    this.hobbies = ['MUSIC','Cricket'];
    this.showHobbies=false;

    this.postService.getPost().subscribe(posts => {
        //console.log(posts);
        this.posts =posts;
    });
  }

  toggleHobbies(){
    //console.log("toggleHobbies >>>");
    if(this.showHobbies){
        this.showHobbies=false; 
    }else{
        this.showHobbies=true;  
    }

  }
  addHobby(hobby){
    //console.log("add hobby >>"+hobby);
    this.hobbies.push(hobby);
  }
  deleteHobby(i){
    //console.log("deleteHobby i >>"+i);
    this.hobbies.splice(i,1)
  }
}
interface address{
   street : string;
   city : string;
   state : string;
} 
interface Post{
  id:number;
  title:string;
  body:string;
}

1 个答案:

答案 0 :(得分:2)

您忘记为函数addHobbydeleteHobby的参数定义类型:

addHobby(hobby: string){
    //console.log("add hobby >>"+hobby);
    this.hobbies.push(hobby);
}
deleteHobby(i: number){
    //console.log("deleteHobby i >>"+i);
    this.hobbies.splice(i, 1)
}