国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

用director.js實現前端路由使用實例

2019-11-19 17:46:40
字體:
來源:轉載
供稿:網友

director.js是什么?

理解:前端的route框架,director.js客戶端的路由注冊/解析器,在不刷新的情況下,利用“#”號組織不同的URL路徑,并根據不同的URL路徑進行不同的方法調用。意思就是有什么樣的路徑就有什么樣的方法。

場合:客戶端瀏覽器和node.js的服務器應用。非常適合用來開發不需要刷新的單頁面應用程序以及node.js應用。

兼容性:不依賴與任何庫。例如jquery等。但它又和jquery能很好的融合在一起;

客戶端的路由:

客戶端的路由 (也稱為哈希路由) 允許您指定一些關于使用URL應用狀態的信息,當用戶指定固定的URL,進行相應的頁面顯示。

簡單例子

1. 單獨使用

<!DOCTYPE html><html> <head>  <meta charset="utf-8">  <title>A Gentle Introduction</title>  <script   src="https://rawgit.com/flatiron/director/master/build/director.min.js">  </script>  <script>   var author = function () { console.log("author"); };   var books = function () { console.log("books"); };   var 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> 

2當與jquery相結合

<!DOCTYPE html><html> <head>  <meta charset="utf-8">  <title>A Gentle Introduction 2</title>  <script   src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js">  </script>  <script   src="https://rawgit.com/flatiron/director/master/build/director.min.js">  </script>  <script>  $('document').ready(function() {   //   // create some functions to be executed when   // the correct route is issued by the user.   //   var showAuthorInfo = function () { console.log("showAuthorInfo"); };   var listBooks = function () { console.log("listBooks"); };   var allroutes = function() {    var route = window.location.hash.slice(2);    var sections = $('section');    var section;    section = sections.filter('[data-route=' + route + ']');    if (section.length) {     sections.hide(250);     section.show(250);    }   };   //   // define the routing table.   //   var routes = {    '/author': showAuthorInfo,    '/books': listBooks   };   //   // instantiate the router.   //   var router = Router(routes);   //   // a global configuration setting.   //   router.configure({    on: allroutes   });   router.init();  });  </script> </head> <body>  <section data-route="author">Author Name</section>  <section data-route="books">Book1, Book2, Book3</section>  <ul>   <li><a href="#/author">#/author</a></li>   <li><a href="#/books">#/books</a></li>  </ul> </body></html> 

Director支持commond的書寫方式

例子如下:

 var director = require('director'); var router = new director.cli.Router(); router.on('create', function () {  console.log('create something'); }); router.on(/destroy/, function () {  console.log('destroy something'); }); // You will need to dispatch the cli arguments yourself router.dispatch('on', process.argv.slice(2).join(' ')); 

初始化及路由器的注冊

 var router = Router(routes); 

另外,構造方法中傳入的routes參數是一個路由對象,它是一個具有鍵值對結構的對象,可以被多層的嵌套。鍵對對應的URL中傳入的路徑,一般一個鍵值對應按照分割符切割后的某一部分;而鍵值對的值對應的該路徑的需要觸發的回調函數名。回調函數要在路由表對象使用前先聲明,否則js會報錯。

另外,回調函數除非特殊情況,一般不推薦使用匿名函數,請盡量先聲明后使用。

   var routes = {  '/dog': bark,    '/cat': [meow, scratch] }; 

這里的的url是#dog和#cat

聲明Router對象后,需要調用init()方法進行初始化,如:

router.init(); 

路由的事件

路由事件是路由注冊表中一個有固定命名的屬性,是指當路由方法router.dispatch()被調用時,路由匹配成功的時定義的需要觸發的回調方法(允許定義多個回調方法)。上文即時注冊功能里的"on"方法就是一個事件。具體信息如下:  

on :當路由匹配成功后,需要執行的方法

before:在觸發“on”方法之前執行的方法

僅在客戶端有效的方法:

after:當離開當前注冊路徑時,需要執行的方法

once: 當前注冊路徑僅執行一次的方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绍兴市| 上栗县| 赤峰市| 霍城县| 保山市| 余江县| 赫章县| 南郑县| 凤阳县| 那曲县| 黄骅市| 平陆县| 朝阳市| 晋中市| 白城市| 澎湖县| 剑河县| 新蔡县| 安仁县| 南安市| 大厂| 云安县| 赤峰市| 象山县| 彩票| 辽阳市| 隆回县| 荔波县| 汶川县| 夏河县| 江达县| 衡东县| 沈丘县| 丰台区| 宽甸| 临沂市| 深州市| 宁远县| 安乡县| 桓台县| 余干县|