Angular2 - 服务未在构造函数和 ngOnInit 之外定义?
Angular2 - service not defined outside of constructor and ngOnInit?
我一定是在这里做错了什么,但我不知道是什么...我是一个 Angular2 新手,在我的第一个 ng2 应用程序中跌跌撞撞。
我试图从组件内部访问服务的方法,但该服务仅在 constructor() 和 ngOnInit() 中定义,它在我的其他组件函数中返回为未定义。
这与 类似,但我使用了 private 关键字,但问题仍然存在。
这是我的服务:
import { Injectable } from "@angular/core";
import { AngularFire,FirebaseListObservable } from 'angularfire2';
import { User } from './user.model';
@Injectable()
export class UserService {
userList$: FirebaseListObservable<any>;
apples: String = 'Oranges';
constructor(private af: AngularFire) {
this.initialize();
}
private initialize():void {
this.userList$ = this.af.database.list('/users');
}
getTest(input:String):String {
return "Test " + input;
}
getUser(userId:String):any {
//console.log('get user',userId);
let path = '/users/'+userId;
return this.af.database.object(path);
}
}
我的组件:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { UserService } from '../../shared/user.service';
@Component({
moduleId: module.id,
selector: 'user-detail',
templateUrl: 'user-detail.component.html'
})
export class UserDetailComponent implements OnInit {
routeParams$: any;
one: String;
two: String;
three: String;
constructor(private usrSvc:UserService,private route:ActivatedRoute) {
console.log('constructor',usrSvc); // defined here
this.one = usrSvc.getTest('this is one'); // works correctly
}
ngOnInit() {
console.log('ngOnInit',this.usrSvc); // also defined here
this.two = this.usrSvc.getTest('this is two'); // also works correctly
this.routeParams$ = this.route.params.subscribe(this.loadUser);
}
loadUser(params:any) {
console.log('loadUser',this.usrSvc); // undefined!!
this.three = this.usrSvc.getTest('this is three'); // BOOM
}
ngOnDestroy() {
if (this.routeParams$) {
console.log('unsub routeParams$');
this.routeParams$.unsubscribe();
}
}
}
这是你传递函数的方式
this.routeParams$ = this.route.params.subscribe(this.loadUser);
应该是
this.routeParams$ = this.route.params.subscribe(this.loadUser.bind(this));
或
this.routeParams$ = this.route.params.subscribe((u) => this.loadUser(u));
否则 this
不会指向您当前的 class 而是指向可观察对象的某处(从它被调用的地方)
我一定是在这里做错了什么,但我不知道是什么...我是一个 Angular2 新手,在我的第一个 ng2 应用程序中跌跌撞撞。
我试图从组件内部访问服务的方法,但该服务仅在 constructor() 和 ngOnInit() 中定义,它在我的其他组件函数中返回为未定义。
这与
这是我的服务:
import { Injectable } from "@angular/core";
import { AngularFire,FirebaseListObservable } from 'angularfire2';
import { User } from './user.model';
@Injectable()
export class UserService {
userList$: FirebaseListObservable<any>;
apples: String = 'Oranges';
constructor(private af: AngularFire) {
this.initialize();
}
private initialize():void {
this.userList$ = this.af.database.list('/users');
}
getTest(input:String):String {
return "Test " + input;
}
getUser(userId:String):any {
//console.log('get user',userId);
let path = '/users/'+userId;
return this.af.database.object(path);
}
}
我的组件:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { UserService } from '../../shared/user.service';
@Component({
moduleId: module.id,
selector: 'user-detail',
templateUrl: 'user-detail.component.html'
})
export class UserDetailComponent implements OnInit {
routeParams$: any;
one: String;
two: String;
three: String;
constructor(private usrSvc:UserService,private route:ActivatedRoute) {
console.log('constructor',usrSvc); // defined here
this.one = usrSvc.getTest('this is one'); // works correctly
}
ngOnInit() {
console.log('ngOnInit',this.usrSvc); // also defined here
this.two = this.usrSvc.getTest('this is two'); // also works correctly
this.routeParams$ = this.route.params.subscribe(this.loadUser);
}
loadUser(params:any) {
console.log('loadUser',this.usrSvc); // undefined!!
this.three = this.usrSvc.getTest('this is three'); // BOOM
}
ngOnDestroy() {
if (this.routeParams$) {
console.log('unsub routeParams$');
this.routeParams$.unsubscribe();
}
}
}
这是你传递函数的方式
this.routeParams$ = this.route.params.subscribe(this.loadUser);
应该是
this.routeParams$ = this.route.params.subscribe(this.loadUser.bind(this));
或
this.routeParams$ = this.route.params.subscribe((u) => this.loadUser(u));
否则 this
不会指向您当前的 class 而是指向可观察对象的某处(从它被调用的地方)