Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework 如何设置离子';将本地存储作为同步?_Ionic Framework_Ionic3 - Fatal编程技术网

Ionic framework 如何设置离子';将本地存储作为同步?

Ionic framework 如何设置离子';将本地存储作为同步?,ionic-framework,ionic3,Ionic Framework,Ionic3,这就是我想要的:当我发送一个网络请求时,希望截取可以自动添加标题 所以我做了一个截取来实现这个目标。我计划通过LocalStorage获取价值。然而,它的执行方式是异步的,因此请求不能赶上存储 接下来我该怎么办 我的代码如下: intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | Htt

这就是我想要的:当我发送一个网络请求时,希望
截取
可以自动添加
标题

所以我做了一个截取来实现这个目标。我计划通过
LocalStorage
获取价值。然而,它的执行方式是异步的,因此请求不能赶上
存储

接下来我该怎么办

我的代码如下:

intercept(req: HttpRequest<any>, next: HttpHandler):
   Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
    console.log('into intercept');
    let url = req.url;
    let opuser;
    this.storage.get('userInfo').then((val) => {
        opuser = val.Noid; // get the value what I need
    });

    if (opuser === undefined) {
        opuser = 'undefined';
    }
    const newReq = req.clone({
        url: url
    }).clone({
        setHeaders: { opuser: 'undefined' }
    });// set the headers
    return next.handle(newReq).pipe(
        mergeMap((event: any) => {
            return of(event);
        })
    );
}
intercept(请求:HttpRequest,下一步:HttpHandler):
可观察{
console.log('into intercept');
让url=req.url;
让奥普瑟;
this.storage.get('userInfo')。然后((val)=>{
opuser=val.Noid;//获取我需要的值
});
如果(opuser==未定义){
opuser=‘未定义’;
}
const newReq=req.clone({
url:url
}).克隆({
SetHeader:{opuser:'未定义'}
});//设置标题
返回next.handle(newReq).pipe(
合并映射((事件:任意)=>{
(事件)的返回;
})
);
}

在调用拦截器之前,您应该准备好标题

好的选择:

应用程序启动时,获取标题,并将其存储在提供程序中。这样,您就可以同步/直接访问它

opUserHeader: any;

setOpuserHeader(){
   return this.storage.get('userInfo').then((val) => {
      this.opUserHeader = val.Noid; // get the value what I need
   });
}

getOpuserHeader(){
   return this.opUserHeader ? this.opUserHeader : 'undefined';
}
当您启动应用程序时,您可以在app.component.ts或第一页(您的选择)中从您的服务调用该方法,并且您的标题值将存在于内存中:

headersService.setOpuserHeader().then(() => { console.log('Header is set')};
现在,拦截器看起来应该非常干净,您可以直接获得值:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Get the header from service, attach to the cloned request and continue
    const opUserHeader = this.headersService.getOpuserHeader();
    const authReq = req.clone({setHeaders: {opuser: opUserHeader}});
    return next.handle(authReq);

}
每次您呼叫HttpClient时:

const apiURL = myService.getApiURL();
this.httpClient.post(apiURL + 'api/login', params);
祝你好运

const apiURL = myService.getApiURL();
this.httpClient.post(apiURL + 'api/login', params);