在Angular 19 App上构建JSON文件至GitHub页面上的Error 我正在尝试将角度应用部署到github页面上。我正在使用Angular 19和Node 22。我已经过去了几天,但是由于不同版本和教程,我对如何部署我的应用程序感到非常困惑...

问题描述 投票:0回答:1
文件夹并将其推开github master分支。

这是我在github页面上的配置:


在这里我的相关文件:

// package.json { "name": "bible-quiz", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build --configuration=production --output-path docs --base-href /bible-quiz/ && mv docs/browser/* docs/ && rmdir docs/browser", "watch": "ng build --watch --configuration development", "test": "ng test" }, "private": true, "dependencies": { "@angular/animations": "^19.1.0", "@angular/common": "^19.1.0", "@angular/compiler": "^19.1.0", "@angular/core": "^19.1.0", "@angular/forms": "^19.1.0", "@angular/platform-browser": "^19.1.0", "@angular/platform-browser-dynamic": "^19.1.0", "@angular/router": "^19.1.0", "@tailwindcss/postcss": "^4.0.0", "postcss": "^8.5.1", "rxjs": "~7.8.0", "tailwindcss": "^4.0.0", "tslib": "^2.3.0", "zone.js": "~0.15.0" }, "devDependencies": { "@angular-devkit/build-angular": "^19.1.4", "@angular/cli": "^19.1.4", "@angular/compiler-cli": "^19.1.0", "@types/jasmine": "~5.1.0", "jasmine-core": "~5.5.0", "karma": "~6.4.0", "karma-chrome-launcher": "~3.2.0", "karma-coverage": "~2.2.0", "karma-jasmine": "~5.1.0", "karma-jasmine-html-reporter": "~2.1.0", "typescript": "~5.7.2" } } enter image description here // angular.json { "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "bible-quiz": { "projectType": "application", "schematics": {}, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:application", "options": { "outputPath": "docs", "index": "src/index.html", "browser": "src/main.ts", "polyfills": [ "zone.js" ], "tsConfig": "tsconfig.app.json", "assets": [ { "glob": "**/*", "input": "public", "output": "/" } ], "styles": [ "src/styles.css" ], "scripts": [] }, "configurations": { "production": { "budgets": [ { "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" }, { "type": "anyComponentStyle", "maximumWarning": "4kB", "maximumError": "8kB" } ], "outputHashing": "all" }, "development": { "optimization": false, "extractLicenses": false, "sourceMap": true } }, "defaultConfiguration": "production" }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "configurations": { "production": { "buildTarget": "bible-quiz:build:production" }, "development": { "buildTarget": "bible-quiz:build:development" } }, "defaultConfiguration": "development" }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n" }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "polyfills": [ "zone.js", "zone.js/testing" ], "tsConfig": "tsconfig.spec.json", "assets": [ { "glob": "**/*", "input": "public" } ], "styles": [ "src/styles.css" ], "scripts": [] } } } } } }

// app.routes.ts import {Routes} from '@angular/router'; import {SubjectListComponent} from './components/subject-list/subject-list.component'; import {SubjectComponent} from './components/subject/subject.component'; import {InnerSubjectListComponent} from './components/inner-subject-list/inner-subject-list.component'; export const routes: Routes = [ {path: '', pathMatch: 'full', redirectTo: 'subjects'}, {path: 'subjects', component: SubjectListComponent}, {path: 'subjects/:parent-id/inner-subjects', component: InnerSubjectListComponent}, {path: 'subjects/:parent-id/inner-subjects/:inner-subject-id', component: SubjectComponent}, ];

<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>BibleQuiz</title>
  <base href="/bible-quiz/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>
// subjects.service.ts
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {ISubject} from '../models/ISubject';
import {environment} from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class SubjectsService {
  private subjectsUrl = environment.apiUrl + 'subjects.json';

  constructor(private http: HttpClient) {
  }

  getSubjects(): Observable<ISubject[]> {
    return this.http.get<ISubject[]>(this.subjectsUrl);
  }
}
// environment.prod.ts
export const environment = {
  production: true,
  apiUrl: '/bible-quiz/'
};
// environment.ts
export const environment = {
  production: false,
  apiUrl: '/'
};
由于我的理解,由于我的理解是由于GitHub页面附加了额外的 /圣经quiz / suprot,因此已经添加了环境,但不确定这是否真的需要。
这是我的完整应用:
https://github.com/francislainy/bible-quiz
在这里我要加载的URL:

https://francislainy.github.io/bible-quiz

和我在控制台中看到的图像:

看起来像是您的Angular.json在fileReplacementsenter image description here下缺少configuration.production

。您需要添加该部分,以便编译器替换为
angular github-pages
1个回答
0
投票

environment.prod.ts

。这将确保您的服务中使用正确的
apiUrl
角 - 在应用程序中使用环境特定变量
"configurations": { "production": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], …
技术上讲
environment.ts
应包含生产环境值,并且您应该有一个针对非生产环境的文件。首先,我将其提供如何拥有并添加上述片段到您的

environment.{env}.ts

中,然后在您有舒适的理解时进行调整。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.