2015-04-01 69 views
1

我嘗試使用route_hierarchical/client.dart中的路由器來偵聽onpopstate事件並在我的index.html中啓用/禁用<div>。 (在stagehand.pub dart插件中的示例) 如果這是通過index.html中的正常<a href="/relativePath">完成的,它可以工作。 但如果我嘗試通過button.onClick.listen()處理程序中,我打電話更改路徑:window.location.assign()不被路由器在飛鏢中捕獲

window.location.assign('/relativePath'); 

我得到404和路由器不正確地處理我的事件。 應該說那個動作不會調用被描述爲here的路由器捕獲的popstate event

handlers.dart

... 
button.onClick.listen((_){ 
    window.location.assign('/about'); 
}); 

... 

router.dart

var router = new Router(); 
    router.root 
    ..addRoute(name: 'about', path: '/about', enter: showAbout) 
    ..addRoute(name: 'login', defaultRoute: true, path: '/', enter: showLogin) 
    ..addRoute(name: 'context', path: '/context', enter: showContext); 
    router.listen(); 
} 

void showAbout(RouteEvent e) { 
    // Extremely simple and non-scalable way to show different views. 
    querySelector('#login').style.display = 'none'; 
    querySelector('#about').style.display = ''; 
    querySelector('#context').style.display = 'none'; 
} ... 

的index.html

... 
<form> 
    <button type="button" id="submit" disabled="true" > 
     Login 
    </button> 
</form> 
... 
+0

看起來像一個bug,請在http://dartbug.com報告)。您可以嘗試'window.location.href = xxx'(另請參閱http://stackoverflow.com/questions/13109233) – 2015-04-01 14:07:14

+0

感謝您的回覆,我之前看到過這篇文章,但不幸的是,href setter也無法正常工作: - ( – raptaML 2015-04-01 14:31:14

回答

1

好像看起來像我沒有實現我的目標與假設上述行爲。

感謝GüntherZöchbauer的幫助。 我提交了相應的Github project,因爲我認爲它應該可以工作。

我現在使用的是什麼工作,包括歷史的支持是

router.gotoUrl('/relativePath')

在onButtonClick處理

。 完全做到了。

1

onPopState是錯誤的事件。如果您導航到現有的歷史記錄(回退,轉發,pushState,轉到歷史記錄中的第二個條目),則只會觸發此事件。 你在找什麼可能是window.onHashChange事件。

+0

看起來像我得到了錯誤,但不應該由路由器進行內部處理? – raptaML 2015-04-01 14:45:43

+0

這取決於路由器配置(我不是這個問題的專家)你可以啓用usePushState。根據這個設置你必須使用URL片段('http:// example.com/index.html#someroute')或者表示路由的整個URL(例如'http:// example.com/someroute'),如果您加載資源,則需要服務器支持。你需要根據這個方案匹配一條路線 – 2015-04-01 14:50:05

+0

如果直接使用route_hierarchical,當你從Angular.dart使用它時,它是'useFragment'而不是'usePushState'(參見例如https://github.com/angular/route .dart /斑點/ cc252ffd3 7944a438f707482fa3bd8aa98a9ae59 /示例/基本/ example.dart#L15)。 – 2015-04-01 14:58:00