首页 > 解决方案 > 当我运行命令“ng build”时,我无法从浏览器输入路线,只能通过内部导航

问题描述

当我用 编译我的项目时ng build,它成功完成。

我得到这样的结构:

<!doctype html>
  <html lang="es" class="thin scrollbar-gris" style="overflow-x: visible;">
  <head>
    <meta charset="utf-8">
    <title>title</title>
    <base href="/">

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="styles.51e30e538d2560c1a9e9.css"></head>
  <script src="./assets/lib/wow.min.js"></script>
  <script>
  new WOW().init();
  </script>
  <body  >
    <app-root style="overflow-x: visible;"></app-root>
  <script type="text/javascript" src="runtime.a5dd35324ddfd942bef1.js"></script><script type="text/javascript" src="polyfills.3eb7881d3a00da6c675e.js"></script><script type="text/javascript" src="scripts.dd86f002a5aef1b13f0f.js"></script><script type="text/javascript" src="main.23ac360bc829b49bc07d.js"></script></body>
  </html>

但它没有找到库,然后我改变

<base href="/">

<base href="./">

它有效。但真正的问题发生在我的应用程序的路由中,例如这个:

 http://localhost/my_folder_compilated/programa/detalle-programa/5cf7d27faa8e180017211754

// this is the route that I have in my app.routing.ts
{path: 'programa/detalle-programa/:programaId', component: DetalleProgramaComponent},
{path: '**', component: NotFoundComponent}

一切正常,但是如果我在这条路线中重新加载页面,我会收到错误 404(未找到页面),如果我按照我的应用程序的链接正常浏览,我没有任何问题。

在编译之前我没有这种问题,我可以从浏览器输入这条路线。

总之:

我执行ng build,输出是code我放的。改成href = "/""./"因为不认了.js files。我对此进行测试xaamp (localhost/my_folder_compilated/)

我的应用程序位于根文件夹中,例如在 xaamp 中是 htdocs/my_folder_compilated 并且浏览器中的访问是 localhost/my_folder_compilated

我该怎么做才能纠正它?

标签: angular

解决方案


与 PathLocationStrategy 一起使用的基本 href 的预定义 DI 令牌。基本 href 是在生成和识别 URL 时应保留的 URL 前缀。

在你的app.module.ts

providers: [{provide: APP_BASE_HREF, useValue: '/'}],

推荐阅读