【问题标题】:Angular2 class name collisionAngular2类名冲突
【发布时间】:2016-07-20 09:34:36
【问题描述】:

我正在使用带有 Typescript 的 Angular2。

我创建了一个名为Input 的类,并尝试在其中一个使用Input 注释的组件中使用它。现在,当我尝试创建Input 类的实例时,我得到了一个不正确的InputMetadata 实例,这显然是由于与从'@angular/core' 导入的Input 冲突:

import { Component, Input, OnInit }  from '@angular/core';

import { Input } from '../controls/input';

@Component({
    moduleId: module.id,
    selector: 'dynamic-form',
    templateUrl: 'dynamic-form.component.html',
    directives: [DynamicFormInputComponent, REACTIVE_FORM_DIRECTIVES],
})
export class DynamicFormComponent implements OnInit {
    ngOnInit() {
         let control = new Input({});
         console.log(control);

    }
}

有没有办法在本地给一个类起别名,例如这样我的 Input 将作为 FormInput 导入。如果不是,那么在不重命名我的 Input 类的情况下,解决此问题的一种合理方法是什么。

【问题讨论】:

  • import { ... as .... }............
  • 我真的会避免使用“保留名称”作为您自己的类的名称。但是@peeskillet 的评论应该对你有所帮助

标签: angular


【解决方案1】:

您可以为此使用aliasing syntax of the import statement

import { Input as InputControl } from '../controls/input';

// …
let control = new InputControl({});

您可能希望让您的 Input 类更具描述性,因为不清楚它是组件、指令、(表单)控件还是其他东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多