gpt4 book ai didi

java - 白标错误页面 404 spring boot angular 5

转载 作者:太空狗 更新时间:2023-10-29 19:33:32 25 4
gpt4 key购买 nike

我正在开发一个 Web 应用程序,我的后端是 spring boot,前端是在 4200 端口上运行的 Angular 5。我在 angular 5 登录、家庭应用程序、搜索中有四个组件。当我启动 spring boot 项目和 angular 项目时,我能够通过提供 http://localhost:4200 来导航登录页面。所以它导航到 http://localhost:4200/login

但是当我刷新该页面时,出现以下错误。 enter image description here

这是我的代码:

代理.conf.json

    {
"/": {
"target": "http://localhost:8081",
"secure": false
}
}

package.json:

 {  "name": "cyber-security-vw",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.conf.json",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"core-js": "^2.4.1",
"rxjs": "^5.5.6",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular/cli": "1.6.5",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
}
}

索引.html

    <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CyberSecurityVw</title>
<base href="/">

<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>

app.component.html

<div align="center">
<router-outlet></router-outlet>
</div>

应用路由.module.ts

import { ApplicationComponent } from './application/application.component';
import { NavigationComponent } from './navigation/navigation.component';
import { HomepageComponent } from './homepage/homepage.component';
import { AppComponent } from './app.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';



const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'home', component: HomepageComponent },
{ path: 'application', component: ApplicationComponent },
{ path: 'navigation', component: NavigationComponent },
];

@NgModule({
imports: [CommonModule, RouterModule.forRoot(routes)],
exports: [RouterModule],
declarations: []
})

export class AppRoutingModule { }

Spring Boot 主类

package com.vl.cybersecurity;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@SpringBootApplication
public class CyberSecurityApplication {

public static void main(String[] args) {
SpringApplication.run(CyberSecurityApplication.class, args);
}
}

其他包:

com.vl.cybersecurity.controller     
com.vl.cybersecurity.entity
com.vl.cybersecurity.service
com.vl.cybersecurity.dao

最佳答案

我也遇到了同样的问题。这是对我有用的解决方案。

我在我的应用程序路由模块中启用了 hash(#)。

如何启用?

第 1 步:打开 app-routing.module.ts第 2 步:在 RouterModule.forRoot 中添加路由时,将可选参数 useHash 作为真值传递。

 @NgModule({
imports: [RouterModule.forRoot(routes, { useHash: true })],
exports: [RouterModule],
})

第 3 步:保存文件。它应该可以解决这个问题。您会在 url 中注意到的唯一不同之处是端口号 http://localhost:4800/#/dashboard

之后的哈希 (#)

还有另一种方法可以做同样的事情,检查this

关于java - 白标错误页面 404 spring boot angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com