我有一个控制器实现了OnInit。问题在于,每当我更改路由并返回到相同的组件时,OnInit都会被调用。我做错了什么,我无法理解。请帮助我。
@Component({
selector:'test-list',
templateUrl:'./testlist.component.html',
styles:[`.testname{
text-transform : capitalize;
}`]
})
export class TestListComponent implements OnInit{
testList:Array<Test>;
constructor(private testService:TestService,private router:Router){}
ngOnInit(){
this.testService.getTest()
.subscribe(
data=>this.testList = <Array<Test>>data,
error=>alert(error)
);
console.log("ngInit")
}
editTest = (id)=>{
this.router.navigate(['createtest',id]);
}
}
TestListComponent
的一个新实例,因此预计会调用OnInit()
。 - penleychan当TestListComponent被创建时,它应该只被调用一次,而不是每次我们返回到同一个组件时都被调用。我说得对吗?
而不是每次我们改变路由时都会创建组件吗?
。 - Aakash Thakuras Array<Test>
?<Array<Test>>data
... 外部的 <> 是必要的吗?如果你的返回类型是我建议的这样,this.testList = data
就足够了。 - JGFMK