ngx-cookie-服务器端渲染中的空白cookie对象

时间:2019-06-12 11:33:21

标签: javascript angular cookies angular-universal

我有一个角度应用程序,其中启用了角度通用性,我正在从API渲染数据,但其中包括cookie的工作。在检查用户浏览器Cookie中的值后,我进行了API调用。

但是对于Angular SSR(服务器端渲染),没有cookie,尝试使用此{}时,我得到console.log(this.cookie.getAll());个空白对象。

我正在使用ngx-cookie软件包,我的安装是这个

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { SlickCarouselModule } from 'ngx-slick-carousel';
import { SharedModule } from './shared/shared.module';
import { CookieModule } from 'ngx-cookie';

import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { CommonModule } from '@angular/common';
import { TransferHttpCacheModule } from '@nguniversal/common';
import { NgtUniversalModule } from '@ng-toolkit/universal';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    CookieModule.forRoot(),
    AppRoutingModule,
    HttpClientModule,
    CommonModule,
    TransferHttpCacheModule,
    NgtUniversalModule,
    SlickCarouselModule,
    SharedModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.server.module.ts

import { NgModule } from '@angular/core';
import { ServerModule, ServerTransferStateModule } from '@angular/platform-server';
import { CookieService, CookieBackendService } from 'ngx-cookie';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';
import { SharedModule } from './shared/shared.module';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule,
    ServerTransferStateModule,
    SharedModule
  ],
  bootstrap: [AppComponent],
  providers: [{ provide: CookieService, useClass: CookieBackendService }]
})
export class AppServerModule {}

server.ts

我已经应用了

app.get('*', (req, res) => {
  res.render('index', {
    req: req,
    res: res,
    providers: [
      {
        provide: 'REQUEST', useValue: (req)
      },
      {
        provide: 'RESPONSE', useValue: (res)
      }
    ]
  });
});

仍然我只能在浏览器中访问cookie,而不能访问服务器端。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用Interceptor将所有请求的Cookie发送到服务器
这是一个使用方法的示例:

intercreptor.service.ts

import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';

import { Observable } from 'rxjs';
import { CookieService } from 'ngx-cookie';

@Injectable()

export class InterceptorService implements HttpInterceptor {

  constructor(private cookies: CookieService) { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    let token = this.cookies.get("my-access-token");

   if(token) req = req.clone({
      setHeaders: { Authorization: `Bearer ${token}`}
    });

    return next.handle(req)
  }

}

intercreptor.module.ts

import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { InterceptorService } from './interceptors/authorization';

@NgModule({
  imports: [
    HttpClientModule
  ],
  providers: [{
      provide: HTTP_INTERCEPTORS,
      useClass: InterceptorService,
      multi: true
   }]
})

export class InterceptorModule { }

并将InterceptorModule导入到您的app.module.ts