Angular 角度2编译错误
编译angular 2应用程序时会遇到一些错误 app/components/user.component.ts(45,11):错误TS7006:参数'hobby'隐式具有'any'类型。 app/components/user.component.ts(49,14):错误TS7006:参数“i”隐式具有“any”类型Angular 角度2编译错误,angular,Angular,编译angular 2应用程序时会遇到一些错误 app/components/user.component.ts(45,11):错误TS7006:参数'hobby'隐式具有'any'类型。 app/components/user.component.ts(49,14):错误TS7006:参数“i”隐式具有“any”类型 import { Component } from '@angular/core'; import {PostService} from '../services/post.se
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;
}
您忘记为函数的参数定义类型
addHobby
和deletehbby
:
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)
}
您忘记为函数的参数定义类型
addHobby
和deletehbby
:
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)
}