2016-06-14 47 views
3

我想將網頁保存在內存中,這樣當我點擊後退按鈕(而不是網頁瀏覽器上的)或路由器鏈接時,如果我已經訪問過它,HTML頁面會立即加載(因爲我有一些數據要加載,我不想重新加載)。使用Angular2將HTML頁存儲在內存中

我見過一種帶有標籤界面的方法: https://www.w3.org/Style/Examples/007/target.en.html#tab1 但它不適合我的代碼架構。

我正在使用 in angular2來導航拋出頁面和一個返回按鈕調用一個功能點擊轉到上一頁。

我儘可能詳細地介紹了我的代碼,以便人們更好地理解我的代碼架構和routerlink方法的工作方式。

後退按鈕功能(從routerlink獨立工作):

​​

從頁面1所述的路由器鏈接方法2頁:

page1.html:

<a[routerLink]="['PAGE2']"> go to page 2</a> 

page1.ts組件:

import { Router, ROUTER_DIRECTIVES } from '@angular/router-deprecated'; 
@Component({ 
selector: 'page1', 
templateUrl: 'page1.html', 
styleUrls: ['page1.css'], 
directives: [ROUTER_DIRECTIVES] 
}) 

main.ts:

import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated'; 

@Component({ 
providers: [ROUTER_PROVIDERS] 
}) 

@RouteConfig([ 
{ path: '/page2', name: 'PAGE2', component: Page2}]) //component representing class Page2 in page2.ts 

任何想法,以幫助我管理它受到歡迎,感謝由前進!

回答

1

那麼,對於那些有同樣的問題,我想管理它是將數據映射到localStorage key這樣的最佳方式:

localStorage.setItem('favoris',JSON.stringify(my_array)); //set my data array 
    array = JSON.parse(localStorage.getItem('key_name')); //get in array 

然後ngOnInit路由器所調用的類會根據localStorage key是否爲真來調用初始函數。