【问题标题】:nginx error 'Cannot match any routes' with Angular 2 router 3.0.0.-alpha.7Angular 2 路由器 3.0.0.-alpha.7 的 nginx 错误“无法匹配任何路由”
【发布时间】:2016-07-02 08:54:13
【问题描述】:

nginx 似乎不适用于 Angular 2 RC3 路由器的 prod 版本。我收到以下错误:

Error: Uncaught (in promise): Error: Cannot match any routes: ''
at s (zone.js:538)
at zone.js:515
at t.invoke (zone.js:323)
at Object.onInvoke (ng_zone_impl.js:46)
at t.invoke (zone.js:322)
at t.run (zone.js:216)
at zone.js:571
at t.invokeTask (zone.js:356)
at Object.onInvokeTask (ng_zone_impl.js:37)
at t.invokeTask (zone.js:355)

我的nginx配置很简单:

    listen       8080;
    server_name  localhost;
    charset utf-8;
    location / {
        root   /Users/JARVIS/src/web/ui/dist;
        index  index.html index.htm;
    }

我不知道这是它的配置问题,还是新路由器的问题,还是它的编译方式问题。我正在使用 angular2-webpack-starter。此问题仅发生在 nginx 中托管的 prod 构建中。在开发服务器中一切正常。

我的路线非常简单:

export const ChromeRoutes: RouterConfig = [
  {
    path: '',
    component: Chrome,
    canActivate: [AuthGuard], // this protects all child routes
    children: [
      { path: 'dashboards', component: Dashboards },
      { path: 'inventory',  component: Inventory },
      { path: 'content',    component: Content },
      { path: 'settings',   component: Settings },
      { path: '',           redirectTo: '[/dashboards]' }
    ]
  }

我尝试将 '' 更改为 'chrome' 而没有任何变化。有无 redirectTo 条目也无所谓。


编辑: 我通过 JSON.stringify() 定义的完整路线... javascript [{ "path": "login", "terminal": true }, { "path": "", "canActivate": [null], "children": [{ "path": "dashboards" }, { "path": "inventory" }, { "path": "content" }, { "path": "settings" }] }, { "path": "inventory", "canActivate": [null], "children": [{ "path": "", "terminal": true }, { "path": ":id" }] }]

【问题讨论】:

    标签: nginx angular router


    【解决方案1】:
    • 如果您在<head> 中有<base href=".">,请尝试将其更改为<base href="/">

    • 添加terminal: true空路径路由

    { path: '',           redirectTo: '[/dashboards]': pathMatch: 'full' }
    

    { path: '',           redirectTo: '[/dashboards]': terminal: true }
    

    【讨论】:

    • 终端已弃用;而应该是:pathMatch: 'full'
    • 感谢您指出这一点。很难跟上所有这些变化;-)
    • 我确保所有具有path: '' 的路径(根路径除外)都具有terminal: true。我的基本href是'/'。仍然得到同样的错误。
    • 我已经编辑以提供我的整个路线配置组合的最终结果。请参阅原始问题。
    • 所以,它似乎是一个组合。我注释掉了所有路线并解决了这个问题。基本 href 是键的一部分。还要寻找正在设置的 APP_BASE_HREF。然后确保 children 数组有一个 path: '', redirectTo: '/default', terminal: true 似乎就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 2019-12-13
    • 2018-01-05
    • 2017-09-25
    • 2019-01-16
    • 2017-01-21
    • 2016-10-01
    相关资源
    最近更新 更多