在服务器端渲染设置

时间:2018-05-29 13:58:47

标签: angular typescript server serverside-rendering

在angular6中成功设置服务器端渲染后,它正在构建并运行为SSR,然后应用程序停止作为基本应用程序工作,基本上我想将{angular}应用程序作为npm run start运行,但是,当我运行此命令我收到此错误,似乎没有相关的文档。

该应用程序构建并用作服务器端呈现,但不是常规应用程序。

这是我得到的错误:

  

ITWS08:A6-Test-3-SSR CC $ npm运行开始      angular@6.0.0 start / eclipse / A6-Test-3-SSR      服务

     

无法确定“服务”目标的单个项目。     错误:无法确定“服务”目标的单个项目。      在ServeCommand.getProjectNamesByTarget(/eclipse/A6-Test-3-SSR/node_modules/@angular/cli/models/architect-command.js:175:19)                         在MergeMapSubscriber._loadWorkspaceAndArchitect.pipe.operators_1.concatMap   [作为项目]   (/eclipse/A6-Test-3-SSR/node_modules/@angular/cli/models/architect-command.js:50:43)                         在MergeMapSubscriber._tryNext(/eclipse/A6-Test-3-SSR/node_modules/rxjs/internal/operators/mergeMap.js:122:27)                         在MergeMapSubscriber._next(/eclipse/A6-Test-3-SSR/node_modules/rxjs/internal/operators/mergeMap.js:112:18)                         在MergeMapSubscriber.Subscriber.next(/eclipse/A6-Test-3-SSR/node_modules/rxjs/internal/Subscriber.js:103:18)                         在TapSubscriber._next(/eclipse/A6-Test-3-SSR/node_modules/rxjs/internal/operators/tap.js:109:26)                         在TapSubscriber.Subscriber.next(/eclipse/A6-Test-3-SSR/node_modules/rxjs/internal/Subscriber.js:103:18)                         在MergeMapSubscriber.notifyNext(/eclipse/A6-Test-3-SSR/node_modules/rxjs/internal/operators/mergeMap.js:141:26)                         在InnerSubscriber._next(/eclipse/A6-Test-3-SSR/node_modules/rxjs/internal/InnerSubscriber.js:30:21)                         在InnerSubscriber.Subscriber.next(/eclipse/A6-Test-3->SSR/node_modules/rxjs/internal/Subscriber.js:103:18)

     

npm ERR!代码ELIFECYCLE npm ERR!错误1       错误的ERR! angular@6.0.0 start:ng serve       错误的ERR!退出状态1       错误的ERR!       错误的ERR!在angular@6.0.0启动脚本时失败。错误的ERR!这可能不是npm的问题。可能有额外的   记录输出上面。

     

npm ERR!可以在以下位置找到此运行的完整日志:      错误的ERR! /Users/CC/.npm/_logs/2018-05-29T13_46_21_934Z-debug.log   ITWS08:A6-Test-3-SSR CC $

这些错误未在构建中显示为服务器端呈现

有什么想法吗?

似乎在angular-cli.json上有问题

如果我在没有服务器端渲染配置的情况下替换angular-cli.json,该应用程序正常工作但无法构建SSR

            {
              "$schema": "./node_modules/@angular-devkit/core/src/workspace/workspace-schema.json",
              "version": 1,
              "newProjectRoot": "projects",
              "projects": {
                "angular.io-example": {
                  "root": "",
                  "projectType": "application",
                  "prefix": "app",
                  "architect": {
                    "build": {
                      "builder": "@angular-devkit/build-angular:browser",
                      "options": {
                        "outputPath": "dist",
                        "index": "src/index.html",
                        "main": "src/main.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "src/tsconfig.app.json",
                        "assets": [
                          {
                            "glob": "favicon.ico",
                            "input": "src",
                            "output": "/"
                          },
                          {
                            "glob": "**/*",
                            "input": "src/assets",
                            "output": "/assets"
                          }
                        ],
                        "styles": [
                          {"input": "node_modules/font-awesome/css/font-awesome.css"},
                          {"input": "src/assets/css/flag-icon.css"},
                          {"input": "src/assets/css/bootstrap.css"},
                          {"input": "src/assets/css/xmas.css"},
                          {"input": "src/assets/css/styles.css"},             
                          {"input": "src/assets/css/browser-specific.css"}
                        ],
                        "scripts": [
                          {"input": "node_modules/hammerjs/hammer.js"}
                        ]
                      },
                      "configurations": {
                        "production": {
                          "fileReplacements": [
                            {
                              "replace": "src/environments/environment.ts",
                              "with": "src/environments/environment.prod.ts"
                            }
                          ],
                          "optimization": true,
                          "outputHashing": "all",
                          "sourceMap": false,
                          "extractCss": true,
                          "namedChunks": false,
                          "aot": true,
                          "extractLicenses": true,
                          "vendorChunk": false,
                          "buildOptimizer": true
                        }
                      }
                    },
                    "serve": {
                      "builder": "@angular-devkit/build-angular:dev-server",
                      "options": {
                        "browserTarget": "angular.io-example:build"
                      },
                      "configurations": {
                        "production": {
                          "browserTarget": "angular.io-example:build:production"
                        }
                      }
                    },
                    "extract-i18n": {
                      "builder": "@angular-devkit/build-angular:extract-i18n",
                      "options": {
                        "browserTarget": "angular.io-example:build"
                      }
                    },
                    "test": {
                      "builder": "@angular-devkit/build-angular:karma",
                      "options": {
                        "main": "src/test.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "src/tsconfig.spec.json",
                        "karmaConfig": "src/karma.conf.js",
                        "styles": [
                          {
                            "input": "styles.css"
                          }
                        ],
                        "scripts": [],
                        "assets": [
                          {
                            "glob": "favicon.ico",
                            "input": "src/",
                            "output": "/"
                          },
                          {
                            "glob": "**/*",
                            "input": "src/assets",
                            "output": "/assets"
                          }
                        ]
                      }
                    },
                    "lint": {
                      "builder": "@angular-devkit/build-angular:tslint",
                      "options": {
                        "tsConfig": [
                          "src/tsconfig.app.json",
                          "src/tsconfig.spec.json"
                        ],
                        "exclude": [
                          "**/node_modules/**"
                        ]
                      }
                    }
                  }
                }
              }
            }

将angular-cli.json更改为服务器端配置SSR构建完美,并且作为SSR完美但不能正常工作,我想知道如何使两个世界都能正常工作

            {
              "$schema": "./node_modules/@angular-devkit/core/src/workspace/workspace-schema.json",
              "version": 1,
              "newProjectRoot": "projects",
              "projects": {
                "angular.io-example": {
                  "root": "",
                  "projectType": "application",
                  "prefix": "app",
                  "architect": {
                    "build": {
                      "builder": "@angular-devkit/build-angular:browser",
                      "options": {
                        "outputPath": "dist/browser",
                        "index": "src/index.html",
                        "main": "src/main.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "src/tsconfig.app.json",
                        "assets": [
                          {
                            "glob": "favicon.ico",
                            "input": "src",
                            "output": "/"
                          },
                          {
                            "glob": "**/*",
                            "input": "src/assets",
                            "output": "/assets"
                          }
                        ],
                        "styles": [
                          {"input": "node_modules/font-awesome/css/font-awesome.css"},
                          {"input": "src/assets/css/flag-icon.css"},
                          {"input": "src/assets/css/bootstrap.css"},
                          {"input": "src/assets/css/xmas.css"},
                          {"input": "src/assets/css/styles.css"},             
                          {"input": "src/assets/css/browser-specific.css"}
                        ],
                        "scripts": [
                          {"input": "node_modules/hammerjs/hammer.js"}
                        ]
                      },
                      "configurations": {
                        "production": {
                          "fileReplacements": [
                            {
                              "replace": "src/environments/environment.ts",
                              "with": "src/environments/environment.prod.ts"
                            }
                          ],
                          "optimization": true,
                          "outputHashing": "all",
                          "sourceMap": false,
                          "extractCss": true,
                          "namedChunks": false,
                          "aot": true,
                          "extractLicenses": true,
                          "vendorChunk": true,
                          "buildOptimizer": true
                        }
                      }
                    },
                    "server": {
                      "builder": "@angular-devkit/build-angular:server",
                      "options": {
                        "outputPath": "dist/server",
                        "main": "src/main.server.ts",
                        "tsConfig": "src/tsconfig.server.json"
                      }
                    },
                    "extract-i18n": {
                      "builder": "@angular-devkit/build-angular:extract-i18n",
                      "options": {
                        "browserTarget": "angular.server:build"
                      }
                    },
                    "test": {
                      "builder": "@angular-devkit/build-angular:karma",
                      "options": {
                        "main": "src/test.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "src/tsconfig.spec.json",
                        "karmaConfig": "src/karma.conf.js",
                        "styles": [
                          {
                            "input": "styles.css"
                          }
                        ],
                        "scripts": [],
                        "assets": [
                          {
                            "glob": "favicon.ico",
                            "input": "src/",
                            "output": "/"
                          },
                          {
                            "glob": "**/*",
                            "input": "src/assets",
                            "output": "/assets"
                          }
                        ]
                      }
                    },
                    "lint": {
                      "builder": "@angular-devkit/build-angular:tslint",
                      "options": {
                        "tsConfig": [
                          "src/tsconfig.app.json",
                          "src/tsconfig.spec.json"
                        ],
                        "exclude": [
                          "**/node_modules/**"
                        ]
                      }
                    }
                  }
                }
              }
            }

2 个答案:

答案 0 :(得分:0)

我有同样的错误。我的问题是app项目的名称和lib项目的名称。当我更改了lib的名称时,错误消失了,我也可以使用ng serve

启动我的项目

angular.json:

 "projects": {
    "project-name": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "pn",
      ...
    } 
    "project-name-lib": {
      "root": "projects/project-name-lib",
      "sourceRoot": "projects/project-name-lib/src",
      "projectType": "library",
      "prefix": "pn",
      ...
  }

答案 1 :(得分:0)

保留"serve"文件中的"server"angular.json配置。这样,您将能够在本地运行应用程序以及服务器端渲染。希望能解决您的问题。