定义服务 属性 并在组件中访问它
Define service property and access it in component
不确定如何从 AuthService
访问 admin
属性。组件中完全相同的代码将 this.admin
设置为数据库中的值,但我不想重写每个组件中的代码。
@Injectable()
export class AuthService {
public admin: any;
constructor() {
this.currentUser.take(1).subscribe(user => {
if (user) {
this.db.object(`/users/${user.uid}`).subscribe(usr => this.setAdmin(usr));
}
});
}
}
setAdmin(usr) {
console.log(usr.admin); // returns value from database
this.admin = usr.admin;
console.log(this.admin); // returns usr.admin value
}
分量
export class AppComponent implements OnInit {
public isA: any;
constructor(public authService: AuthService,){
}
ngOnInit() {
this.isA = this.authService.admin; // this is undefined
}
}
组件编辑*****
ngOnInit() {
this.msgService.getPermission();
this.msgService.receiveMessage();
this.message = this.msgService.currentMessage;
this.authService.currentUser.take(1).subscribe(user => {
if (user) {
console.log(user.uid);
this.db.object(`/users/${user.uid}`).subscribe(usr => this.setAdmin(usr))
}
});
}
setAdmin(usr) {
this.admin = usr.admin;
}
首先,理想情况下,您的服务需要一个 get
方法。其次,在不知道什么时候会存在的情况下访问一个值是很棘手的(ASYNC,不会立即 return 也不会为它停止代码。像 PHP )。
所以你的选择是将你想用 .subscribe ()
方法中的值做的事情包装起来,或者让 ngDoCheck (){}
不断地监视变化,当值存在时它会拾取(我不推荐这种方法因为它运行任何更改)。或者我更喜欢把东西做成 BehaviourSubject
这样它就可以作为 Observable 并且你可以 Subscribe
它,这样当值发生变化时组件会自动拾取它。
@Injectable()
export class AuthService {
public admin: BehaviourSubject < any > = new BehaviourSubject(null);
constructor() {
this.currentUser.take(1).subscribe(user => {
if (user) {
this.db.object(`/users/${user.uid}`).subscribe(usr => this.setAdmin(usr));
}
});
}
}
setAdmin(usr) {
console.log(usr.admin); // returns value from database
this.admin.next (usr.admin);
console.log(this.admin); // returns usr.admin value
}
getAdmin(): Observable < any > {
return this.admin.asObservable();
}
export class AppComponent implements OnInit {
public isA: any;
constructor(public authService: AuthService, ) {
}
ngOnInit() {
this.authService.getAdmin.subscribe((admin: any) => {
//Anything in here get executed every time the `admin` variable's value chnages
});
}
// If you want to see how the ngDocheck() behave
ngDoCheck() {
console.log("DoCheck");
}
}
不确定如何从 AuthService
访问 admin
属性。组件中完全相同的代码将 this.admin
设置为数据库中的值,但我不想重写每个组件中的代码。
@Injectable()
export class AuthService {
public admin: any;
constructor() {
this.currentUser.take(1).subscribe(user => {
if (user) {
this.db.object(`/users/${user.uid}`).subscribe(usr => this.setAdmin(usr));
}
});
}
}
setAdmin(usr) {
console.log(usr.admin); // returns value from database
this.admin = usr.admin;
console.log(this.admin); // returns usr.admin value
}
分量
export class AppComponent implements OnInit {
public isA: any;
constructor(public authService: AuthService,){
}
ngOnInit() {
this.isA = this.authService.admin; // this is undefined
}
}
组件编辑*****
ngOnInit() {
this.msgService.getPermission();
this.msgService.receiveMessage();
this.message = this.msgService.currentMessage;
this.authService.currentUser.take(1).subscribe(user => {
if (user) {
console.log(user.uid);
this.db.object(`/users/${user.uid}`).subscribe(usr => this.setAdmin(usr))
}
});
}
setAdmin(usr) {
this.admin = usr.admin;
}
首先,理想情况下,您的服务需要一个 get
方法。其次,在不知道什么时候会存在的情况下访问一个值是很棘手的(ASYNC,不会立即 return 也不会为它停止代码。像 PHP )。
所以你的选择是将你想用 .subscribe ()
方法中的值做的事情包装起来,或者让 ngDoCheck (){}
不断地监视变化,当值存在时它会拾取(我不推荐这种方法因为它运行任何更改)。或者我更喜欢把东西做成 BehaviourSubject
这样它就可以作为 Observable 并且你可以 Subscribe
它,这样当值发生变化时组件会自动拾取它。
@Injectable()
export class AuthService {
public admin: BehaviourSubject < any > = new BehaviourSubject(null);
constructor() {
this.currentUser.take(1).subscribe(user => {
if (user) {
this.db.object(`/users/${user.uid}`).subscribe(usr => this.setAdmin(usr));
}
});
}
}
setAdmin(usr) {
console.log(usr.admin); // returns value from database
this.admin.next (usr.admin);
console.log(this.admin); // returns usr.admin value
}
getAdmin(): Observable < any > {
return this.admin.asObservable();
}
export class AppComponent implements OnInit {
public isA: any;
constructor(public authService: AuthService, ) {
}
ngOnInit() {
this.authService.getAdmin.subscribe((admin: any) => {
//Anything in here get executed every time the `admin` variable's value chnages
});
}
// If you want to see how the ngDocheck() behave
ngDoCheck() {
console.log("DoCheck");
}
}