我在android仿真器中遇到错误
类型错误:无法读取未定义的属性'clientId'
当我调用函数linkedinLogin()
时,该错误会在警报框中弹出。
`clientId = 'xxxxx';`
linkedinLogin() {
this.platform.ready().then(() => {
this.linkedinPage().then(success => {
alert(success.access_token);
},(error) => {
alert(error);
});
});
}
linkedinPage(): Promise<any> {
return new Promise(function(resolve, reject) {
var browserRef = window.cordova.InAppBrowser.open('https://www.linkedin.com/uas/oauth2/authorization?client_id=' + this.clientId + '&redirect_uri=' + this.redirect_uri + '&scope=' + this.appScope.join(" ") + '&response_type=code&state=' + this.state, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
browserRef.addEventListener("loadstart", (event) => {
if((event.url).indexOf(this.redirect_uri) === 0) {
try {
var requestToken = (event.url).split("code=")[1].split("&")[0];
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post("https://www.linkedin.com/oauth/v2/accessToken?client_id='xxxx'&client_secret='xxxxx'&redirect_uri=http://localhost/callback&grant_type=authorization_code&code=" + requestToken,
{headers: headers})
.success(function(data){
resolve(data);
alert(data);
})
.error(function(data,status) {
reject('problem in authenticating');
})
.finally(function() {
browserRef.close();
},10);
} catch(e) {
setTimeout(function() {
browserRef.close();
}, 10);
}
}
else {
browserRef.addEventListener("exit", function(event) {
reject("The linkedin sign in flow was canceled");
});
}
});
});
}
答案 0 :(得分:0)
在变量中设置this关键字以访问您的类的上下文,如下所示
linkedinPage(): Promise<any> {
var self=this;
return new Promise(function(resolve, reject) {
var browserRef = window.cordova.InAppBrowser.open('https://www.linkedin.com/uas/oauth2/authorization?client_id=' + self.clientId + '&redirect_uri=' + this.redirect_uri + '&scope=' + this.appScope.join(" ") + '&response_type=code&state=' + this.state, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
var self = this;
self.clientId
希望这会有所帮助