我希望获得当前用户的userId,并将其发送到另一个AddExperience方法中,该方法包含该userId作为参数(外键)。UserId在登录后存储在LocalStorage中!
这是服务的操作:
AddExperience (UserId : any) {返回this.http.post(this.rootUrl + '/AddExperience'+ UserId,{responseType:'json'});}
这些是localeStorage值:
令牌-费用:2122-07-23T01:40:15 z
UserRole :用户
编号:2aa0f 755-cf7d-4c62-bfe8-1de35ee01b09
如何在AddExperience TypeScript文件中添加当前用户的体验?
发布于 2022-07-23 02:05:43
从localStorage中检索第一个导入的方法
import { StorageMap } from '@ngx-pwa/local-storage';然后,在构造函数中,我声明了一个受保护的变量,这个变量来自于w治角自动创建一个局部变量。
constructor(protected storage: StorageMap) { }然后,您可以调用存储局部变量中的方法,如下所示
public get<D, M>(key: string) {
return this.storage.get(key).pipe(
map((item?: any) => {
if (item) {
return new StorageItem(item?.data, item?.meta, item?.meta?._creationTimestamp);
} else {
return new StorageItemError(`Data of ${key} was not found in Storage`);
}
})
);
}然后你可以在你的特殊情况下用它像这样
public userIdObtainedFromLocalStorage: string = '';
this.get('ID').subscribe((userId: string) => {
this.userIdObtainedFromLocalStorage = userId;
})https://stackoverflow.com/questions/73087494
复制相似问题