如何从服务器端向客户端注入值?

时间:2019-05-29 17:16:06

标签: angular angular-universal

我正在服务器上渲染一个有角度的应用程序,并且试图将字符串值注入到我的服务中,它在服务器上可以正常工作,但在浏览器上该值为null。有什么想法可以做到这一点吗?

server.ts:

renderModuleFactory(AppServerModuleNgFactory, {
  // Our index.html
  document: template,
  url: options.req.url,
  // DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
  extraProviders: [
    provideModuleMap(LAZY_MODULE_MAP),
    {provide: "clientIp", useValue: "129.112.28.171", deps: []}
  ]
})

服务:

@Injectable({
   providedIn: 'root'
})
export class GeoService {
    constructor(@Inject("clientIp") @Optional() protected clientIp: string) {
   // clientIp is "129.112.28.171" on the server but null on the client/browser
   }
}

如何在浏览器中访问值?

0 个答案:

没有答案