我正在尝试使用Angular 2中的ActivatedRoute来获取URL参数。它实际上是在提前获取值,但在重新启动服务器时,它将被提取为未定义。
这是我的组件代码:
ngOnInit(): void {
this.activatedRoute.queryParams.subscribe((params: Params) => {
var theAccessToken = params['accessToken'];
var theRefreshToken = params['refreshToken'];
var theLivePortalID = params['livePortalId'];
var theDemoPortalID = params['demoPortalId'];
localStorage.setItem('theAccessToken', theAccessToken);
localStorage.setItem('theRefreshToken', theRefreshToken);
localStorage.setItem('theLivePortalID', theLivePortalID);
localStorage.setItem('theDemoPortalID', theDemoPortalID);
console.log(theAccessToken);
console.log(theRefreshToken);
console.log(theLivePortalID);
console.log(theDemoPortalID);
});
this.surveyService.getSurveysToken().then(surveys => {
console.log("After Hit" , surveys);
var theAccessToken = localStorage.getItem("theAccessToken");
var theRefreshToken = localStorage.getItem("theRefreshToken");
console.log(theAccessToken);
console.log(theRefreshToken);
this.surveys = surveys;
this.surveyToken = Object.keys(this.surveys);
console.log("Token message: " ,surveys["message"]);
console.log("Token value: " ,surveys["data"].token);
console.log("Token message: " ,surveys["status"]);
if (surveys["message"] == 'Token generated') {
localStorage.setItem('theListingToken', surveys["data"].token);
this.surveyService.getSurveysList().then(surveys => this.surveys = surveys);
} else {
alert("Error: List not found")
}
});
这是服务:
getSurveysToken(): Promise<Survey[]> {
console.log("Before Hit Token ",this.adminToken);
var theAccessToken = localStorage.getItem("theAccessToken");
var theRefreshToken = localStorage.getItem("theRefreshToken");
var data = {
access_token: theAccessToken,
refresh_token: theRefreshToken,
}
console.log(data);
return this.http.post(this.adminToken, data)
.toPromise()
.then(response => response.json() as Survey[])
.catch(this.handleError);
}
getSurveysList(): Promise<Survey[]> {
var theLivePortalID = localStorage.getItem("theLivePortalID");
var theDemoPortalID = localStorage.getItem("theDemoPortalID");
var theListingToken = localStorage.getItem("theListingToken");
console.log("Before Hit surveyList ",theListingToken);
var data = {
live_portal_id: theLivePortalID,
demo_portal_id: theDemoPortalID,
token: theListingToken
}
console.log(data);
return this.http.post(this.surveysListing, data)
.toPromise()
.then(response => response.json() as Survey[])
.catch(this.handleError);
}
有人可以指出我哪里出错吗?