[英]Director.js and Turbolinks - changing the window hash and using then back
[英]Perform a function based on the hash with director.js
我正在嘗試使用Director.js在我的單頁應用程序上進行路由。 問題是,當您通過在地址欄中鍵入頁面進入頁面時,應用程序不執行路由表中的哈希指向的功能。
這是github上的示例 -如果我刷新帶有哈希的頁面或直接轉到帶有哈希的頁面,則控制台中不會顯示任何內容。 但是,如果我單擊鏈接控制台之一,則會顯示輸出。 如何更改此代碼,以便如果用戶直接轉到鏈接,則路由表會檢查哈希並從路由表執行其匹配功能? 請回答利用Director.js的方法
<html>
<head>
<meta charset="utf-8">
<title>A Gentle Introduction</title>
<script src="https://raw.github.com/flatiron/director/master/build/director.min.js"></script>
<script>
var author = function () { console.log("author"); },
books = function () { console.log("books"); },
viewBook = function(bookId) { console.log("viewBook: bookId is populated: " + bookId); };
var routes = {
'/author': author,
'/books': [books, function() { console.log("An inline route handler."); }],
'/books/view/:bookId': viewBook
};
var router = Router(routes);
router.init();
</script>
</head>
<body>
<ul>
<li><a href="#/author">#/author</a></li>
<li><a href="#/books">#/books</a></li>
<li><a href="#/books/view/1">#/books/view/1</a></li>
</ul>
</body>
</html>
我沒有使用Director,但是您的問題可能是GitHub提供了MIME類型為“ text / plain”的“ raw”文件。 因此,Director js代碼可能無法作為javascript處理,從而導致您的測試頁無法正常工作。 嘗試下載自己的Director副本並鏈接到本地版本,而不是GitHub上的版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.