Angular-错误错误:未被发现(在承诺中):TypeError:Storemodule.Forroot()称为两次
在Angular-13项目中,我正在Coremodule中实现 @ngrx/Store进行布局自定义。
如下图所示:
-
-
然后, @ngrx/store也将在Coremodule中实现。另外,在施工中导入曲线模型。
state.ts:
import {UiState} from './ui/state';
export interface AppState {
auth: any;
ui: UiState;
}
reducer.ts:
import {
NAVBAR_DARK_VARIANTS,
NAVBAR_LIGHT_VARIANTS,
SIDEBAR_DARK_SKINS,
SIDEBAR_LIGHT_SKINS
} from '../../utils/themes';
import * as Actions from './actions';
import {UiAction} from './actions';
import initialState, {UiState} from './state';
export function uiReducer(state: UiState = initialState, action: UiAction) {
switch (action.type) {
case Actions.TOGGLE_SIDEBAR_MENU:
return {
...state,
menuSidebarCollapsed: !state.menuSidebarCollapsed
};
case Actions.TOGGLE_CONTROL_SIDEBAR:
return {
...state,
controlSidebarCollapsed: !state.controlSidebarCollapsed
};
case Actions.TOGGLE_DARK_MODE:
let variant: string;
let skin: string;
if (state.darkMode) {
variant = NAVBAR_LIGHT_VARIANTS[0].value;
skin = SIDEBAR_LIGHT_SKINS[0].value;
} else {
variant = NAVBAR_DARK_VARIANTS[0].value;
skin = SIDEBAR_DARK_SKINS[0].value;
}
return {
...state,
navbarVariant: variant,
sidebarSkin: skin,
darkMode: !state.darkMode
};
case Actions.SET_NAVBAR_VARIANT:
let navbarVariant: string;
if (state.darkMode) {
navbarVariant = action.payload || NAVBAR_DARK_VARIANTS[0].value;
} else {
navbarVariant =
action.payload || NAVBAR_LIGHT_VARIANTS[0].value;
}
return {
...state,
navbarVariant
};
case Actions.SET_SIDEBAR_SKIN:
let sidebarSkin: string;
if (state.darkMode) {
sidebarSkin = action.payload || SIDEBAR_DARK_SKINS[0].value;
} else {
sidebarSkin = action.payload || SIDEBAR_LIGHT_SKINS[0].value;
}
return {
...state,
sidebarSkin
};
default:
return state;
}
}
core.module:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { AdminHeaderComponent } from './components/layouts/admin-header/admin-header.component';
import { AdminSidebarComponent } from './components/layouts/admin-sidebar/admin-sidebar.component';
import { PageNotFoundComponent } from './components/errors/page-not-found/page-not-found.component';
import { StoreModule } from '@ngrx/store';
import { authReducer } from './store/auth/reducer';
import { uiReducer } from './store/ui/reducer';
@NgModule({
declarations: [
AdminHeaderComponent,
AdminSidebarComponent,
PageNotFoundComponent,
],
imports: [
CommonModule,
RouterModule,
FormsModule,
ReactiveFormsModule,
StoreModule.forRoot({ui: uiReducer}),
],
exports: [
AdminHeaderComponent,
AdminSidebarComponent,
FooterComponent
]
})
export class CoreModule { }
app.module.ts:
import { ErrorHandler, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CoreModule } from './core/core.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
CommonModule,
HttpClientModule,
CoreModule,
FormsModule,
ReactiveFormsModule
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PageNotFoundComponent } from 'src/app/core/components/errors/page-not-found/page-not-found.component';
const routes: Routes = [
{
path: '',
redirectTo: 'admin',
pathMatch: 'full'
},
{ path: 'page-not-found', component: PageNotFoundComponent, data: {breadcrumb: 'Page Not Found'} },
{
path: 'admin',
loadChildren: () => import('./features/admin/admin.module').then(m => m.AdminModule)
},
{ path: '**',
redirectTo: 'page-not-found', pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
当我服务Angular应用程序时,我得到了此错误:
错误错误:未接收(在承诺中):typeError:storemodule.forroot()称为两次。功能模块应使用storemodule.forfeature()。 TypeError:storemodule.forroot()称为两次。功能模块应使用storemodule.forfeature()。 在object._provideforrootguard [as useFactory](ngrx-store.mjs:1294:1
如果我发表评论 storemodule.forroot({ui:uireducer})在CoreModule
中我得到这个解决方案吗
?
In Angular-13 project, I am implementing @ngrx/store in the CoreModule for Layout Customization.
This is shown in the diagram below:
Apart from the AppModule, I have these Modules:
- CoreModule
- AdminModule
The Layout is in CoreModule. Then, the @ngrx/store is also implemented in the CoreModule. Also, CoreModule is imported in AdminModule.
state.ts:
import {UiState} from './ui/state';
export interface AppState {
auth: any;
ui: UiState;
}
reducer.ts:
import {
NAVBAR_DARK_VARIANTS,
NAVBAR_LIGHT_VARIANTS,
SIDEBAR_DARK_SKINS,
SIDEBAR_LIGHT_SKINS
} from '../../utils/themes';
import * as Actions from './actions';
import {UiAction} from './actions';
import initialState, {UiState} from './state';
export function uiReducer(state: UiState = initialState, action: UiAction) {
switch (action.type) {
case Actions.TOGGLE_SIDEBAR_MENU:
return {
...state,
menuSidebarCollapsed: !state.menuSidebarCollapsed
};
case Actions.TOGGLE_CONTROL_SIDEBAR:
return {
...state,
controlSidebarCollapsed: !state.controlSidebarCollapsed
};
case Actions.TOGGLE_DARK_MODE:
let variant: string;
let skin: string;
if (state.darkMode) {
variant = NAVBAR_LIGHT_VARIANTS[0].value;
skin = SIDEBAR_LIGHT_SKINS[0].value;
} else {
variant = NAVBAR_DARK_VARIANTS[0].value;
skin = SIDEBAR_DARK_SKINS[0].value;
}
return {
...state,
navbarVariant: variant,
sidebarSkin: skin,
darkMode: !state.darkMode
};
case Actions.SET_NAVBAR_VARIANT:
let navbarVariant: string;
if (state.darkMode) {
navbarVariant = action.payload || NAVBAR_DARK_VARIANTS[0].value;
} else {
navbarVariant =
action.payload || NAVBAR_LIGHT_VARIANTS[0].value;
}
return {
...state,
navbarVariant
};
case Actions.SET_SIDEBAR_SKIN:
let sidebarSkin: string;
if (state.darkMode) {
sidebarSkin = action.payload || SIDEBAR_DARK_SKINS[0].value;
} else {
sidebarSkin = action.payload || SIDEBAR_LIGHT_SKINS[0].value;
}
return {
...state,
sidebarSkin
};
default:
return state;
}
}
core.module:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { AdminHeaderComponent } from './components/layouts/admin-header/admin-header.component';
import { AdminSidebarComponent } from './components/layouts/admin-sidebar/admin-sidebar.component';
import { PageNotFoundComponent } from './components/errors/page-not-found/page-not-found.component';
import { StoreModule } from '@ngrx/store';
import { authReducer } from './store/auth/reducer';
import { uiReducer } from './store/ui/reducer';
@NgModule({
declarations: [
AdminHeaderComponent,
AdminSidebarComponent,
PageNotFoundComponent,
],
imports: [
CommonModule,
RouterModule,
FormsModule,
ReactiveFormsModule,
StoreModule.forRoot({ui: uiReducer}),
],
exports: [
AdminHeaderComponent,
AdminSidebarComponent,
FooterComponent
]
})
export class CoreModule { }
app.module.ts:
import { ErrorHandler, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CoreModule } from './core/core.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
CommonModule,
HttpClientModule,
CoreModule,
FormsModule,
ReactiveFormsModule
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { PageNotFoundComponent } from 'src/app/core/components/errors/page-not-found/page-not-found.component';
const routes: Routes = [
{
path: '',
redirectTo: 'admin',
pathMatch: 'full'
},
{ path: 'page-not-found', component: PageNotFoundComponent, data: {breadcrumb: 'Page Not Found'} },
{
path: 'admin',
loadChildren: () => import('./features/admin/admin.module').then(m => m.AdminModule)
},
{ path: '**',
redirectTo: 'page-not-found', pathMatch: 'full'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
When I serve the Angular Application, I got this error:
ERROR Error: Uncaught (in promise): TypeError: StoreModule.forRoot() called twice. Feature modules should use StoreModule.forFeature() instead.
TypeError: StoreModule.forRoot() called twice. Feature modules should use StoreModule.forFeature() instead.
at Object._provideForRootGuard [as useFactory] (ngrx-store.mjs:1294:1
If I comment out StoreModule.forRoot({ui: uiReducer}) in CoreModule, the error is no more there.
How do I get this resolved?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论