我正在尝试让我的Angular2服务使用由linkedin提供的脚本加载的LinkedIN javascript SDK。该功能适用于获取数据并返回它但我无法将本地函数变量设置为我从linkeidn获取的数据
export class LinkedInAuthService {
constructor(
) {
}
linkedInAuthorization() {
window['IN'].User.authorize();
window['IN'].Event.on(window['IN'], 'auth', this.getLinkedInUserInfo);
}
getLinkedInUserInfo() {
let linkedinInfo:any;
window['IN']
.API
.Raw()
.url("/people/~:(firstName,lastName,emailAddress,industry,specialties,positions)?format=json")
.result(function(data:any){
console.log(data) //logs data correctly
linkedinInfo = data
});
console.log(linkedinInfo) // undefined
}
AuthLinkedInUserforGlx() {
console.log(localStorage.getItem('linkedinInfo'))
}
}
我认为问题是当我设置linkedinInfo = data
时它没有在本地设置它,只是不确定如何使它本地化
答案 0 :(得分:0)
这对我有用
export class LinkedInService {
IN = window['IN'];
userProfile: Object;
constructor() {
this.userProfile = JSON.parse(localStorage.getItem('profile'))
this.IN.Event.on(this.IN, 'auth', this.getLinkedInUserInfo);
console.log('end of constructor')
}
linkedInAuthorization(){
this.IN.User.authorize();
console.log('end of linkedInAuthorization service')
}
getLinkedInUserInfo() {
this.IN.API.Raw()
.url("/people/~:(firstName,lastName,emailAddress,industry,specialties,positions)?format=json")
.result((data:any) => {
localStorage.setItem('profile', JSON.stringify(data));
this.userProfile = data;
});
console.log('end of getLinkedInUserInfo service');
this.AuthLinkedInOnGlx
}
AuthLinkedInOnGlx (){
}
}