Java 如何在使用spring boot时通过点击浏览器中的URL加载角度组件?

Java 如何在使用spring boot时通过点击浏览器中的URL加载角度组件?,java,angular,spring-mvc,spring-boot,Java,Angular,Spring Mvc,Spring Boot,我通过以下链接创建了angular客户端和spring引导服务器应用程序。 在我的angular应用程序中,我定义了如下路线: 当我使用“ng serve”启动angular development server并在浏览器中点击下面的url时,它工作正常。 然而,当我启动SpringBoot,然后在浏览器中点击下面的URL时,它不工作并抛出错误。 我的要求是,当我在浏览器中点击带有角度路由的URL时,我应该能够加载特定的角度组件。 我的角度组件不需要从后端从RESTAPI获取任何数据,

我通过以下链接创建了angular客户端和spring引导服务器应用程序。

在我的angular应用程序中,我定义了如下路线:

当我使用“ng serve”启动angular development server并在浏览器中点击下面的url时,它工作正常。

然而,当我启动SpringBoot,然后在浏览器中点击下面的URL时,它不工作并抛出错误。

我的要求是,当我在浏览器中点击带有角度路由的URL时,我应该能够加载特定的角度组件。 我的角度组件不需要从后端从RESTAPI获取任何数据,它是一个简单的角度组件

有人能建议如何解决这个问题吗

我是spring boot的新手,不知道如何使其与角度路线一起工作。我试着按照下面的链接,事情对我来说不是很清楚


为了防止spring boot应用程序解析角度路由,您可以添加一个
ViewController
将路由重定向到角度应用程序,如中所述

Angular5
app.module.js

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component';

const appRoutes: Routes = [{
  path: 'dashboard',
  component: DashboardComponent
}]

@NgModule({
  declarations: [
    AppComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true },
    )
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
使用
ng生成组件仪表板创建的仪表板组件

angular应用程序必须位于spring boot项目之外,该项目由
pom.xml
中定义的资源插件请求

|_弹簧靴

|_角度应用程序

据我所知,您的问题只是创建一个名为 将proxy.config.json粘贴到该文件的下面代码中,然后将文件放在下一个位置 to.angular-cli.json

在app.component.ts文件中,将代码放在ngOnInit()的下面


我找到了答案。下面的工作如预期

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {

   @RequestMapping({ "/dashboard", "/contacts" })   
   public String index() {
       return "forward:/index.html";
   }
}

否,标记存在于my app.component.html中。我需要通过点击浏览器中的url来加载,而不是点击某个链接。好吧,那是另一回事。添加了阻止spring boot解析角度路由的方法。您所说的“添加了阻止spring boot的方法”是什么意思?我的回答来得太晚了,您当时已经有了解决方案。这意味着在没有ViewController重定向的情况下,spring boot尝试解析´/dashboard`URL。工作起来很有魅力。您可能需要共享一些代码。谢谢!让我检查一下我哪里弄错了。嗨,我在我的答案中添加了运行示例的所有基本信息,但为时已晚……如果有许多组件(如仪表板)需要通过在浏览器中点击url加载,它会工作吗?
{
  "/": {
    "target": "http://localhost:8080",
    "secure": false,
    "logLevel": "debug"
  }
}
this.http.get('/dashboard',someRequestOption).subscribe(res =>{
 ---------------
})
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {

   @RequestMapping({ "/dashboard", "/contacts" })   
   public String index() {
       return "forward:/index.html";
   }
}