【问题标题】:Serving Angular2 at a path with Spring Boot使用 Spring Boot 在路径上服务 Angular2
【发布时间】:2017-06-05 16:45:47
【问题描述】:

我正在 Spring Boot 应用程序中运行 Angular2 应用程序。这是我目前的结构:

src/main
    java/com/test
        Application.java
    resources/static
        app/
        css/
        images/
        node_modules/
        index.html
        package.json
        systemjs.config.js
        tsconfig.json

我这辈子都不知道如何告诉 Angular 我正在通过这样的路径为它提供服务:localhost:8080/angularApp

我已经在弹簧方面进行了配置:

system.context-path=/angularApp

但我无法让应用程序真正认识到这一点。以下是我尝试过的几件事:

tsconfig.json 内部:

"baseUrl": "./angularApp

也在 tsconfig.json 中:

"paths": {
  "app/*": [ "angularApp/app/*" ]
}

但这些都不起作用。

当我尝试加载 localhost:8080/angularApp

我看到的问题是这样的:

GET http://localhost:8080/css/styles.css 404 ()

在我的 index.html 中,我将其配置如下:

<link rel="stylesheet" href="css/styles.css">

如果我像这样配置它,它确实可以工作,但是该路径可能会根据我运行它的环境而改变,所以像这样对每一行进行硬编码是不可行的:

<link rel="stylesheet" href="angularApp/css/styles.css">

我觉得这应该很容易,但我错过了一些东西。

谁能提供一些见解?

【问题讨论】:

  • 您是否尝试在 index.html 的头部添加基本标签?
  • @dmungin 我没有,这绝对是整个问题。如果您想提交答案,我会接受。不敢相信我错过了,非常感谢!

标签: spring-mvc angular spring-boot


【解决方案1】:

您需要在 index.html 的头部添加一个基本标签

<base href="/angularApp/">

如果需要,可以链接到文档以获得进一步的解释:https://angular.io/docs/ts/latest/guide/router.html#!#base-href

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 2017-10-30
    • 2017-06-14
    • 1970-01-01
    • 2020-01-07
    • 2016-03-13
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多