类型“ LocationComponent”是2个模块的声明的一部分:角6?

时间:2018-09-11 08:02:38

标签: angular routing angular5 angular6

我对angular完全陌生,因此我无法使用其他堆栈流答案来解决错误。请帮助。

我正在使用角度6。我已经有一种基本格式,所以我试图添加子页面。但是我收到此错误“ Type LocationComponent是2个模块的声明的一部分” ,并在其中停留了大约2天。

以下是“我的src”文件夹格式的图片:My src folder:

我的app.module.ts编码如下:

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CommonModule, LocationStrategy, HashLocationStrategy } from '@angular/common';
import { NgModule, NgModuleFactory } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { Routes, RouterModule } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LocationComponent } from './masterpage/location/location.component';
import { StorageComponent } from './masterpage/storage/storage.component';
@NgModule({
  declarations: [
    AppComponent,
    SpinnerComponent,         
    LocationComponent,
    StorageComponent
  ],
  imports: [
    CommonModule,
    BrowserModule,
    BrowserAnimationsModule,   
    FormsModule,
    HttpClientModule,
    NgbModule.forRoot(),      
    AppRoutingModule,        
  ],
  providers: [
      {
      provide: PERFECT_SCROLLBAR_CONFIG,
      useValue: DEFAULT_PERFECT_SCROLLBAR_CONFIG
    },{
    provide: LocationStrategy,
    useClass: HashLocationStrategy
  }],
  bootstrap: [AppComponent]
})
export class AppModule { }

我的app-routing.module.ts如下:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
export const routes: Routes = [
{
    path: '',
    component: FullComponent,
    children: [
        { path: '', redirectTo: '/masterpage/location', pathMatch: 'full' },
        { path: 'masterpage', loadChildren: './masterpage/masterpage.module#MasterPageModule' }, 
    ]
}];
@NgModule({
    imports: [RouterModule.forRoot(routes), NgbModule.forRoot()],
    exports: [RouterModule]
})
export class AppRoutingModule { }

我的masterpage.module.ts代码如下:

import { NgModule,Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { LocationComponent } from './location/location.component';
import { StorageComponent } from './storage/storage.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MasterPageRoutes } from './masterpage.routing';

@NgModule({
    imports: [
        FormsModule,
        CommonModule,
        NgbModule,
        RouterModule.forChild(MasterPageRoutes)
    ],
    declarations: [
        LocationComponent,
        StorageComponent,
    ]
})
export class MasterPageModule { }

我的masterpage.routing.ts代码如下:

import { Routes } from '@angular/router';
import { LocationComponent } from './location/location.component';
import { StorageComponent } from './storage/storage.component';

export const MasterPageRoutes: Routes = [
  {
    path: '',
    children: [
    {
      path: 'location',
      component: LocationComponent,
      data: {
        title: 'Location',
        urls: [{title: 'Location',url: '/location'},{title: 'Location'}]
      }
    }, {
      path: 'storage',
      component: StorageComponent,
      data: {
        title: 'Storage',
        urls: [{title: 'Storage',url: '/storage'},{title: 'Storage'}]
      }
    }
]
  }
];

“我的示例”以前使用url '/ localhost:4200 /#/ dashboard / dashboard1'运行-这是示例。我按照自己的要求将仪表板更改为“母版页”,并将其子级“ dashboard1”和“ dashboard2”分别更改为“ location”和“ storage”。我希望子页面的位置和存储空间必须加载'/ localhost:4200 /#/ masterpage / location'  但是它对我不起作用。代码页中没有错误。我在控制台中仅在检查元素下发现错误。

请帮助我解决问题。 预先感谢。

1 个答案:

答案 0 :(得分:0)

由于您已经在app.module.ts中声明了 LocationComponent ,因此它已在整个应用范围内可用。因此,无需在 masterpage.module.ts 中再次声明它。

从声明数组中删除 masterpage.module.ts 中的 LocationComponent ,并删除导入。