简体   繁体   English

将 Angular Universal 添加到现有的 Angular 14 应用程序

[英]Adding Angular Universal to an existing Angular 14 app

I am trying to install Angular Universal to my existing app via ng add @nguniversal/express-engine as per documentation but am running into a couple of issues:我正在尝试根据文档通过ng add @nguniversal/express-engine将 Angular Universal 安装到我现有的应用程序,但遇到了几个问题:

  1. When I run that command I'm getting that the detected compatible version is 7.0.1 : Found compatible package version: @nguniversal/express-engine@7.0.1.当我运行该命令时,我得到检测到的兼容版本是7.0.1Found compatible package version: @nguniversal/express-engine@7.0.1. which is a 4-year old version, whereas if I run that on a brand new, clean project I get version 14.1.0 .这是一个 4 年前的旧版本,而如果我在一个全新的、干净的项目上运行它,我会得到版本14.1.0 If I go ahead and use 7.0.1 then I get an error about id , $id or something like that not being supported and the command exits, so as a workaround I ran ng add @nguniversal/express-engine@14.1.0 which completed successfully but this leads to the following issues.如果我继续使用7.0.1 ,则会收到有关id$id或类似内容不受支持的错误,并且命令退出,因此作为解决方法,我运行了ng add @nguniversal/express-engine@14.1.0 ,其中成功完成,但这会导致以下问题。

  2. If I then try to run npm run dev:ssr I get this error: error TS2339: Property 'removeAllListeners' does not exist on type 'Window & typeof globalThis'.如果我随后尝试运行npm run dev:ssr我会收到此错误: error TS2339: Property 'removeAllListeners' does not exist on type 'Window & typeof globalThis'. which I was able to resolve by adding:我能够通过添加来解决:

declare global {
  interface Window {
    removeAllListeners: any;
  }
}

at the bottom of my app.module.ts file.在我的app.module.ts文件的底部。

  1. But I also get this other error:但我也遇到了另一个错误:
./node_modules/@angular/platform-server/fesm2015/platform-server.mjs:1372:27-53 - Error: export 'ɵinternalCreateApplication' (imported as 'ɵinternalCreateApplication') was not found in '@angular/core' (possible exports: ANALYZE_FOR_ENTRY_COMPONENTS, ANIMATION_MODULE_TYPE, APP_BOOTSTRAP_LISTENER, APP_ID, ........

I think this is probably related to the version mismatch thing when I first added Universal to my project but I don't really know where to go from here since I double checked and my dependencies versions are the same as a new, clean project.我认为这可能与我第一次将 Universal 添加到我的项目时版本不匹配的事情有关,但我真的不知道从这里去哪里,因为我仔细检查并且我的依赖项版本与一个新的、干净的项目相同。

This is my package.json :这是我的package.json

{
  "name": "test",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "node server/server.js",
    "start:dev": "ng serve",
    "start:docker": "ng serve --host 0.0.0.0 --disable-host-check",
    "build": "npx @angular/cli build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "docs:json": "compodoc -p ./tsconfig.json -e json -d .",
    "storybook": "npm run docs:json && start-storybook -p 6006",
    "build-storybook": "npm run docs:json && build-storybook",
    "dev:ssr": "ng run test:serve-ssr",
    "serve:ssr": "node dist/test/server/main.js",
    "build:ssr": "ng build && ng run test:server",
    "prerender": "ng run test:prerender"
  },
  "private": true,
  "dependencies": {
    "@angular-devkit/build-angular": "^14.0.5",
    "@angular/animations": "^14.0.0",
    "@angular/common": "^14.0.0",
    "@angular/compiler": "^14.0.0",
    "@angular/compiler-cli": "^14.0.0",
    "@angular/core": "^14.0.0",
    "@angular/forms": "^14.0.0",
    "@angular/google-maps": "^14.1.3",
    "@angular/platform-browser": "^14.0.0",
    "@angular/platform-browser-dynamic": "^14.0.0",
    "@angular/platform-server": "^14.0.0",
    "@angular/router": "^14.0.0",
    "@ng-select/ng-select": "^9.0.2",
    "@nguniversal/express-engine": "^14.1.0",
    "angular2-collapsible": "^0.8.0",
    "dayjs": "^1.11.4",
    "express": "^4.15.2",
    "express-http-proxy": "^1.6.3",
    "iotacss": "^1.6.0",
    "ngx-smart-modal": "^7.4.1",
    "rxjs": "~7.5.0",
    "swiper": "~8.1.0",
    "tslib": "^2.3.0",
    "typescript": "~4.7.2",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular/cli": "~14.0.5",
    "@babel/core": "^7.18.9",
    "@compodoc/compodoc": "^1.1.19",
    "@nguniversal/builders": "^14.1.0",
    "@storybook/addon-actions": "^6.5.9",
    "@storybook/addon-essentials": "^6.5.9",
    "@storybook/addon-interactions": "^6.5.9",
    "@storybook/addon-links": "^6.5.9",
    "@storybook/angular": "^6.5.9",
    "@storybook/builder-webpack5": "^6.5.9",
    "@storybook/manager-webpack5": "^6.5.9",
    "@storybook/testing-library": "^0.0.13",
    "@types/express": "^4.17.0",
    "@types/jasmine": "~4.0.0",
    "@types/node": "^14.15.0",
    "babel-loader": "^8.2.5",
    "jasmine-core": "~4.1.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.0.0",
    "karma-jasmine-html-reporter": "~1.7.0"
  }
}

So for some reason even though I had declared my dependencies the same way as a fresh project (currently 14.0.0 ), I had to explicitly define these four as:因此,出于某种原因,即使我以与新项目(当前14.0.0 )相同的方式声明我的依赖项,我也必须将这四个明确定义为:

"@angular/core": "^14.2.0",
"@angular/platform-browser": "^14.2.0",
"@angular/platform-browser-dynamic": "^14.2.0",
"@angular/platform-server": "^14.2.0",

It is working as expected now.它现在按预期工作。

I am exploring Angular Universal for server-side rendering (SSR) so that my social tags (SEO tags) will work properly.我正在探索用于服务器端渲染 (SSR) 的 Angular Universal,以便我的社交标签(SEO 标签)能够正常工作。 I also ran into these crazy problems.我也遇到了这些疯狂的问题。 Here are my steps to get things working.这是我使事情正常进行的步骤。

First, here are my tools and versions.首先,这是我的工具和版本。

  • Node v18.10.0节点 v18.10.0
  • NPM v8.19.2 NPM v8.19.2
  • ng-cli v14.2.10 ng-cli v14.2.10

Second, create a vanilla Angular application with ng-cli.其次,使用 ng-cli 创建一个普通的 Angular 应用程序。

ng new dummy

Now, I have no idea who's responsibility it is to add @angular/platform-server to package.json , but it needs to be added there manually.现在,我不知道谁负责将@angular/platform-server添加到package.json ,但需要手动将其添加到那里。 I would think that when you add Angular Universal, it will place that dependency in there, but that does not seem to be the case.我认为当你add Angular Universal 时,它会将依赖项放在那里,但事实似乎并非如此。

Third, go to package.json and add "@angular/platform-server": "^14.2.0" to the dependencies.第三,转到package.json并将"@angular/platform-server": "^14.2.0"添加到依赖项中。

Fourth, go ahead and install Angular Universal.第四,继续安装 Angular Universal。 If you do NOT specify a version, for some not-so-smart reason, v7.0.1 is installed and you get the id is used instead of $id for schema.如果您没有指定版本,出于某些不那么聪明的原因,v7.0.1 将被安装并且您将使用id而不是$id用于架构。 Since I was on Angular v14.2.0, I installed the corresponding Angular Universal version.由于我使用的是Angular v14.2.0,所以安装了对应的Angular Universal版本。

ng add @nguniversal/express-engine@14.2.0

When you install Angular Universal, a bunch of new files and configuration updates are done for you .当您安装 Angular Universal 时,会为您完成一堆新文件和配置更新 You should see an output like below.您应该会看到如下所示的输出。

The package @nguniversal/express-engine@14.2.0 will be installed and executed.
Would you like to proceed? Yes
✔ Packages successfully installed.
CREATE src/main.server.ts (677 bytes)
CREATE src/app/app.server.module.ts (318 bytes)
CREATE tsconfig.server.json (296 bytes)
CREATE server.ts (2022 bytes)
UPDATE package.json (1441 bytes)
UPDATE angular.json (5025 bytes)
UPDATE src/main.ts (537 bytes)
UPDATE src/app/app.module.ts (438 bytes)
UPDATE src/app/app-routing.module.ts (291 bytes)
✔ Packages installed successfully.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM