Angular2 - 获取错误:`EXCEPTION:在MyComponent上找不到指令注释

时间:2016-03-14 07:16:01

标签: angular

当我尝试从其他文件导入指令时,我收到错误为EXCEPTION: No Directive annotation found on MyComponent - 如何解决此问题?

这是我的代码:

app.component.ts:

import {Component} from 'angular2/core';
import {MyComponent} from "./app.my-component"

@Component({
    selector: 'app',
    template: `
        <h1>Hellow World</h1>
        <span>Here is your component:</span>
        <my-component></my-component>
    `,
    directives: [MyComponent]
})

export class AppComponent { }

app.mycomponent.ts:

import {Component} from 'angular2/core';

@Component({

    selector : 'my-component',

    template : `<h2>This is from My component</h2>`

});

export class MyComponent { };

但我收到的错误是:No Directive annotation found on MyComponent如何解决这个问题?

4 个答案:

答案 0 :(得分:30)

您应该在组件关闭;指令括号后删除Component

import {Component} from 'angular2/core';

@Component({

selector : 'my-component',

template : `<h2>This is from My component</h2>`

})
export class MyComponent { };

答案 1 :(得分:0)

也许你必须做一个相对路径。它取决于您要导入的组件相对于要导入组件的脚本的位置。类似

import {MyComponent} from "../app.my-component"

import {MyComponent} from "./app.my-component"

如果您发布了文件夹结构,也会有所帮助。

答案 2 :(得分:0)

哦,我还在学习&#39;在这方面,但我得到了与你相同的信息。 我想我解决了......

在这里找到了答案:https://angular.io/docs/ts/latest/guide/attribute-directives.html

将此添加到您的app.mycomponent.ts:

QGraphicsTextItem

并添加指令:

import {Directive} from 'angular2/core';

希望这对你有所帮助。

再次,不确定这是否正确......

答案 3 :(得分:0)

就我而言,导出特定类时出现问题,并在其他组件中导入该类。

但是这个错误没有明确显示,而是在控制台的EXCEPTION: No Directive annotation...中表现为异常。

为了明确地获得此错误:

  1. 停止本地服务器
  2. 重新运行npm start(假设Angular快速入门设置)
  3. 之前隐藏的任何当前错误都会立即显示。