Search code examples
nativescriptnativescript-angularngxs

NativeScript Importing NGXS module in app.module throw an error in log console


I'm new with NativeScript and I want to use a state management NGXS and implement to my app. I have installed NGXS with NPM: @ngxs/store @ngxs/logger-plugin and @ngxs/devtools-plugin.

So I added those NGXS module to my 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 { }

After I added the ngxs module, I got 2 issues.

  1. under NgxsModule.forRoot if I uncomment PetState it throw me error in console log, Failed to find module: "./pet.actions". Please refer

  2. If I uncomment NgxsLoggerPluginModule.forRoot() / NgxsReduxDevtoolsPluginModule.forRoot() I will get error in console log and saying failed to find module @ngxs/logger-plugin / @ngxs/devstool-plugin

below is pet.state.ts codes

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 )
    })
  }
}

I really need someone can give some hand to help me fix the issues, thanks,

Updated here is my 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"
}

Solution

  • Ok I found the solution. After few hours I found this solution here and with my solutions

    Just need to remove node_modules, hook and platform folder then npm i and add the platform for both ios and android.

    and 1 more thing I am not sure why, I uninstalled for @ngxs/logger-plugin and @ngxs/devtools-plugin and reinstall back to the dependency not in dev dependency.

    anyway, for now it work for me.