构建 angular 应用程序并放置在服务器上
Building angular application and putting on the server
我正在开发一个 angular6 应用程序,我想生成一个构建以在我的服务器上进行测试,目前我使用 ng 服务器并且它 运行 在我的浏览器上工作而没有产生任何错误。
c:\Users\emiry\Desktop\Angular\Projects\StartingNewProject
当我 运行 ng build
命令时,它为 /dist 文件夹生成一个构建并且 returns 没有错误
在我的机器上我已经安装了 wamp64
所以我得到这个由 ng 生成的构建并将它放在我的 www 文件夹中
C:\wamp64\www\StartingNewProject
仅此就足以让我能够 运行 我的应用程序在服务器上吗?
当我尝试通过 http:// localhost/startingnewproject/
访问我在服务器上的应用程序时
它returns我在浏览器控制台中出现以下错误
Failed to load resource: the server responded with a status of 404
(Not Found) polyfills.js:1 Failed to load resource: the server
responded with a status of 404 (Not Found) styles.js:1 Failed to load
resource: the server responded with a status of 404 (Not Found)
vendor.js:1 Failed to load resource: the server responded with a
status of 404 (Not Found) main.js:1 Failed to load resource: the
server responded with a status of 404 (Not Found)
angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"StartingNewProject": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/StartingNewProject",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css",
{
"input": "./node_modules/bootstrap/dist/css/bootstrap.css"
}
],
"scripts": []
},
"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": "StartingNewProject:build"
},
"configurations": {
"production": {
"browserTarget": "StartingNewProject:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "StartingNewProject: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": [
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"StartingNewProject-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "StartingNewProject:serve"
},
"configurations": {
"production": {
"devServerTarget": "StartingNewProject:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "StartingNewProject"
}
package.json
{
"name": "starting-new-project",
"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": "^6.0.3",
"@angular/common": "^6.0.3",
"@angular/compiler": "^6.0.3",
"@angular/core": "^6.0.3",
"@angular/forms": "^6.0.3",
"@angular/http": "^6.0.3",
"@angular/platform-browser": "^6.0.3",
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/router": "^6.0.3",
"@ng-bootstrap/ng-bootstrap": "^2.0.0-alpha.0",
"@ng-bootstrap/schematics": "^2.0.0-alpha.1",
"bootstrap": "^4.0.0",
"core-js": "^2.5.4",
"jshint": "^2.9.5",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.3",
"@angular-devkit/build-angular": "~0.6.8",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.8",
"@angular/language-service": "^6.0.3",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
}
}
我尝试访问如下
file:///C:/wamp64/www/StartingNewProject/index.html
5index.html:13 GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET
file:///C:/favicon.ico 0 ()
translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00:1
GET
file://translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00
0 ()
file:///C:/wamp64/www/StartingNewProject/index.html
GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET
file:///C:/favicon.ico 0 ()
当我尝试访问根目录以查看文件夹和文件时,我完全可以,问题是当我加载 index.html
该项目存在的问题是试图找到它的依赖项。 Angular 项目的 index.html
文件应包含如下所示的标签:
<base href="/">
或类似。这意味着,当它查找依赖项时,它会查找相对于根的关系。在你的情况下,根目录旁边没有(即本地主机),它们在 StartingNewProject
下
您可以使用 ng build --prod --base-href=/StartingNewProject/
生成一个新版本,它会为您添加基本 href 标签。请注意,在此处包含前导斜线和尾随斜线很重要。
可以在 Angular 文档中找到更多详细信息,here
我遇到了同样的问题。我使用 ./
.
解决了这个问题
<base href="/"> to <base href="./">
再次构建和部署。
使用
时,您可能会收到带有 Angular 11 的“已弃用”警告
ng build --prod --base-href=/StartingNewProject/
您可以改为调整 angular.json。
在 angular.json 文件中,像这样创建一个“baseHref”属性:
"projects": {
"your-project-name" : {
"architect": {
"build": {
"options": {
"baseHref": "/your-relative-path/"
这将改变生产和开发中的 URL。
请注意,在您的链接中,请始终使用这样的相对路径(而不是“/”):
<img class="top-bar__logo" src="./assets/images/picture1.svg" />
我正在开发一个 angular6 应用程序,我想生成一个构建以在我的服务器上进行测试,目前我使用 ng 服务器并且它 运行 在我的浏览器上工作而没有产生任何错误。
c:\Users\emiry\Desktop\Angular\Projects\StartingNewProject
当我 运行 ng build
命令时,它为 /dist 文件夹生成一个构建并且 returns 没有错误
在我的机器上我已经安装了 wamp64
所以我得到这个由 ng 生成的构建并将它放在我的 www 文件夹中
C:\wamp64\www\StartingNewProject
仅此就足以让我能够 运行 我的应用程序在服务器上吗?
当我尝试通过 http:// localhost/startingnewproject/
访问我在服务器上的应用程序时它returns我在浏览器控制台中出现以下错误
Failed to load resource: the server responded with a status of 404 (Not Found) polyfills.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) styles.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) vendor.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) main.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"StartingNewProject": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/StartingNewProject",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css",
{
"input": "./node_modules/bootstrap/dist/css/bootstrap.css"
}
],
"scripts": []
},
"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": "StartingNewProject:build"
},
"configurations": {
"production": {
"browserTarget": "StartingNewProject:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "StartingNewProject: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": [
"src/styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"StartingNewProject-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "StartingNewProject:serve"
},
"configurations": {
"production": {
"devServerTarget": "StartingNewProject:serve:production"
}
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "StartingNewProject"
}
package.json
{
"name": "starting-new-project",
"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": "^6.0.3",
"@angular/common": "^6.0.3",
"@angular/compiler": "^6.0.3",
"@angular/core": "^6.0.3",
"@angular/forms": "^6.0.3",
"@angular/http": "^6.0.3",
"@angular/platform-browser": "^6.0.3",
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/router": "^6.0.3",
"@ng-bootstrap/ng-bootstrap": "^2.0.0-alpha.0",
"@ng-bootstrap/schematics": "^2.0.0-alpha.1",
"bootstrap": "^4.0.0",
"core-js": "^2.5.4",
"jshint": "^2.9.5",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.3",
"@angular-devkit/build-angular": "~0.6.8",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.8",
"@angular/language-service": "^6.0.3",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
}
}
我尝试访问如下
file:///C:/wamp64/www/StartingNewProject/index.html
5index.html:13 GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET file:///C:/favicon.ico 0 () translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00:1 GET file://translate.google.com/gen204?nca=te_li&client=te_lib&logld=vTE_20180625_00 0 ()
file:///C:/wamp64/www/StartingNewProject/index.html
GET file:///C:/runtime.js 0 () /C:/favicon.ico:1 GET file:///C:/favicon.ico 0 ()
当我尝试访问根目录以查看文件夹和文件时,我完全可以,问题是当我加载 index.html
该项目存在的问题是试图找到它的依赖项。 Angular 项目的 index.html
文件应包含如下所示的标签:
<base href="/">
或类似。这意味着,当它查找依赖项时,它会查找相对于根的关系。在你的情况下,根目录旁边没有(即本地主机),它们在 StartingNewProject
您可以使用 ng build --prod --base-href=/StartingNewProject/
生成一个新版本,它会为您添加基本 href 标签。请注意,在此处包含前导斜线和尾随斜线很重要。
可以在 Angular 文档中找到更多详细信息,here
我遇到了同样的问题。我使用 ./
.
<base href="/"> to <base href="./">
再次构建和部署。
使用
时,您可能会收到带有 Angular 11 的“已弃用”警告ng build --prod --base-href=/StartingNewProject/
您可以改为调整 angular.json。
在 angular.json 文件中,像这样创建一个“baseHref”属性:
"projects": {
"your-project-name" : {
"architect": {
"build": {
"options": {
"baseHref": "/your-relative-path/"
这将改变生产和开发中的 URL。
请注意,在您的链接中,请始终使用这样的相对路径(而不是“/”):
<img class="top-bar__logo" src="./assets/images/picture1.svg" />