【问题标题】:Angular 2 Exception: TypeError: Cannot read property 'annotations' of undefinedAngular 2 异常:TypeError:无法读取未定义的属性“注释”
【发布时间】:2016-04-29 15:39:46
【问题描述】:

我做错了什么?

app.component.ts

import {Component} from 'angular2/core';
@Component({
  selector: 'my-app',
  template: `{{title}}`
})
export class App {
  title = "Angular 2 beta";
  constructor() { console.clear(); }
}

main.ts

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from './app.component';

bootstrap(AppComponent);

错误是

EXCEPTION: TypeError: Cannot read property 'annotations' of undefined

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    类名与您正在引导的名称不匹配。即,而不是

    export class App { ... }
    

    使用

    export class AppComponent { ... }
    

    Angular 正在尝试查找名为 AppComponent 的类的 annotations 属性,但该类不存在,因此出现错误。

    【讨论】:

    • 这个错误非常棘手。应该简单地说'App' is not found每当我在plunker中做一个例子时,我都会遇到这个错误,我花了相当长的时间来找出问题所在。
    • 遗憾的是,当您使用 Angular 时,您会得到所要付出的代价。 Google 似乎对提供有用的错误消息不感兴趣。
    • 我不认为 Angular 开发人员不想提供有用的错误消息,我认为他们只是高估了开发人员对 Angular 2 的平均理解以及我们将多快找出细节。他们只知道他们的代码并假设我们也知道。
    • 这可能有点棘手,因为它也可能是一个旧的导入。我遇到了由导入不存在组件的组件引起的类似问题。我不知道为什么我在浏览器中测试时没有收到任何错误,但在部署到本机时却出现了错误。
    猜你喜欢
    • 2016-10-19
    • 2015-11-04
    • 2022-01-21
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多