app.module 中的 NativeScript 导入 NGXS 模块在日志控制台中抛出错误
NativeScript Importing NGXS module in app.module throw an error in log console
我是 NativeScript 的新手,我想使用状态管理 NGXS
并在我的应用程序中实施。我安装了 NGXS
和 NPM
:@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
下如果我取消注释 PetState
它会在控制台日志中抛出错误,无法找到模块:“./pet.actions”。请参考
如果我取消注释 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"
}
好的,我找到了解决方案。几个小时后,我找到了这个解决方案 here 以及我的解决方案
只需删除 node_modules
、hook
和 platform
文件夹,然后 npm i
并为 ios 和 [ 添加 platform
=26=].
还有一件事我不确定为什么,我卸载了 @ngxs/logger-plugin
和 @ngxs/devtools-plugin
并重新安装回不在开发依赖中的依赖。
无论如何,现在它对我有用。
我是 NativeScript 的新手,我想使用状态管理 NGXS
并在我的应用程序中实施。我安装了 NGXS
和 NPM
:@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
下如果我取消注释PetState
它会在控制台日志中抛出错误,无法找到模块:“./pet.actions”。请参考如果我取消注释
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"
}
好的,我找到了解决方案。几个小时后,我找到了这个解决方案 here 以及我的解决方案
只需删除 node_modules
、hook
和 platform
文件夹,然后 npm i
并为 ios 和 [ 添加 platform
=26=].
还有一件事我不确定为什么,我卸载了 @ngxs/logger-plugin
和 @ngxs/devtools-plugin
并重新安装回不在开发依赖中的依赖。
无论如何,现在它对我有用。