角2编译错误
问题描述:
当编译角2应用程式一些误差面临角2编译错误
应用/组件/ user.component.ts(45,11):错误TS7006:参数“爱好”隐含有一个“任意”类型的。 app/components/user.component.ts(49,14):错误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 = '[email protected]';
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
和deleteHobby
的参数定义类型:
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)
}
感谢您的解决方案。 –