【问题标题】:Can not get /route after refresh page Angular 7刷新页面Angular 7后无法获取/路由
【发布时间】:2019-12-21 11:03:42
【问题描述】:

我在 Angular 7 中有一些组件,我不使用 'useHash: true' 进行路由。

我在实时服务器上部署了我的 Angular 应用程序,如果我使用路由导航到某个组件(例如 /panel),那么它可以正常工作,但如果我重新加载该页面,我会收到错误“无法获取 /panel”

有人可以帮忙吗?

【问题讨论】:

  • 将 index.html 中的基本路径设置为您的项目根文件夹

标签: angular server routing live


【解决方案1】:

您应该将服务器配置为回退到 index.html。 查看 angular docs here 你如何做到这一点。 例如,在 Appcache 服务器上,我正在使用这个 .htaccess 文件:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) /index.html [NC,L]

【讨论】:

    【解决方案2】:

    在您的应用中运行此 CLI 命令

    ng build --prod="true" --base-href="<Your-Base-Path/<your-project-folder-name>"
    

    并且,这将使用您定义的路径在index.html 中设置基本路径,因此每次刷新页面时,它都会在您的根文件夹的基础上。

    现在你的index.html 文件有这个base-path

    <base href="<Your-Base-Path/<your-project-folder-name>">
    

    【讨论】:

    • ng build --prod="true" --base-href="" 这个命令对我没有帮助跨度>
    猜你喜欢
    • 2019-05-25
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多