当我们尝试移入Angular时,我们正在使用ngUpgrade一起运行AngularJS和Angular 6。我们没有任何特定于Angular的逻辑,它们都存在于AngularJS下。这确实很好,但是我希望我们可以运行Angular Universal,以便各种爬网程序都能正确读取页面,特别是Facebook等社交媒体网站的元标记。我没有在documentation中看到任何内容,表明它不适用于ngUpgrade / angularJS
我似乎已经足够正确地编译了它,但是一旦它进入了angularJS,它就会失败,因为找不到angular
。由于这里的腿很多,我会尽量保持冗长
这是我的main.ts
:
import { AppModule } from './app/app.module';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import * as _angular_ from 'angular';
declare global {
const angular: typeof _angular_;
}
还有我的app.module.ts
import 'reflect-metadata'
import 'zone.js'
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {UpgradeModule} from '@angular/upgrade/static';
import {HttpClientModule} from "@angular/common/http";
@NgModule({
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
UpgradeModule,
HttpClientModule
]
})
export class AppModule {
constructor(private upgrade: UpgradeModule) { }
ngDoBootstrap() {
this.upgrade.bootstrap(document.body, ['myproject']);
}
}
我认为可能与angular
中没有提到main.server.ts
有关,所以我将declare
代码从main.ts
移到了main.server.ts
,但没有任何改变。作为参考,我的main.server.ts
仅包含:
export { AppServerModule } from './app/app.server.module';
在运行ng add @nguniversal/express-engine --clientProject myproject
之后,还没有真正触摸过任何其他文件,因此它生成/修改的所有其他文件都完全相同。
作为参考,ng serve
仍然可以正常运行/可以正确加载angularJS应用程序。
错误本身(匿名)为:
C:\Users\myname\PycharmProjects\myproject\dist\server.js:128985
angular.module('myproject')
^
ReferenceError: angular is not defined
at Object../src/app/app.module.ts (C:\Users\myname\PycharmProjects\myproject\dist\server.js:128985:1)
at __webpack_require__ (C:\Users\myname\PycharmProjects\myproject\dist\server.js:128865:30)
at Object../src/app/app.server.module.ngfactory.js (C:\Users\myname\PycharmProjects\myproject\dist\server.js:129023:11)
at __webpack_require__ (C:\Users\myname\PycharmProjects\myproject\dist\server.js:128865:30)
at Object../src/main.server.ts (C:\Users\myname\PycharmProjects\myproject\dist\server.js:129203:37)
at __webpack_require__ (C:\Users\myname\PycharmProjects\myproject\dist\server.js:128865:30)
at Object.0 (C:\Users\myname\PycharmProjects\myproject\dist\server.js:129219:18)
at __webpack_require__ (C:\Users\myname\PycharmProjects\myproject\dist\server.js:128865:30)
at ./src/app/account.service.ts.Object.defineProperty.value (C:\Users\myname\PycharmProjects\myproject\dist\server.js:128914:18)
at Object.<anonymous> (C:\Users\myname\PycharmProjects\myproject\dist\server.js:128917:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! myproject@0.0.0 serve:ssr: `node dist/server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the myproject@0.0.0 serve:ssr script.