How to set base url for Angular application

元气小坏坏 提交于 2019-12-08 02:57:25

You can modify it using <base href="/"> in src/index.html file.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>SiteFrontend</title>
    <base href="/test">   //<------ this line

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

Better to do it when you build project. Like this:

ng build --prod --base-href=/test/

Please follow these following steps:

  1. Set useHash: false in app-routing-module.ts
  2. In index.html, change <base href="./" /> to <base href="/" />

Here is the solution that comes closest to what I wanted:

Rename src/main/webapp/index.html to src/main/webapp/crud.html

Create a new file src/main/webapp/index.html, with whatever static content you want to serve on /. Alternatively, you can have a Spring Boot Controller serve /.

Changes to webpack/webpack.common.js:

Under new HtmlWebpackPlugin, add this entry:

filename: 'crud/index.html'

Under new CopyWebpackPlugin, add this entry:

{ from: './src/main/webapp/index.html', to: 'index.html' }

and rename the template:

template: './src/main/webapp/crud.html',

Under modules/rules/*.html, rename the html file as well:

exclude: /(src\/main\/webapp\/crud.html)/

In the crud.html file, I changed the base to this, as others already stated:

<base href="/" />.

This is needed to find the backend APIs, which are still on /api/

These were all the changes I had to make. Now / and /index.html will serve my own non-jhipster page. All JHipster is served under /crud/index.html. Once you start navigating, the address in the browser will show /#/, but that does not bother me that much for now. But will be glad to hear how to change that to /crud/ as well.

Chandan Agarwal

Below steps are for setting base url to /crud in production only which has worked for me

  1. in webpack.prod.js add the following in plugins section:

    new BaseHrefWebpackPlugin({ baseHref: '/crud' })
    
  2. in webpack.prod.js in the output section add:

    publicPath: '/crud/',
    

On executing npm run build the index.html will prefix baseHref with '/crud' and also add prefix 'crud' to all the css and urls.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!