代码之家  ›  专栏  ›  技术社区  ›  don

Angular Universal获取“找不到'[object object]'的ngmodule元数据”错误

  •  1
  • don  · 技术社区  · 6 年前

    我刚从5点更新到7点。在第5版中,我可以很好地进行服务器端渲染(SSR)。

    更新后,SSR失败,并出现以下错误(构建过程正常工作):

    Error: No NgModule metadata found for '[object Object]'.
    at NgModuleResolver.resolve (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:20015:27)
    at CompileMetadataResolver.getNgModuleMetadata (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:18657:47)
    at JitCompiler._loadModules (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:26060:55)
    at JitCompiler._compileModuleAndComponents (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:26041:40)
    at JitCompiler.compileModuleAsync (/app/node_modules/@angular/compiler/bundles/compiler.umd.js:26001:41)
    at CompilerImpl.compileModuleAsync (/app/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js:202:35)
    at /app/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:109:25
    at new ZoneAwarePromise (/app/node_modules/zone.js/dist/zone-node.js:910:29)
    at getFactory (/app/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:95:12)
    at View.engine (/app/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:74:13)
    

    鉴于此 SO question ,我没有使用 "bundleDependencies": "all" ,我甚至试图明确地陈述 none 在里面 angular.json 但是它没有解决这个问题。

    我正在使用此配置 贾森 :

       "server": {
          "builder": "@angular-devkit/build-angular:server",
          "options": {
            "outputPath": "dist/server",
            "main": "src/main.server.ts",
            "tsConfig": "src/tsconfig.server.json",
            "bundleDependencies": "none"
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "none"
            }
          }
        }
    

    我也懒得加载几乎所有的模块,如果这很重要的话(在第5版中没有):

    { path: 'private', loadChildren: '../private/private.module#PrivateModule', canActivate: [AuthGuardService], data: { state: 'private' } as RouteDataValues },
    {
      path: '', component: PageWithFooterComponent, children: [
        { path: 'about', loadChildren: '../about/about.module#AboutModule', data: { state: 'about' } as RouteDataValues },
        { path: 'instructions', loadChildren: '../instructions/instructions.module#InstructionsModule', data: { state: 'instructions' } as RouteDataValues },
        { path: '', component: HomeComponent, data: { state: 'home' } as RouteDataValues },
        { path: 'home', redirectTo: '/' },
        { path: '**', redirectTo: '/' }
      ]
    }
    

    我的服务器:

    import * as ngUniversal from '@nguniversal/express-engine';
    import * as compression from 'compression';
    import * as express from 'express';
    import * as path from 'path';
    import * as appServer from '../dist/server/main.js';
    // tslint:disable-next-line:no-require-imports
    require('zone.js/dist/zone-node');
    
    const ROOT = path.resolve();
    
    const app = express();
    
    // Server-side rendering
    function angularRouter(req, res): void {
      res.render('index', { req, res });
    }
    
    // Enable compression
    app.use(compression());
    
    // Root route before static files, or it will serve a static index.html, without pre-rendering
    app.get('/', angularRouter);
    
    // Serve the static files generated by the CLI (index.html, CSS? JS, assets...)
    app.use(express.static('client'));
    
    // Configure Angular Express engine
    app.engine('html', ngUniversal.ngExpressEngine({ bootstrap: appServer.AppServerModuleNgFactory }));
    app.set('view engine', 'html');
    app.set('views', path.join(ROOT, 'client'));
    
    app.listen(3000, () => {
      console.log('Listening on http://localhost:3000');
    });
    

    知道什么不管用吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   don    6 年前

    丢失了多个部分,这确实是延迟加载模块的问题。为了解决这个问题,我将服务器与 this one .

    添加到我的服务器的代码:

    import 'reflect-metadata';
    import 'zone.js/dist/zone-node';
    
    ...
    
    import { ngExpressEngine } from '@nguniversal/express-engine';
    import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
    
    ...
    
    const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('../dist/server/main.js');
    
    ...
    
    app.engine('html', ngExpressEngine({
      bootstrap: AppServerModuleNgFactory,
      providers: [
        provideModuleMap(LAZY_MODULE_MAP)
      ]
    }));