【问题标题】:Nested routes in Ember.js router and initialStateEmber.js 路由器和 initialState 中的嵌套路由
【发布时间】:2012-11-18 14:33:46
【问题描述】:

我正在尝试了解 emeber.js 路由。我不太确定,为什么这不是路由器的有效定义

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'

一旦进入根 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  

谁能解释一下,问题出在哪里?

【问题讨论】:

  • 查看您的代码,我没有看到您调用connectOutlets。您可以查看 StackOverflow 上有关路由器的其他一些问题...例如,您可能想查看 this question 以及 this guide
  • 这段代码只是sn-p,展示思路。还是谢谢你。

标签: javascript coffeescript ember.js ember-old-router


【解决方案1】:

我只能根据您提供的信息来回答...关于错误uncaught Error: assertion failed: Could not find state for path,这是因为您没有与路径“/”匹配的叶子状态。

root.index 匹配 '/' 但它不是叶子,它有一个子状态 batches 所以路由器将在 root.index 的子状态中查找匹配 '/' 并且它没有找不到一个,它只找到batches(匹配'/batches')并抛出错误。必须有一个与路由路径匹配的叶子。

至于帮助您实际尝试做的事情,您似乎希望“/”重定向到“/batches”?如果是这样的话:

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

你可以像上面一样使用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: ...

但是,您不能使用redirectsTo 并拥有自己的connectOutlets,因此如果您需要在转换到root.batches 之前在root.index 中做任何事情,您将需要使用第二种方式并在之前处理您的业务transitionTo

【讨论】:

  • 谢谢,这对我很有帮助。
【解决方案2】:

我试图了解您想要做什么,但从外观上看,您希望 /batches/ 路由到 root.index.batches.index/batches 路由到 root.index.batches。那是对的吗?

我的理解是,您的root.index.batchesroot.index.batches.index 路线实际上是同一条路线。如果您删除后者,您应该会很好。

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2016-08-18
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多