gpt4 book ai didi

angular - 在 Angular Universal 中获取主机名和路径名信息

转载 作者:太空狗 更新时间:2023-10-29 17:48:15 24 4
gpt4 key购买 nike

这里有人知道如何访问 Angular universal 中的主机名和路径名信息吗?

在客户端应用程序中,您可以访问包含该信息的窗口对象。

我只需要服务器上的信息?

最佳答案

ngExpressEngine :

server.ts:

...
app.engine('html', (_, options, callback) => {
let engine = ngExpressEngine({
bootstrap: ServerAppModule,
providers: [ { provide: 'host', useFactory: () => options.req.get('host') } ]
});

engine(_, options, callback)
})
...

app.component.ts:

import { Inject, Injector, PLATFORM_ID } from '@angular/core';
import { DOCUMENT, isPlatformServer } from '@angular/common';

...

constructor(@Inject(DOCUMENT) private document, private injector: Injector, @Inject(PLATFORM_ID) private platformId: Object) {
if (isPlatformServer(this.platformId)) {
console.log(this.injector.get('host'))
} else {
console.log('document: ', document.location.hostname);
}
}

没有 ngExpressEngine:

server.ts:

...
app.engine('html', (_, options, callback) => {
const opts = {
document: template,
url: options.req.url,
extraProviders: [{ provide: 'host', useFactory: () => options.req.get('host') }]
};
renderModuleFactory(AppServerModuleNgFactory, opts)
.then(html => callback(null, html));
});
...

关于angular - 在 Angular Universal 中获取主机名和路径名信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44460313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com