gpt4 book ai didi

javascript - Angular 通用获取错误 : You must pass in a NgModule or NgModuleFactory to be bootstrapped

转载 作者:太空狗 更新时间:2023-10-29 16:54:37 25 4
gpt4 key购买 nike

我按照 this guide 将我现有的 angular-cli 应用程序转换为 angular-universal .

可以看看我的完整源码here .

我能够同时构建浏览器和客户端项目,但在浏览器中查看应用程序时出现以下错误:

Error: You must pass in a NgModule or NgModuleFactory to be bootstrapped at View.engine (D:\ng-ssr-demo\dist\server.js:359545:23)

问题出在我的 server.ts 文件中,其中 AppServerModuleNgFactory 未定义,并且由于此工厂用于在 express 后端引导应用程序,因此引导失败.

./server.ts:

const MockBrowser = require('mock-browser').mocks.MockBrowser;
const mock = new MockBrowser();

// 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(), 'dist');

// Fix for window error:
const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.resolve('./', 'dist', 'browser/', 'index.html')).toString();
const win = domino.createWindow(template);

// workaround for leaflet
global['window'] = win;
global['document'] = win.document;

// workaround for nex-charts
win.screen = { deviceXDPI: 0, logicalXDPI: 0 };
global['MouseEvent'] = win.MouseEvent;
global['navigator'] = mock.getNavigator();


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

// AppServerModuleNgFactory is undefined
console.log('AppServerModuleNgFactory', AppServerModuleNgFactory);

// This is injected
console.log('LAZY_MODULE_MAP', LAZY_MODULE_MAP);

// 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)
]
}));

./webpack.server.config.js:

module.exports = {
entry: {
// This is our Express server for Dynamic universal
server: './server.ts',
// This is an example of Static prerendering (generative)
prerender: './prerender.ts'
},
target: 'node',
resolve: { extensions: ['.ts', '.js'] },
// Make sure we include all node_modules etc
externals: [/node_modules/],
output: { path: path.join(__dirname, 'dist'), filename: '[name].js' },
module: { rules: [{ test: /\.ts$/, loader: 'ts-loader'}] },
plugins: [
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, 'src'), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?express(\\|\/)(.+)?/,
path.join(__dirname, 'src'), {}
)
]
}

./src/tsconfig.server.json:

{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "commonjs",
"baseUrl": "./",
"types": ["node"],
"typeRoots": ["../node_modules/@types"],
"paths": {
"@angular/*": [
"../node_modules/@angular/*"
],
"@nebular/*": [
"../node_modules/@nebular/*"
]
}
},
"exclude": [
"test.ts",
"**/*.spec.ts"
],
"angularCompilerOptions": {
"entryModule": "app/app.server.module#AppServerModule",
"platform": 1
}
}

./src/main.server.ts:

export { AppServerModule } from './app/app.server.module';

./src/app/app.module.ts:

@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule.withServerTransition({appId: 'my-app'}),
BrowserAnimationsModule,
HttpModule,
AppRoutingModule,

NgbModule.forRoot(),
ThemeModule.forRoot(),
CoreModule.forRoot(),
environment.production ? ServiceWorkerModule.register('./ngsw-worker.js') : [],
],
bootstrap: [AppComponent],
providers: [
{ provide: APP_BASE_HREF, useValue: '/' }, WebWorkerService,
],
})
export class AppModule {
}

./src/app/app.server.module.ts:

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

最佳答案

ANGULAR 8 更新(v8.0.1 - 截至 2019 年 6 月)

对我来说,我使用 Angular CLI 运行了通用设置,但它并没有直接运行。经过数小时的阅读后,我发现包裹不匹配。我让 Angular 8 运行该项目,但我的 package.json 中的 nguniversal 包是在 v7 指定的。

我建议将它们更新为您安装的相同版本的 Angular。 CLI 应该默认执行此操作,但我猜(还没有?)。

对于 Angular 8,在撰写本文时(2019 年 6 月)这是版本 @next@8.0.0-rc.1,因此请运行以下命令更新命令:

npm i --save @nguniversal/express-engine@next @nguniversal/module-map-ngfactory-loader@next

更新后我仍然收到错误,并设法确定了另一个问题。我还必须为服务器端应用程序关闭 Ivy 编译器。为此,我将以下行添加到 tsconfig.server.json:

{
"extends": "./tsconfig.app.json",
...
"angularCompilerOptions": {
...
"enableIvy": false
}
...
}

Ivy 在 Angular 8 中默认关闭,但是因为我的 tsconfig.server.json 扩展了 tsconfig.app.json,并且应用配置打开了 Ivy,我必须为服务器配置明确关闭它。

在这一切之后,服务器对内容的请求实际上开始为我工作。

如果这对您没有帮助,我建议您下载 Angular 文档中提到的通用示例项目:

下载:https://angular.io/generated/zips/universal/universal.zip文档:https://angular.io/guide/universal

下载后,比较所有相关文件以确保您拥有相同的文件。如果您在自己的项目中仍然遇到错误,但该示例正在运行,那么,请尝试将您的设置文件、模块和组件等一个一个地移动到示例项目中,看看是什么破坏了它。这就是我如何能够确定是我的 tsconfig.server.json 文件破坏了它。

关于javascript - Angular 通用获取错误 : You must pass in a NgModule or NgModuleFactory to be bootstrapped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49850267/

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