Aurelia - 歷史



在本章中,你將學習如何使用aurelia-history外掛。

步驟 1 - 安裝外掛

該外掛已作為標準配置的一部分提供。如果你已經將aurelia.use.standardConfiguration()設為手動配置的一部分,你可以開始了。

main.js

export function configure(aurelia) {
   aurelia.use
   .standardConfiguration()
   .developmentLogging();

   aurelia.start().then(() => aurelia.setRoot());
}

步驟 2 - 使用歷史記錄

我們將使用上一章(Aurelia - 路由)的示例。如果我們想要設定返回或前進功能,我們可以使用帶有back()forward()方法的history物件。我們會在路由配置之後新增這個功能。

app.js

export class App {
   configureRouter(config, router) {
      config.title = 'Aurelia';
      config.map([
         { route: ['','home'],  name: 'home',  
            moduleId: './pages/home/home',  nav: true, title:'Home' },
         { route: 'about',  name: 'about',    
            moduleId: './pages/about/about',    nav: true, title:'About' }
      ]);
      this.router = router;
   }
   goBack() {
      history.back();
   }
	goForward() {
      history.forward();
   }
}

現在,讓我們在我們view中新增兩個按鈕。

app.html

<template>
   <nav>
      <ul>
         <li repeat.for = "row of router.navigation">      
            <a href.bind = "row.href">${row.title}</a>
         </li>
      </ul>
   </nav>
	
   <button click.delegate = "goBack()"></button> 
   //The button used for navigationg back...
	
   <button click.delegate = "goForward()"></button> 
   //The button used for navigationg forward...
	
   <router-view></router-view>
</template>

使用者可以透過點選我們新增的按鈕來返回和前進。

Aurelia History Example
廣告
© . All rights reserved.