0

在 angular7 中创建一个简单的项目。它在开发模式下工作正常。

ng build --prod完成后,通过命令构建。

它生成带有资产和一些的dist文件夹index.htmlJS files

检查索引文件

在此处输入图像描述

如果在浏览器中打开,它只是白屏。

我所缺少的任何解决方案。

包.json

{
  "name": "app2",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "@ng-bootstrap/ng-bootstrap": "^4.0.2",
    "bootstrap": "^4.2.1",
    "core-js": "^2.5.4",
    "jquery": "^3.3.1",
    "ngx-swiper-wrapper": "^7.2.1",
    "popper": "^1.0.1",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.11.0",
    "@angular/cli": "~7.1.2",
    "@angular/compiler-cli": "~7.1.0",
    "@angular/language-service": "~7.1.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.6"
  }
}
4

2 回答 2

1

你总是需要一个服务器来运行 Angular 项目。当您在ng serve内部运行 Angular 时,会创建一个名为localhost.

您可以使用任何本地服务器iis来运行项目。

编辑:

如果你想使用,xampp你需要base href在文件夹index.html中进行更改。dist

STEP:1 复制app2里面生成的文件夹dist并粘贴进去xampp/htdocs

STEP:2 - xampp/htdocs/app2/<base href="/"> index.html 变成

<base href="./">

STEP:3打开浏览器并导航到http://localhost/app2

于 2019-02-06T12:15:48.963 回答
1

我假设这是因为浏览器尝试访问本地文件,这会向您的控制台抛出错误。您不能通过打开直接服务,index.html因为文件访问受到限制。

lite-server但是,您可以使用或使用一些创建静态服务器chrome extension.

[试试这个:https ://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb ]

另一种选择是修改浏览器目标快捷方式并添加--web-security-disabled --allow-file-access-from-files标志

阅读更多:如何在“--allow-file-access-from-files”模式下使用 Chrome 启动 html?

于 2019-02-06T12:14:02.370 回答