如何为 Angular 应用程序设置基数 url

How to set base url for Angular application

我有一个由 JHipster 生成的 Angular 应用程序。默认情况下,它在根 / url 上运行,并重定向到 /#/。有没有办法将它覆盖为其他东西?

编辑

抱歉,我必须重新表述我的问题,因为它在下面的所有答案中都被误解了。我想将我的 JHipster 生成的 Angular 应用程序放在 / 以外的东西上,例如 /crud/。目的是在 / 上提供一些非 Angular 的内容。如何将整个应用程序从 / 移动到 /crud// 仍然由静态 index.html 文件提供服务吗?

您可以使用 src/index.html 文件中的 <base href="/"> 对其进行修改。

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

最好在构建项目时进行。像这样:

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

请按照以下步骤操作:

  1. app-routing-module.ts
  2. 中设置useHash: false
  3. index.html中,将<base href="./" />更改为<base href="/" />

以下步骤仅在生产中将基础 url 设置为 /crud,这对我有用

  1. in webpack.prod.js 在插件部分添加以下内容:

    new BaseHrefWebpackPlugin({ baseHref: '/crud' })
    
  2. in webpack.prod.js 在输出部分添加:

    publicPath: '/crud/',
    

在执行 npm run build 时,index.html 将在 baseHref 前加上 '/crud' 并向所有 css 和 url 添加前缀 'crud' .

使用 ng build --prod --base-href=/test 而不是 ng build --prod --base-href=/test/ 因为那样我就不会加载图像