我有Angular2应用程序,可注射服务,类,异步后端调用,什么不是,这不是由be编写的。但不知何故,我被要求在那里提供一种以前没有实现的会话控制。 所以我创建了一个新课程,一个非常基础的课程。
//EnvConfig.ts:
export class SessionParameters {
private static userId: string = 'EMPTY_USER_ID';
private static userLogin: string = 'EMPTY_USER_LOGIN';
private static callEDUID: string = 'EMPTY_CALL_ID'
static setUserID(userid: string): void {
SessionParameters.userId = userid;
}
static setUserLogin(usrLogin: string): void {
SessionParameters.userLogin = usrLogin;
}
static setCurrentCallId(eduid: string): void {
SessionParameters.callEDUID = eduid;
}
static getUserID(): string {
return SessionParameters.userId;
}
static getUserLogin(): string {
return SessionParameters.userLogin;
}
static getCurrentCallId(): string {
return SessionParameters.callEDUID;
}
static clearSession(): void {
SessionParameters.userId = 'EMPTY_USER_ID';
SessionParameters.userLogin = 'EMPTY_USER_LOGIN';
SessionParameters.callEDUID = 'EMPTY_CALL_ID';
}
这里没什么特别的,好吧。现在......我想在应用程序初始化时添加会话参数,所以我这样做了:
//app.ts:
import { SessionParameters } from 'components/EnvConfig';
@Component({
selector: 'http-app',
template: `
<div class="tcs-parent-container" [ngClass]='cardProduct'>
<contact-details
[cId]='cId'>
</contact-details>`
class HttpApp {
constructor (public siebel: CustomerService) {}
....
PostMessageHandler(eventJson: any): void {
...
console.log("Login: "+SessionParameters.getUserLogin());
SessionParameters.setUserLogin(eventJson.params.userLogin);
...
}
好的,这实际上运行正常,因为我在浏览器控制台中看到:“登录:EMPTY_USER_LOGIN”来自传入的事件交互。 如您所见,有一个“联系人详细信息”选择器,子组件将呈现在其中:
//ContactForm.ts:
import { CustomerService } from 'components/CustomerService';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
...
constructor (public crv: CustomerService)
...
/*
subscribe on request's responce execution with implicit observer creation:
- first parameter: function to invoke for each element in the observable sequence - in our case it's responce from http
- second parameter: function to invoke upon exceptional termination of the observable sequence
- third parameter: function to invoke upon graceful termination of the observable sequence
*/
this.crv
.searchBackEnd(id, 'contact')
.subscribe(
(response: any) => this.renderApplet(response),
(error: any) => this.crv.logMessage('contact-details',`${this.cId}`,'backend_call_request_error:${error}'),
() => this.crv.logMessage('contact-details',`${this.cId}`,'backend_call_completed')
);
...
在子组件中,我们使用我们的服务来获取后端的东西。除了这部分之外,服务与我无关:
...
import { BackEndURL, GeneralSettings, SessionParameters } from 'components/EnvConfig';
import 'rxjs/Rx';
...
//method for writig logs back into back-end
logMessage(entityName: string, identifier: string, msg: string): void {
console.log("login_srv: "+SessionParameters.getUserLogin());
//check the switcher
if(GeneralSettings.SERVICE_LOG_ON){
// time management
let currDate: Date = new Date();
let hrs: number = currDate.getHours();
let mnt: number = currDate.getMinutes();
let sec: number = currDate.getSeconds();
let mls: number = currDate.getMilliseconds();
let timeStr: string = `${hrs}:${mnt}:${sec}:${mls}`;
// calling http. No callback on subscription
this.queryWithoutJSONTransform(`${BackEndURL.SERVICE_LOG_ENDPOINT_BRANCH}`, [
`msg=${SessionParameters.getUserLogin()}||${entityName}||${identifier}||${msg}||${timeStr}||`
]).subscribe();
} else {
//do nothing
}
嗯,问题是,SessionParameters.getUserLogin在这里返回undefined。
总而言之,我看到了这个控制台输出:
...
app.ts:109 Login: EMPTY_USER_LOGIN
2016-12-28 12:35:15.815 CustomerService.ts:49 login_srv: undefined
2016-12-28 12:35:15.821 CustomerService.ts:49 login_srv: undefined
2016-12-28 12:35:15.895 CustomerService.ts:49 login_srv: undefined
...
任何人都可以解释为什么私有静态成员在我的服务中是不可访问的,除非由setter特别设置?它通常工作正常,但问题恰恰在于会话控制类的初始静态成员值。