提前感谢您的帮助。我试图通过打字稿获取Angular 2中的本地存储访问。我正在使用npm包angular2-localstorage。我有angular.io“英雄之旅”的工作(没有太多的造型)。我按照https://www.npmjs.com/package/angular2-localstorage的说明进行操作。将建议的代码添加到我的app.module.ts文件后,我尝试使用npm start
运行,并获得以下编译错误:
node_modules/angular2-localstorage/LocalStorageEmitter.ts(46,9): error TS2305: Module '"/Users/joshuaforman/Documents/CodeCraft/a2quickstart/a2-angular/node_modules/@angular/core/src/facade/lang"' has no exported member 'Type'.
node_modules/angular2-localstorage/LocalStorageEmitter.ts(47,9): error TS2305: Module '"/Users/joshuaforman/Documents/CodeCraft/a2quickstart/a2-angular/node_modules/@angular/core/src/di"' has no exported member 'provide'.
您可以在此处查看所有代码:https://github.com/joshuaforman/angular2-quickstart
此外,这里是完整的app.module.ts文件(我改变的唯一一个导致错误发生的文件):
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpModule } from "@angular/http";
// added for LocalStorage
// followed instructions here: https://www.npmjs.com/package/angular2-localstorage
import { LocalStorageService, LocalStorageSubscriber } from "angular2-localstorage/LocalStorageEmitter";
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from "angular2-in-memory-web-api";
import { InMemoryDataService } from "./in-memory-data.service";
import { AppComponent } from "./app.component";
import { HeroDetailComponent } from "./hero-detail.component";
import { HeroesComponent } from "./heroes.component";
import { HeroService } from "./hero.service";
import { DashboardComponent } from "./dashboard.component";
import { routing } from "./app.routing";
@NgModule({
imports: [
BrowserModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService),
FormsModule,
routing
],
declarations: [
AppComponent,
HeroesComponent,
HeroDetailComponent,
DashboardComponent
],
bootstrap: [ AppComponent ],
providers: [
HeroService,
LocalStorageService // added for Local Storage
]
})
export class AppModule {
// added for Local Storage
constructor(storageService: LocalStorageService) {}
}
我对我添加的每一行都有一条评论,用于实现包含文本“LocalStorage”的本地存储。如果你注释掉这三行,编译和运行时工作正常。
此时,我只是希望能够成功编译。感谢。
注意:这是我在堆栈上的第一个问题。我过去在这里找到了很多答案,并且我已经尽力遵循惯例来提出这个问题。欢迎提出任何有关我应该采取不同举措的有效反馈。
答案 0 :(得分:5)
不再维护Angular2-LocalStorage,你必须使用另一个库,直到这个库得到一个活跃的维护者。我推荐Angular2 Cool Storage
答案 1 :(得分:0)
将此行替换为" LocalStorageEmitter.ts"来自node_modeule" angular2-localstorage"
import {Type} from "@angular/core/src/facade/lang";
import {provide} from "@angular/core/src/di";
import {ComponentRef} from "@angular/core";
export function LocalStorageSubscriber(appPromise: Promise<ComponentRef<any>>) {
appPromise.then((bla) => {
bla.injector.get(<Type>LocalStorageService);
});
}
有了这个。
import {Type} from "@angular/core/src/type";
import {Provider} from "@angular/core/src/di";
import {ComponentRef} from "@angular/core";
export function LocalStorageSubscriber(appPromise: Promise<ComponentRef<any>>) {
appPromise.then((bla) => {
bla.injector.get(<Type<any>>LocalStorageService);
});
}
它的作品对我来说......
了解更多详情=&gt; https://github.com/marcj/angular2-localstorage/issues/66