将app.module中的NativeScript导入NGXS模块在日志控制台中引发错误

时间:2019-04-04 05:13:54

标签: nativescript nativescript-angular ngxs

我是NativeScript的新手,我想使用状态管理NGXS并实现到我的应用程序。我已将NGXSNPM一起安装:@ngxs/store @ngxs/logger-plugin@ngxs/devtools-plugin

所以我将那些NGXS模块添加到了我的app.module。

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";
import { NativeScriptUISideDrawerModule } from "nativescript-ui-sidedrawer/angular";

import { NgxsModule } from '@ngxs/store';
import { NgxsLoggerPluginModule } from '@ngxs/logger-plugin';
import { NgxsReduxDevtoolsPluginModule } from '@ngxs/devtools-plugin';

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { PetState } from './_ngxs/pet/pet.state';

@NgModule({
  bootstrap: [
    AppComponent
  ],
  imports: [
    AppRoutingModule,
    NativeScriptModule,
    NativeScriptUISideDrawerModule,
    NgxsModule.forRoot([
        //if i uncomment PetState, in console log show me error, Failed to find module: "./pet.actions"
        // PetState
    ]),
    //if i uncomment `NgxsLoggerPluginModule.forRoot()`, in console log show me error, Failed to find module: "@ngxs/logger-plugin
    // NgxsLoggerPluginModule.forRoot(),
    //if i uncomment `NgxsReduxDevtoolsPluginModule.forRoot()`, in console log show me error, Failed to find module: "@ngxs/devstool-plugin
    // NgxsReduxDevtoolsPluginModule.forRoot() 
  ],
  declarations: [
    AppComponent
  ],
  schemas: [
    NO_ERRORS_SCHEMA
  ]
})
export class AppModule { }

添加ngxs模块后,我遇到了2个问题。

    NgxsModule.forRoot下的
  1. 如果我取消注释PetState,则会在控制台日志中引发我错误,找不到模块:“ ./ pet.actions”。请参阅

  2. 如果我取消注释NgxsLoggerPluginModule.forRoot() / NgxsReduxDevtoolsPluginModule.forRoot(),则会在控制台日志中出现错误,并说failed to find module @ngxs/logger-plugin / @ngxs/devstool-plugin

下面是pet.state.ts

import { State, Action, Selector, StateContext } from '@ngxs/store';
import { Pet } from './pet.model';
import { AddPet, RemovePet } from './pet.actions';

export class PetStateModel {
  pets: Pet[];
}

@State<PetStateModel>({
  name: 'Pet',
  defaults: {
    pets: []
  }
})

export class PetState {

  @Selector()
  static getPet(state: PetStateModel) {
    return state.pets;
  }

  @Action(AddPet)
  addPet({getState, patchState}: StateContext<PetStateModel>, { payload }: AddPet) {
    const state = getState();
    patchState({
        pets: [...state.pets, payload]
    })
  }

  @Action(RemovePet)
  removePet({getState, patchState}: StateContext<PetStateModel>, { payload }: RemovePet) {
    const state = getState();
    patchState({
        pets: state.pets.filter(a => a.name !== payload )
    })
  }
}

我真的需要有人可以帮我解决问题, 谢谢,

已更新 这是我的package.json

{
  "nativescript": {
    "id": "org.nativescript.pledgeCareSample",
    "tns-android": {
      "version": "5.2.1"
    },
    "tns-ios": {
      "version": "5.2.0"
    }
  },
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "repository": "<fill-your-repository-here>",
  "scripts": {
    "lint": "tslint \"src/**/*.ts\""
  },
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/http": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@ngxs/storage-plugin": "^3.4.3",
    "@ngxs/store": "^3.4.3",
    "nativescript-angular": "~7.2.1",
    "nativescript-theme-core": "~1.0.4",
    "nativescript-ui-sidedrawer": "~5.1.0",
    "nativescript-unit-test-runner": "^0.6.0",
    "reflect-metadata": "~0.1.12",
    "rxjs": "~6.3.0",
    "tns-core-modules": "~5.2.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~7.2.0",
    "@nativescript/schematics": "~0.5.0",
    "@ngtools/webpack": "~7.2.0",
    "@ngxs/devtools-plugin": "^3.4.3",
    "@ngxs/logger-plugin": "^3.4.3",
    "@types/jasmine": "^3.3.12",
    "codelyzer": "~4.5.0",
    "karma": "4.0.1",
    "karma-jasmine": "2.0.1",
    "karma-nativescript-launcher": "0.4.0",
    "nativescript-dev-sass": "~1.6.0",
    "nativescript-dev-typescript": "~0.8.0",
    "nativescript-dev-webpack": "~0.20.0",
    "tslint": "~5.11.0"
  },
  "gitHead": "f548ec926e75201ab1b7c4a3a7ceefe7a4db15af",
  "readme": "NativeScript Application"
}

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。几个小时后,我找到了此解决方案here以及我的解决方案

只需删除node_moduleshookplatform文件夹,然后删除npm i并为iOS和android添加platform

还有另外1件事,我不确定为什么,我为@ngxs/logger-plugin@ngxs/devtools-plugin进行了卸载,然后重新安装到非dev依赖的依赖中。

无论如何,现在它对我有用。