[英]Nested routes in Ember.js router and initialState
我正在嘗試了解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
將有人能夠解釋我,問題出在哪里?
我只能根據你給出的信息回答...關於錯誤uncaught Error: assertion failed: Could not find state for path
,這是因為你沒有與路徑“/”匹配的葉狀態。
root.index
匹配'/',但它不是一個葉子,它有一個子狀態batches
所以路由器將在root.index
的子項中root.index
'/'的匹配,但它找不到一個,它只查找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
之前處理你的業務。
我試圖了解你想要做什么,但是通過它的外觀,你想要/batches/
路由到root.index.batches.index
和/batches
以路由到root.index.batches
。 那是對的嗎?
我的理解是你的root.index.batches
和root.index.batches.index
路由實際上是相同的路由。 如果你刪除后者你應該是好的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.