【问题标题】:Angular routing with parameters doesn't work when URL is requested out of the app当从应用程序请求 URL 时,带参数的角度路由不起作用
【发布时间】:2018-05-27 12:19:09
【问题描述】:

我的 Angular 路由有问题,如果从应用程序外部请求带有参数的 URL,应用程序将失败。

http://localhost:4200/clients 如果您从应用程序导航到该路线或只是在地址栏中键入它,则此路线有效。

http://localhost:4200/clients/20 如果您从应用程序导航到该路线,则此路线有效,如果在地址栏中输入,则有效。

注意:在控制台中,它从二级路由请求css和javascript文件(http://localhost:4200/clients/assets/vendor/jquery/jquery.js而不是http://localhost:4200/assets/vendor/jquery/jquery.js

有什么帮助吗?

【问题讨论】:

  • 这是一个 angularjs 或 Angular 应用程序。您也许应该显示一些路由配置的代码。
  • “不起作用”可能意味着很多事情。当你这样做时会发生什么?是完全失败了,是请求了根页面还是导致了 404 错误?
  • 好吧,它在加载页面资源时一开始就失败了,因为我在问题末尾的注释中提到了所有资源的路径都发生了变化。
  • 请问您的问题是怎么解决的?

标签: angular url-routing angular-routing


【解决方案1】:

通过在路由中启用哈希来完成修复。

在 app.module.ts 中

RouterModule.forRoot(
      [
        { path: 'Home', component: HomeComponent },
        { path: 'ImportOperations', component: ImportOperationComponent },
        { path: 'ImportData/:Id', component: ImportRawDataComponent },
        { path: 'rules/:Id', component: RulesComponent },
        { path: 'newrule', component: RulesComponent },
        { path: 'rules', component: RulesListComponent },
        { path: 'search', component: SearchComponent },        
        { path: 'editresult/:Id', component: EditSearchComponent }
      ]
      ,{useHash: true}), 

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2018-10-04
    • 1970-01-01
    • 2017-06-22
    相关资源
    最近更新 更多