2014-10-02 42 views
2

我想在渲染任何路線後將頁面的標題更改爲該路線的名稱。這是我以前使用的代碼:Router.after不推薦使用,掛鉤後是否有替換全局?

Router.after(function(){document.title = this.route.name;}); 

manual提及使用onAfterAction個別路線裏面,但我想在全球範圍內做到這一點?

+0

嗨,你可以請閱讀本指南(http://stackoverflow.com/help/how-to-ask)。在你的問題中包含儘可能多的相關信息和代碼,並且很可能會得到很好的答案:) – 2014-10-02 22:15:01

回答

1

你一定錯過了這一點:http://eventedmind.github.io/iron-router/#using-hooks

正確的語法很簡單:

Router.onAfterAction(function(){ 
    // your hook definition 
}); 

注意:該指南鐵:[email protected]必須添加到您的應用程序明確這樣的:

meteor add iron:[email protected] 

Router.onAfterActioniron:[email protected]正常工作了。

我建議使用this.route.getName(),而不是this.route.name,見此處詳細瞭解此問題:

https://github.com/EventedMind/iron-router/issues/878

+0

由於某種原因,我的一個路由沒有函數this.route.getName。所以我現在使用這個代碼: 'Router.onAfterAction(function(){document.title = this.route.getName?this.route.getName():this.route.name;});'它是現在爲所有路線工作。缺少getName()函數的路由定義如下:''''''''''''''''' ,數百行數據並需要幾秒鐘才能完成,因此渲染路線的延遲可能是原因。 – user1862165 2014-10-03 09:29:24

0

router.after();方法雖然已被棄用仍將在代碼被允許在語法方面。正如IRC聊天中所討論的,最好的方法是使用新的語法。

Router.onAfterAction(function(){ 
    document.title = this.route.name; 
}); 

這應該可以解決您要查找的內容。

乾杯!

Ryan