Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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
是否有方法从angular 6 universal中的server.ts文件中的interceptor.service.ts访问变量?_Angular_Angular6_Angular Universal_Server Side Rendering - Fatal编程技术网

是否有方法从angular 6 universal中的server.ts文件中的interceptor.service.ts访问变量?

是否有方法从angular 6 universal中的server.ts文件中的interceptor.service.ts访问变量?,angular,angular6,angular-universal,server-side-rendering,Angular,Angular6,Angular Universal,Server Side Rendering,我需要将“this.cacheFlag”变量从interceptor.service.ts访问到应用程序根目录下的server.ts文件中 返回next.handle(this.authReq).pipe( 点击((事件:HttpEvent)=>{ if(HttpResponse的事件实例){ 此.reqArray.push(事件状态); this.cacheFlag=true;//这是我想在server.ts文件中访问的变量 } }), catchError((错误,捕获)=>{ //截取响

我需要将“this.cacheFlag”变量从interceptor.service.ts访问到应用程序根目录下的server.ts文件中

返回next.handle(this.authReq).pipe(
点击((事件:HttpEvent)=>{
if(HttpResponse的事件实例){
此.reqArray.push(事件状态);
this.cacheFlag=true;//这是我想在server.ts文件中访问的变量
}
}),
catchError((错误,捕获)=>{
//截取响应错误并将其转移到控制台
if(HttpErrorResponse的错误实例){
if(error.url.indexOf(“detail?referenceCode=”)=-1){
此.reqArray.push(错误状态);
this.cacheFlag=false;//这是我要在server.ts文件中访问的变量
}
})

将变量设置为静态:

export class MyInterceptorService {
  static cacheFlag;

  intercept(...) {
    ...
    if (MyInterceptorService.cacheFlag) {
      // Cache
    }
  }
}

例如,尝试使用共享服务,然后订阅服务器中的值 使用此代码创建共享服务

cacheFlag = new Observable<boolean>(false);
_cacheFlag = this.cacheFlag.asObservable();


nextValue(val) {
  this.cacheFlag.next(val);
}
最后在你的拦截器里

return next.handle(this.authReq).pipe(
    tap((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {
        this.reqArray.push(event.status);
        this.ss.nextValue(true); //change this part

      }
    }),
    catchError((error, caught) => {
      //intercept the response error and displace it to the console
      if (error instanceof HttpErrorResponse) {
        if(error.url.indexOf("detail?referenceCode=") == -1){
          this.reqArray.push(error.status);
          this.ss.nextValue(false) //change this part
        }
   })
返回next.handle(this.authReq).pipe(
点击((事件:HttpEvent)=>{
if(HttpResponse的事件实例){
此.reqArray.push(事件状态);
this.ss.nextValue(true);//更改此部分
}
}),
catchError((错误,捕获)=>{
//截取响应错误并将其转移到控制台
if(HttpErrorResponse的错误实例){
if(error.url.indexOf(“detail?referenceCode=”)=-1){
此.reqArray.push(错误状态);
this.ss.nextValue(false)//更改此部分
}
})
该值将在interceptor.ts和server.ts中更改
我希望这在某种程度上有所帮助。

如何添加一个共享服务,将其注入到interceptor.service.ts和server.ts中,并在共享服务中设置标志?interceptor本身就是一个服务。问题是server.ts文件位于根级别(即myapp/server.ts)和所有服务(包括公共服务)位于(myapp/src/app/interceptor.service.ts)内部。server.ts是角度组件吗?该组件有什么特殊之处吗?server.ts和interceptor是否共享同一个模块?您可以从服务器提供一个对象,并从拦截器修改其属性。您可以使用响应对象执行此操作,如下所示:
return next.handle(this.authReq).pipe(
    tap((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {
        this.reqArray.push(event.status);
        this.ss.nextValue(true); //change this part

      }
    }),
    catchError((error, caught) => {
      //intercept the response error and displace it to the console
      if (error instanceof HttpErrorResponse) {
        if(error.url.indexOf("detail?referenceCode=") == -1){
          this.reqArray.push(error.status);
          this.ss.nextValue(false) //change this part
        }
   })