ERROR TypeError:无法分配给只读属性' stopImmediatePropagation'对象' [对象]'

时间:2018-06-16 20:14:58

标签: angular typescript angular6 angular-universal

我正在尝试将angular universal集成到我的angular6项目中。我按照文档(以及同一页面上的角度通用的示例项目,https://angular.io/guide/universal)。

构建成功没有任何问题,也运行良好。但是当我调用localhost:4000时,会抛出此错误,

TypeError: Cannot assign to read only property 'stopImmediatePropagation' of object '[object Object]'
at DomEventsPlugin.patchEvent (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:84969:50)
at new DomEventsPlugin (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:84956:15)
at /home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:60393:1533
at _callFactory (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:13158:28)
at _createProviderInstance$1 (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:13104:26)
at resolveNgModuleDep (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:13067:21)
at _createClass (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:13131:29)
at _createProviderInstance$1 (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:13101:26)
at resolveNgModuleDep (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:13067:21)
at _createClass (/home/kushalgbk/Desktop/savemonk/savemonkWebsite/Savemonk-Mobile-Website-V2.0/dist/server.js:13137:32)

server.ts

// These are important and needed before anything else
import 'zone.js/dist/zone-node';
import 'reflect-metadata';

import { enableProdMode } from '@angular/core';

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

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

// Express Engine
import { ngExpressEngine } from '@nguniversal/express-engine';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));

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

// TODO: implement data requests securely
app.get('/api/*', (req, res) => {
  res.status(404).send('data requests are not supported');
});

// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));

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

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

如果需要,我可以发布任何其他文件。

AppServerModule

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

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

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ModuleMapLoaderModule
  ],
  providers: [
    // Add universal-only providers here
  ],
  bootstrap: [ AppComponent ],
})
export class AppServerModule {}

3 个答案:

答案 0 :(得分:1)

看起来平台服务器和降级/核心版本降级之间存在冲突(请确保两者是相同的版本。)将平台服务器升级到您的降级/核心版本可能会帮助您解决问题。

答案 1 :(得分:0)

目前看来,最简单的解决方案是从angular universal-starter克隆仓库  并在其中包含项目的所有文件。另外,您将需要进行一些调整(例如,相关性,路线,路径)。我走了,这解决了这个问题。

答案 2 :(得分:0)

官方文档中没有提及在app.server.module.ts中添加ServerTransferStateModule 这有所作为。 请遵循此示例项目。

https://github.com/angular/universal-starter

此外,github问题链接(解决后我关闭了)

https://github.com/angular/angular-cli/issues/11273