错误声明期望

3

我正在使用typescript版本1.8,并且遇到了这个错误。我对typescript很陌生,请帮助我。
npm版本:3.9

文件:ponymain-app.component.ts

 import {Component} from '@angular/core';
    @Component({
        selector: 'ponyracer-app',
        template: `<h1>Hye bud!</h1>`
    })

文件:Index.html

<body>
  <ponyracer-app>
    loading....
  </ponyracer-app>
</body>

2
这是您完整的ponymain-app.component.ts文件吗? - Maximilian Riegler
1
关于错误信息还有更多的细节吗? - Günter Zöchbauer
是的,完整的文件。我刚开始一个Angular项目,并在命令行中得到了这个消息。在执行tsc --watch之后。 - Bhawna
3个回答

4

您需要直接将@Component装饰器附加到类上,直到引导根组件之前,您将不会得到任何东西。

我假设您的主根组件是PonyComponent。

这是您代码的编辑版本。

import { bootstrap } from '@angular/platform-browser-dynamic';

import {Component} from '@angular/core'; 

@Component({ 
selector: 'ponyracer-app', 
template: `<h1>Hye bud!</h1>` 
}) 

class PonyComponent { }

bootstrap( PonyComponent );

以下是翻译的结果:

这里有一个不错的资源可以开始学习: https://angular.io/docs/js/latest/quickstart.html


1
有时停止和重新启动ng serve也有帮助。这对我行得通。

0
装饰器(例如@Component)需要附加到某些东西上,不能单独使用。在@Component的情况下,您需要在其直接下方添加一个类:
import {Component} from '@angular/core';

@Component({
    selector: 'ponyracer-app',
    template: `<h1>Hye bud!</h1>`
})
class AppComponent { }

我真的鼓励你阅读Angular教程 - 它会解释所有这些!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接