本文介绍了无法使用localStorage或sessionStorage从内部订阅到外部订阅获得同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个映射:

User     *----------------------------1     Role

从用户列表中,我去咨询该列表中的一位用户.要获取有关该选定用户的一些信息,我需要使用 localSorage 内部订阅外部订阅中获取对象.

From the list of users, I go to consult one user from this list.To get some informations about that selected user, I need to use localSorage to get an object from inside subscribe to outside subscribe.

下面是方法:

this.userService.getAllRolesOfActualUser(user.id).subscribe(listRoles =>
{

     let roles:Array<Role> = [];

     if (listRoles)
     {
         listRoles.forEach((role) =>
         {
             roles.push(new Role(role.id, role.name, role.selected));
         });
     }

     sessionStorage.removeItem("roles");
     sessionStorage.setItem("roles", JSON.stringify(roles));
     console.log("1. Inside Subscribe- " + user.id + "--------------: " + 
     sessionStorage.getItem("roles"));

});

console.log("2. Outside Subscribe- " + user.id + "--------------: " + sessionStorage.getItem("roles"));

user.service.ts 上,我有:

getAllRolesOfActualUser(id: number): Observable<any>
    {
        return this.http.get(`${this.baseUrl}/users/roles/${id}`);
    }

我的问题是,通常我会得到以前咨询的用户的价值,而不是此屏幕截图:

My problem is that usually I got the value of the previous consulted user not the actual user a described by this screenshot:

您能帮我解决这个问题吗?!非常感谢.

Could you please help me solving that issue ?!.Big thanks.

推荐答案

您必须使用 promise :

    ses : String[];
    expertises : String[] = [];

    async getUri(userId: number)
        {
            let affected = this.userService.getExpertisesListByIdUserPromise(userId);

            await affected.then((uri) =>
            {
                this.ses = uri;
            })

            return this.ses;
        }

在ngOnInit上,我已经

and on ngOnInit, I have

this.getUri(16).then(item => 
        {
            item.forEach(pro=>
            {
                this.expertises.push(pro);
            });
        });

在服务文件上,您具有:

on the service file, you have:

getExpertisesListByIdUserPromise(id: number): Promise<any>
    {
        return this.http.get(`${this.baseUrl}/users/expertises/${id}`).toPromise();
    }

HTH

这篇关于无法使用localStorage或sessionStorage从内部订阅到外部订阅获得同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:25