【问题标题】:TypeError: Cannot read property 'module' of undefinedTypeError:无法读取未定义的属性“模块”
【发布时间】:2018-12-20 03:43:22
【问题描述】:

我是 Angular 的新手,我试图在我的应用程序中使用 localStorage API。每当我运行应用程序时,它都会在控制台中抛出错误 TypeError: localStorage.getItems is not a function。然后我在npm的帮助下下载了angular-local-storage。我还在app.module.ts中导入了LocalStorageModule并将其添加到导入数组中。但是,现在它抛出了这个错误:

angular-localstorage.js:24 Uncaught TypeError: Cannot read property 'module' of undefined
at TEST (angular-localstorage.js:24)
at eval (angular-localstorage.js:198)
at Object.../../../../angular-localstorage/angular-localstorage.js (vendor.bundle.js:206)
at __webpack_require__ (inline.bundle.js:55)
at eval (index.js:1)
at Object.../../../../angular-localstorage/index.js (vendor.bundle.js:213)
at __webpack_require__ (inline.bundle.js:55)
at eval (app.module.ts:7)
at Object.../../../../../src/app/app.module.ts (main.bundle.js:36)
at __webpack_require__ (inline.bundle.js:55)<br/>

包含getItem函数的代码:

    export class Init{
    load(){
        if(localStorage.getItem('markers')===null || localStorage.getItem('markers')===undefined)
        {
            console.log('No markers found...creating...');

            var markers=[

  {
    name:'Company One',
    lat:42.825588,
    lng:-71.018029,
    draggable:true
  },
 {
    name:'Company Two',
    lat:42.868170,
    lng:-70.889071,
    draggable:false
  },
  {
    name:'Company Three',
    lat:42.858279,
    lng:-71.930498,
    draggable:true
  }
  ];
        localStorage.setItem('markers',JSON.stringify(markers));


        }
        else
        {
            console.log('Loading markers...')
        }
    }
}

app.module.ts 中的代码:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import {MarkerService} from './services/marker.service';
import {LocalStorageModule} from 'angular-localstorage';
import { AgmCoreModule } from '@agm/core';

@NgModule({
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    LocalStorageModule,
    AgmCoreModule.forRoot({
      apiKey:''
    })
  ],
  providers: [MarkerService],
  declarations: [ AppComponent ],
  bootstrap: [ AppComponent ]
})
export class AppModule {}

谁能帮我解决这个问题。
提前致谢。

【问题讨论】:

  • 发布您的代码,带有 getItems() 和 app.module 的位 - 这将帮助人们帮助您。
  • 我已添加代码

标签: angular npm


【解决方案1】:

localstorage API 内置在 Angular 框架中,您不需要为此使用外部库。你可以通过localStorage.&lt;yourMethodName&gt;直接在你的代码中使用它

此外,没有任何称为getItems 的方法。

可用方法列表是...

getItem removeItem setItem clear

More Info Here

【讨论】:

  • 实际上我在一个继承了我在这里发布的代码的类中打错了。
【解决方案2】:

标准的localStorage API应该可以正常工作,不需要为此安装模块,使用通常的方式获取和设置localStorage中的项目:

localStorage.setItem('name', 'ABC');
localStorage.getItem('name');

【讨论】:

  • 看我的回答,你不需要单独的模块 angular-local-storage。标准 API 应该为您完成这项工作。
  • 删除angular-local-storage 给出错误:TypeError: localStorage.getItems is not a function
  • 您需要将“dom”添加到 tsconfig.json 中的“lib”数组中。
猜你喜欢
  • 2017-05-17
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2017-09-14
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多