gpt4 book ai didi

Angular Universal 不在路由器导出内呈现内容

转载 作者:太空狗 更新时间:2023-10-29 18:00:13 25 4
gpt4 key购买 nike

我正在尝试使用此链接中的 Angular Universal 通过 SSR 呈现我的 Angular 7 应用程序 https://angular.io/guide/universal .我以为我可以从那里开始一切工作,因为我能够查看页面源代码并看到呈现给页面的 app.component 内部的所有内容。但是,我无法呈现 router-outlet 标签内的任何路由

我只是简单地剥离了我的应用程序,使其完全按照文档中的说明进行操作。我在那里读到一些关于确保 ModuleMapLoaderModule 位于 app.server.module 内部以便延迟加载工作的内容。这确实是通用的 angular-cli 命令的默认设置,所以我不确定发生了什么。

app.server.module

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';

import { AppComponent } from './app.component';
import { AppModule } from './app.module';

@NgModule({
imports: [
AppModule,
ServerModule,
ModuleMapLoaderModule,
],
bootstrap: [AppComponent],
})
export class AppServerModule {}

tsconfig.server.json

  "extends": "./tsconfig.app.json",
"compilerOptions": {
"outDir": "../out-tsc/app-server",
},
"angularCompilerOptions": {
"entryModule": "app/app.server.module#AppServerModule"
}
}

server.ts

import 'zone.js/dist/zone-node';
import { enableProdMode } from '@angular/core';
// Express Engine
import { ngExpressEngine } from '@nguniversal/express-engine';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

import * as express from 'express';
import { join } from 'path';

// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// Express server
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'browser');

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');

// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
app.engine('html', ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP)
]
}));

app.set('view engine', 'html');
app.set('views', DIST_FOLDER);

// Serve static files from /browser
app.get('*.*', express.static(DIST_FOLDER, {
maxAge: '1y'
}));

// All regular routes use the Universal engine
app.get('*', (req, res) => {
res.render(join(DIST_FOLDER, 'index.html'), { req });
});

// Start up the Node server
app.listen(PORT, () => {
console.log(`Node Express server listening on http://localhost:${PORT}`);
});

渲染输出

<html lang="en">

<head>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- ...Rendered CSS styles... -->
<body>
<app-root _nghost-sc0="" ng-version="7.2.15">
<!-- ...Header code that has been rendered... -->

<router-outlet _ngcontent-sc0=""></router-outlet> <!-- This is where I am expecting the content of the route to be -->

<!-- ...Footer code that has been rendered... -->
</app-root>
</body>

</html>

app.routing.module.ts

// Angular Imports

// Pages
// ...Page Imports..

const routes: Routes = [
{ path: '', component: HomePageComponent, pathMatch: 'full' },
{ path: 'events/:id', component: EventDetailPageComponent }, // This is the detail page I am testing with
{ path: 'unknown-error', component: UnknownErrorPageComponent },
{ path: '404', component: NotFoundPageComponent },
{ path: '**', redirectTo: '404' }
];

@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(routes),
],
exports: [
RouterModule,
]
})
export class AppRoutingModule { }

在尝试为 Angular 7 找到准确的文档时,我的运气基本上为零,非常感谢任何帮助!谢谢

最佳答案

尝试添加 relativeLinkResolution: 'legacy'

@NgModule({
imports: [RouterModule.forRoot(routes, {
relativeLinkResolution: 'legacy'
})],
exports: [RouterModule]
})
export class AppRoutingModule {
}

它对我有帮助。

关于Angular Universal 不在路由器导出内呈现内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56368564/

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