简体   繁体   English

Angular 在 webjar 中发布的应用程序:baseHref 问题

[英]Angular App released in a webjar: issue with baseHref

I need to package my angular frontend in a webjar to be imported, through maven, in whatever java backend. I need to package my angular frontend in a webjar to be imported, through maven, in whatever java backend.

I did it, so now I have a spring boot backend with the following application.properties我做到了,所以现在我有一个 spring 引导后端,具有以下application.properties

server.servlet.context-path=/demo 

and the following pom.xml和以下pom.xml

<dependencies>
  <dependency>
    <groupId>com.foo.bar</groupId>
    <artifactId>angular-frontend-webjar</artifactId>
    <version>1.0.0</version>
 </dependency>
 ...     

And this config而这个配置

@Configuration
public class WebjarConfig extends WebMvcConfigurationSupport {
  
  @Override
  protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }

So far so good , I'm able to reach the frontend at the url: http://localhost:8080/demo/index.html到目前为止一切顺利,我能够到达 url 的前端:http://localhost:8080/demo/index.html
But the angular frontend bootstrap fails because JS/CSS files are not correctly loaded.angular 前端引导程序失败,因为 JS/CSS 文件未正确加载。

The reason is: the contextPath of backend is /demo but the tag <base href="/"> specified into the index.html of the frontend is "/" (root).原因是:后端的contextPath是/demo ,而前端的index.html中指定的标签<base href="/">"/" (root)。
So the browser tries to download JS from http://localhost:8080/main-es2015.js instead of http://localhost:8080/demo/main-es2015.js所以浏览器尝试从 http://localhost:8080/main-es2015.js 下载 JS 而不是 http://localhost:8080/demo/main-es2015.js

I've read from the angular doc that is possible to override the base href at the frontend build time with the command ng build --base-href /actualContextPath/ , but at that building time I don't know the contextPath of the spring boot app that will import the webjar with the frontend.我已经阅读了angular 文档,该文档可以在前端构建时使用命令ng build --base-href /actualContextPath/覆盖基本 href,但在构建时我不知道 spring 的 contextPath启动应用程序,它将使用前端导入 webjar。

Any help?有什么帮助吗?

Did you try this?你试过这个吗?

On your pom.xml:在你的 pom.xml 上:

<build>
    <finalName>${project.parent.name}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${version.spring-boot}</version>
            <!-- Habilitamos la generacion de un properties con la info de maven 
                accesible desde spring-boot, como el nombre que le queramos dar a la apliacion, 
                para facilitar el desarrollo -->
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                    <configuration>
                        <additionalProperties>
                            <applicationName>${project.parent.name}</applicationName>
                        </additionalProperties>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Then in your configuration java file you have access to the property "applicationName"然后在您的配置 java 文件中,您可以访问属性“applicationName”

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
@Autowired
BuildProperties buildProperties;
...

And you can set de application context this way:您可以通过这种方式设置应用程序上下文:

@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
    webServerFactoryCustomizer() {
    return factory -> factory.setContextPath("/"+buildProperties.get("applicationName"));
}

So, on pom.xml applicationName --> "demo" and in your java spring boot config you set the context path to "demo".因此,在 pom.xml applicationName --> "demo" 和 java spring 引导配置中,您将上下文路径设置为 "demo

This way you use a more dynamic route to set the context path.这样您就可以使用更动态的路由来设置上下文路径。 If you don't want to play with pom.xml you only have to use this:如果你不想玩 pom.xml 你只需要使用这个:

@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
    webServerFactoryCustomizer() {
    return factory -> factory.setContextPath("/demo");
}

Good luck and sorry for my English.祝我好运,对不起我的英语。

OK OK, I got it now.好的好的,我现在明白了。

Set your base href to "./"将您的基本 href 设置为“./”

Load your webjar scripts like this src="webjars/..." without the "/" before webjars.像这样加载你的 webjar 脚本 src="webjars/..." 在 webjars 之前没有“/”。

And the last point is let Angular manage the routes that are out of Spring.最后一点是让Angular管理出Spring的路由。 You can check this repo to have an idea, read the las part on the readme:你可以检查这个 repo 有一个想法,阅读自述文件中的 las 部分:

https://github.com/FraktonDevelopers/spring-boot-angular-maven-build https://github.com/FraktonDevelopers/spring-boot-angular-maven-build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Angular应用程序部署到具有更改的baseHref丢失文件的服务器 - Deploying Angular app to server with changed baseHref missing files Angular Monorepo 与 Basehref 共享资产 - Angular Monorepo shared assets with Basehref 如何使用baseHref正确构建带有i18n的Angular 6应用程序到“语言环境目录”? - How to correctly build an Angular 6 app with i18n into “locale directories” with baseHref? 角度:设置baseHref不会创建子文件夹 - Angular: setting baseHref does not create subfolders 在 Angular 14 中为 ng 服务覆盖 baseHref - override baseHref for ng serve in Angular 14 angular baseHref 对索引没有影响。html 链接 - angular baseHref no impact on index.html links Angular:如何在使用SSR时为开发中的资产设置baseHref - Angular: How to set baseHref for assets in development when using SSR 更新到 Angular 11 时,BaseHref 在 scss 文件中不起作用 - BaseHref not working in scss files when updating to Angular 11 Angular-这是应用程序问题还是部署问题? - Angular - Is this an app issue or a deployment issue? Angular 7 SyntaxError:预期的表达式,当 basehref 从 root 更改时,在生产中得到 &#39;&lt; - Angular 7 SyntaxError: expected expression, got '< in production when ever basehref change from root
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM