angular - 无法导入导出的接口(interface) - 找不到导出

标签 angular typescript ecmascript-6

我将问题简化为这个例子:

test.model.ts

export class A {
    a: number;
}

export interface B {
    b: number;
}

test.component.ts

import { Component, Input } from '@angular/core';
import { A, B } from './test.model';

@Component({
    selector: 'test',
    template: '<h1>test</h1>'
})
export class TestComponent {

    //This works fine
    @Input() foo: A;

    //Does NOT work - export 'B' was not found in './test.model'
    @Input() bar: B;

}

当我想使用接口(interface)时,我收到以下警告:

WARNING in ./src/app/.../test.component.ts
21:76 export 'B' was not found in './test.model'

但是,使用 是可以的。有什么提示吗?

更新:似乎与这个问题有某种关系:https://github.com/webpack/webpack/issues/2977

最佳答案

根本原因在于 Typescript 和转译。一旦 TypeScript 代码被转译,接口(interface)/类型就消失了。它们不再存在于发出的文件中。

虽然类型被删除,但导入/导出不一定。原因是存在歧义,编译器并不总是确定导出的内容是类型还是值。

当使用 TypeScript 3.8 及更高版本时,您需要在 test.component.ts 中做的就是:

import { A } from './test.model';
import type { B } from './test.model';

关于angular - 无法导入导出的接口(interface) - 找不到导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40841641/

相关文章:

javascript - 通过 Angular(8.x) 中的类方法更新输入标签的值后,输入标签中的焦点消失

javascript - 无法从路径导入 IonicModule

javascript - 带条件的箭头函数对象迭代

javascript - 如何在MainApplication.java中获取环境变量 react native

javascript - 如何使用es6 import代替require?

angular2 错误 : this. $modal.modal 不是函数

angular - 设置 angular cli production build tfs

angular - Angular 2 中的组件还需要导入共享服务吗?

angular - ng-bootstrap 预先输入选定的对象

Angular 5 - 路由更改 url 但不导航