首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在浏览器中点击URL加载angular组件,同时使用spring boot?

问如何在浏览器中点击URL加载angular组件,同时使用spring boot?
EN

Stack Overflow用户
提问于 2018-10-19 17:49:03
回答 3查看 3.6K关注 0票数 1

我已经通过以下链接创建了angular客户端和spring boot服务器应用程序。https://vitalflux.com/spring-boot-angular-app-hello-world-one-deployable-war/ https://blog.jdriven.com/2016/12/angular2-spring-boot-getting-started/

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

​

​

当我使用"ng serve“启动angular development server并在浏览器中点击url下方时,它工作得很好。http://localhost:4200/dashboard

然而,当我启动spring boot,然后在浏览器中点击下面的URL时,它不工作并抛出错误。http://localhost:8080/dashboard

​

​

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

有没有人能建议一下如何解决这个问题?

我是spring boot的新手,不确定如何在angular路由上工作。我试着使用下面的链接,但我不是很清楚。

Spring Boot Angular application route from spring controller to angular component(view)

Spring Boot with AngularJS html5Mode

Angular routing doesn't work after deploy into a Springboot Application

Springboot/Angular2 - How to handle HTML5 urls?

How to integrate an Angular 4 app with a Spring Boot stack?

https://blog.jdriven.com/2016/10/integrate-angular-spring-boot-gradle/#support-angular-html5-mode

Spring Boot-Angular - Entering Url in Address Bar results in 404

EN

回答 3

Stack Overflow用户

发布于 2018-10-20 01:10:26

我找到了答案。下面的工作与预期的一样。

代码语言:javascript
复制
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";
   }
}
票数 1
EN

Stack Overflow用户

发布于 2018-10-19 18:41:51

为了防止spring-boot应用程序解析您的angular路由,您可以添加一个ViewController来将路由重定向到angular应用程序,如Springboot/Angular2 - How to handle HTML5 urls?中所述。

代码语言:javascript
复制
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {

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

Angular5 app.module.js

代码语言:javascript
复制
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 generate component Dashboard创建的仪表板组件

angular应用程序必须位于pom.xml中定义的资源插件所请求的spring-boot项目旁边。

|_弹簧靴

|_ angular-应用

票数 0
EN

Stack Overflow用户

发布于 2018-10-19 18:44:50

根据我对您问题的理解,只需创建一个名为proxy.config.json的新文件,并将下面的代码粘贴到该文件中,将该文件放在.

-cli.json旁边

代码语言:javascript
复制
{
  "/": {
    "target": "http://localhost:8080",
    "secure": false,
    "logLevel": "debug"
  }
}

app.component.ts文件中的

放置在ngOnInit()中的代码下面

代码语言:javascript
复制
this.http.get('/dashboard',someRequestOption).subscribe(res =>{
 ---------------
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52889911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档