从多个子级扩展服务会创建该服务的新实例。我希望能够扩展服务,并且如果一个孩子的数据发生更改,那么另一个孩子也应该可以使用该数据。
我尝试创建服务。我创建了两个模块,两个模块都在扩展服务。
@Injectable({
providedIn: 'root'
})
export class BaseService {
//Api stuff going on here
constructor(){
}
}
@Injectable({
providedIn: 'root'
})
export class AuthService extends BaseService {
//Login stuff going on there
constructor(){
super()
}
}
export class LoginPage extends AuthService {
//Handles login
constructor(){
super()
}
}
@Injectable({
providedIn: 'root'
})
export class UserService extends AuthService {
//User stuff going on here
constructor(){
super()
}
}
export class ProfilePage extends UserService {
//Handles user stuff
constructor(){
super()
}
}
//So LoginPage and UserService are both extending AuthService. For example
//setting a token from LoginPage ->AuthService should make the token available //in the UserService. But how it is now is that it creates multiple instances //of AuthService. Is this even possible to accomplish?
我想知道是否可行。我知道注入服务的基本方法,但这是更凉爽,更实用的方法。