2016-11-02 70 views

回答

0

每次您更改模型堅持它的localStorage,然後是OnInit讀回。

對角2的localStorage: https://www.npmjs.com/package/angular-2-local-storage

只是把它注入到你的組件和模塊

import { LocalStorageService, LOCAL_STORAGE_SERVICE_CONFIG } from 'angular-2-local-storage'; 

// Create config options (see ILocalStorageServiceConfigOptions) for deets: 
let localStorageServiceConfig = { 
    prefix: 'my-app', 
    storageType: 'sessionStorage' 
}; 

// Use the provider: 
@NgModule({ 
    imports: [ 
     .. 
    ], 
    declarations: [ 
     .. 
    ], 
    providers: [ 
     LocalStorageService, 
     { 
      provide: LOCAL_STORAGE_SERVICE_CONFIG, useValue: localStorageServiceConfig 
     } 
    ], 
    bootstrap: [AppComponent] 
}) 


import { LocalStorageService } from 'angular-2-local-storage'; 

@Component({ 
    // ...  
}) 
export class SomeComponent { 
    constructor (
     private localStorageService: LocalStorageService 
    ) { 
     // YAY! 
    } 
} 
+0

你有例子的localStorage的?任何文檔鏈接都會有所幫助。謝謝! – KartDev

相關問題