有一個不錯的q &一個在這裏會使用localStorage和angular2。 https://stackoverflow.com/a/39098748/6203604角通用啓動,localStorage的生產中:在browser.module使用「窗口」指本地存儲中斷生產建設
這種做法似乎好工作與「新公共管理的開始」。然而,當你運行「故宮運行生成」錯誤:
Property 'window' does not exist on type 'typeof ../browser.module' appears.
在「通用陷阱」上https://github.com/angular/universal-starter它說
window, document, navigator, and other browser types - do not exist on the server - so using them, or any library that uses them (jQuery for example) will not work. You do have some options, if you truly need some of this functionality: If you need to use them, consider limiting them to only your main.client and wrapping them situationally with the imported isBrowser/isNode features from Universal. import { isBrowser, isNode } from 'angular2-universal'; Another option is using DOM from "@angular/platform-browser"
會的isBrowser包裝或使用該平臺的瀏覽器適合在這裏指localStorage的?任何如何最好的寫這個例子?
e.g在browser.module.ts,是有什麼事應該做包裝window.localStorage的分配管束?
import {LocalStorage} from './local-storage.ts';
export function ngApp() {
return bootstrap(App, [
// ...
UserService,
{ provide: LocalStorage, useValue: window.localStorage}
]);
有些事情要與'npm run build'可分發的捆綁包一起使用嗎?
這似乎來自文件node.module.ts中useValue中的'u',因爲disapears並被替換爲'沒有Token LocalStorage提供程序!'
...
providers: [
// ...
UserService,
{provide: LocalStorage, useValue: {getItem() {} }}
]
...
任何人都得到類似的東西。想法?
感謝伊薩克,我給它一個去這個週末,讓你知道如何去。 – owenc