简体   繁体   English

Ember.js路由器和initialState中的嵌套路由

[英]Nested routes in Ember.js router and initialState

I'm trying to understand emeber.js routing. 我正在尝试了解emeber.js路由。 I'm not really sure, why this is not valid definition of router 我不太确定,为什么这不是路由器的有效定义

Platby.Router = Ember.Router.extend   
  location: 'hash'   
  enableLogging : true

  root: Ember.Route.extend
    index: Ember.Route.extend
      route: '/'
      initialState: 'batches'
      enter: (router) -> console.log 'root.index'

      batches: Ember.Route.extend
        initialState: 'index'
        route: '/batches'
        enter: (router) -> console.log 'root.index.batches'

        index: Ember.Route.extend
          route: '/'
          enter: (router) -> console.log 'root.index.batches.index'

Once proceed to the root url, I get the following output in the console. 一旦进入根URL,我在控制台中得到以下输出。

STATEMANAGER: Entering root
STATEMANAGER: Sending event 'navigateAway' to state root.
STATEMANAGER: Sending event 'unroutePath' to state root. 
STATEMANAGER: Sending event 'routePath' to state root. 
Uncaught Error: assertion failed: Could not find state for path  

Will be somebody able to explain me, where's the problem? 将有人能够解释我,问题出在哪里?

I can only answer based on the information you've given... Regarding the error uncaught Error: assertion failed: Could not find state for path , this is because you have no leaf state that matches the path "/". 我只能根据你给出的信息回答...关于错误uncaught Error: assertion failed: Could not find state for path ,这是因为你没有与路径“/”匹配的叶状态。

root.index matches '/' but it isn't a leaf, it has a single child state batches so the router will look in the children of root.index for a match for '/' and it doesn't find one, it only finds batches (which matches '/batches' ) and the error is thrown. root.index匹配'/',但它不是一个叶子,它有一个子状态batches所以路由器将在root.index的子项中root.index '/'的匹配,但它找不到一个,它只查找batches (与'/batches'匹配)并抛出错误。 There has to be a leaf matching the route path. 必须有一个与路径路径匹配的叶子。

As for helping with what you're actually trying to do, it looks like you want "/" to redirect to "/batches"? 至于帮助你实际尝试做什么,看起来你想要“/”重定向到“/批次”? If that's the case: 如果是这样的话:

Platby.Router = Em.Router.extend
  root: Em.Route.extend
    index: Em.Route.extend
      route: '/'
      redirectsTo: 'batches'
    batches: Em.Route.extend
      route: '/batches'
      connectOutlets: ...

You can use redirectsTo as above, which is a shortcut for: 你可以像上面一样使用redirectsTo ,这是一个快捷方式:

Platby.Router = Em.Router.extend
  root: Em.Route.extend
    index: Em.Route.extend
      route: '/'
      connectOutlets: (router) ->
        router.transitionTo('batches')
    batches: Em.Route.extend
      route: '/batches'
      connectOutlets: ...

You can't use redirectsTo and have your own connectOutlets however, so if you need to do anything in root.index before transitioning to root.batches you will want to use the second way and handle your business before the transitionTo 你不能使用redirectsTo并拥有自己的connectOutlets ,所以如果你需要在转换到root.batches之前在root.index做任何事情,你将需要使用第二种方式并在transitionTo之前处理你的业务。

I'm trying to understand what it is you want to do, but by the looks of it you want /batches/ to route to root.index.batches.index and /batches to route to root.index.batches . 我试图了解你想要做什么,但是通过它的外观,你想要/batches/路由到root.index.batches.index/batches以路由到root.index.batches Is that correct? 那是对的吗?

My understanding is that your root.index.batches and root.index.batches.index routes are actually the same route. 我的理解是你的root.index.batchesroot.index.batches.index路由实际上是相同的路由。 If you remove the latter you should be good. 如果你删除后者你应该是好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM