typescript 合并声明'DepartmentListComponent'中的单个声明必须全部导出或全部本地,ts(2395)路由组件

kkih6yb8  于 2022-12-19  发布在  TypeScript
关注(0)|答案(9)|浏览(495)

我对这段代码有一个问题,我完全按照视频教程(我正在学习)的方法来做,但是它不起作用,需要一些声明。有可能指南中的代码是用一些旧的方法编写的?它是没有服务的硬代码。
app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DepartmentListComponent } from './department-list/department-list.component';
import { EmployeeListComponent } from './employee-list/employee-list.component';

const routes: Routes = [
  { path: 'departments', component: DepartmentListComponent },
  { path: 'employees', component: EmployeeListComponent }

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
export const routingComponents  [DepartmentListComponent, EmployeeListComponent ]

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule, routingComponents } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent,
    routingComponents
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html

<h1> Welocme in routing</h1>

<router-outlet></router-outlet>

我需要看到运行应用程序与改变网址形式http://localhost:4200/employeeshttp://localhost:4200/departments。我是乞丐,所以请理解。

umuewwlo

umuewwlo1#

您正在导出此文件中的DepartmentListComponent,该文件在本地导入到相同的文件模块中。

dxxyhpgq

dxxyhpgq2#

当我们在同一个文件中使用importexport相同的模块时,会抛出此错误。
checkout 所有导入,并确保这些不是从同一个文件导出。

ctehm74n

ctehm74n3#

同样的问题在这里甚至奇怪的决议:删除导出行(最后一行)。编译(通常没有错误)再次添加最后一行。编译...完成。不要问我为什么。我也是一个初学 typescript 和Angular 的人; -)

w1jd8yoj

w1jd8yoj4#

我在使用Webstorm时遇到了同样的问题,重启+缓存清理为我解决了这个问题。

9wbgstp7

9wbgstp75#

我有这个错误消息时,不小心导出相同的接口两次从同一个文件。

juud5qan

juud5qan6#

我在一个新创建和导出的接口上遇到了同样的错误。在接口名称后添加"Interface"后缀可以解决这个问题。
变更:

export interface SomeThing {}

收件人:

export interface SomeThingInterface {}
nwlqm0z1

nwlqm0z17#

您不能将import重新导出为const

export const routingComponents  [DepartmentListComponent, EmployeeListComponent ]

应该是

export const routingComponents = [DepartmentListComponent, EmployeeListComponent]

export { DepartmentListComponent, EmployeeListComponent }
eaf3rand

eaf3rand8#

在intellij IDE上,我发现删除导出并再次添加它(在示例之前,而不是类之前)是有效的。
我有

// x.ts
export class X {}
// y.ts
import {X} from "./x.ts"
export const instance = new X();  // the export to be removed in my case
toe95027

toe950279#

当我试图导出与const变量同名的模块时,我遇到了这个错误。
例如:

export const DepartmentListComponent = [DepartmentListComponent]

当我给出不同的名字时,它开始工作正常,希望它能帮助到别人。

相关问题