获取窗口函数在本地分配变量

Getting Window function to assign variable locally

本文关键字:分配 变量 窗口函数 获取      更新时间:2023-09-26

我试图让我的Angular2服务使用LinkedIN javascript SDK,该SDK是由LinkedIN提供的脚本加载的。该功能用于获取数据并返回数据但我不能将本地函数变量设置为我从链接

返回的数据
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时它没有在本地设置它,只是不确定如何使它成为本地

这对我有用

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 (){
}

}