【问题标题】:Why doesn't this route match when navigating to it directly?为什么直接导航到这条路线时不匹配?
【发布时间】:2016-06-26 14:05:35
【问题描述】:

声明:这里使用的是当前相当新的"@angular/router": "3.0.0-alpha.8",路由定义在帖子底部。

尝试在我的应用程序中导航时,行为会有所不同,具体取决于我是直接输入 URL 还是跟随链接:

  • 作品:在地址栏中输入http://localhost:9292,正确转发到http://localhost:9292/about
  • 工作:通过地址栏直接导航到http://localhost:9292/about
  • 有效:如果我通过<a> 标记导航到http://localhost:9292/about/projects,在FrontComponent[routerLink]="['projects']" 属性的上下文中,路由工作正常。
  • 损坏:直接导航到 http://localhost:9292/about/projects 会导致以下错误消息(缩短的 Stacktrace):

    Unhandled Promise rejection: Cannot match any routes: 'projects' ; Zone: angular ; Task: Promise.then ; Value: Error: Cannot match any routes: 'projects'
    Stack trace:
    applyRedirects/<@http://localhost:9292/node_modules/@angular/router/apply_redirects.js:29:34
    Observable.prototype.subscribe@http://localhost:9292/node_modules/rxjs/Observable.js:52:57
    

我在这里可能做错了什么?有没有办法查看错误消息中所有被拒绝的路由?

编辑,因为这已被多次建议:我不怀疑这是服务器端问题。先前通过router-deprecated 的路由工作正常,而从失败路由提供的HTML 看起来也很好。但以防万一,这是相关的服务器端路由代码(rubysinatra):

# By now I have too often mistakenly attempted to load other assets than
# HTML files from "user facing" URLs, mostly due to paths that should have
# been absolute but weren't. This route attempts to catch all these
# mistakes rather early, so that the show up as a nice 404 error in the
# browsers debugging tools
get /^\/(about|editor)\/.*\.(css|js)/ do
  halt 404, "There are no assets in `editor` or `about` routes"
end

# Matching the meaningful routes the client knows. This enables navigation
# even if the user submits a "deep link" to somewhere inside the
# application.
index_path = File.expand_path('index.html', settings.public_folder)
get '/', '/about/?*', '/editor/*' do
  send_file index_path
end

# Catchall for everything that goes wrong
get '/*' do
  halt 404, "Not found"
end

我不怀疑这是由于错误的路由定义(路由确实通过链接起作用),但为了完整起见,这些是路由:

front/front.routes.ts

export const FrontRoutes : RouterConfig = [
    {
        path: '',
        redirectTo: '/about',
        terminal: true
    },
    {
        path : 'about',
        component: FrontComponent,
        children : [
            { path: 'projects', component: ProjectListComponent},
            { path: '',         component: AboutComponent},
        ]
    }
]

editor/editor.routes.ts

export const EditorRoutes : RouterConfig = [
    {
        path: "editor/:projectId",
        component : EditorComponent,
        children : [
            { path: '', redirectTo: 'settings', terminal: true},
            { path: 'settings', component : SettingsComponent },
            { path: 'schema', component : SchemaComponent },
            { path: 'query/create', component : QueryCreateComponent },
            { path: 'query/:queryId', component : QueryEditorComponent },
            { path: 'page/:pageId', component : PageEditorComponent },
        ]
    }
]

app.routes.ts

import {EditorRoutes}                   from './editor/editor.routes'
import {FrontRoutes}                    from './front/front.routes'

export const routes : RouterConfig = [
    ...FrontRoutes,
    ...EditorRoutes,
]

export const APP_ROUTER_PROVIDERS = [
    provideRouter(routes)
]

【问题讨论】:

  • 确保您的服务器端代码在访问http://localhost:9292/about/projects时返回正确的HTML页面
  • 在地址栏中输入完整 URL 时,请求首先到达服务器,然后在客户端执行 Angular,
  • 如果这是问题所在,导航到http://localhost:9292/about 也不应该工作,但确实可以。我已经在服务器上解决了这个问题。
  • 是的,但我会说要仔细检查,因为它看起来像服务器端问题
  • 我检查了三次,这 100% 不是问题。

标签: angular angular2-routing


【解决方案1】:

"/" 设置为base 标记的值index.html 以使路由器正确解析子路径。这是not in line with the offical docs,但似乎有效。

【讨论】:

  • 因为文档悲伤'.'而损失了两个小时
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多