您好,我在Component
中使用的是Service
方法,该方法又返回了observable
。我想知道的是,如果我在组件中多次调用Subscribe
会创建多个订阅,从而导致应用程序中断吗?
服务方法
getClients():Observable<User[]>{
let route=UserService.baseUrl+"/users";
var data= this.http.get(route)
.map(resp=>resp)
.catch(error=>{console.log(error);return Observable.throw(JSON.stringify(error));}) as Observable<User[]>;
return data;
组件
export class IndexComponent {
private canPress:boolean;
public enableButtons: boolean;
public users: Array<User>;
constructor(private userservice:UserService,private router:Router) {
}
private getUsers(){
console.log("Entered init");
this.userservice.getClients().subscribe(data=>{
this.users=data;
this.canPress=this.users.length>0;
},error=>{
console.log("From Index: Could not obtain users");
this.users=UserService.users;
console.log(this.users);
});
}
ngOnInit(): void {
this.getUsers();
}
}
如果在我的HTML
中,我将getUsers
方法附加到click
事件中……我会产生新的订阅吗?
答案 0 :(得分:1)