0

你好,我是 Angular 的新手,在这个框架中学习了一门课程,我遇到了一个错误问题,我不明白如何解决它。从'@angular/core'导入{组件};

code: @Component{{ selector: 'contador-app', }} //se le pone export para poder utilzar esta clase fuera de este archivo :3

export class contadorComponent{

}

错误消息:( 别名) const Component: ComponentDecorator import Component 为 Angular 组件提供配置元数据。组件装饰器和元数据。

@publicApi

@注解

@publicApi

声明预期.ts(1146)

4

1 回答 1

0

看起来您不小心为组件装饰器使用了两次大括号。

@Component >{{<
    selector: 'contador-app', >}}<

组件装饰器如下所示:

@Component({
    selector: 'contador-app',
})

使用 templateUrl: 'path to html-file' 和 styleUrls: ['path to stylesheet'] 来指定组件 html 文件和样式表也很常见,请注意后者在括号之间,因为您可以指定一个或多个路径样式表。还可以选择使用直接在组件装饰器中编写 html / css

template: 'html-code' 

和/或

styles: 'css-code'. 

此外,Purvang 在评论中提出了一个很好的问题,即您是如何创建组件的?如果您手动执行此操作,则必须确保将其正确导入到负责的模块中,因为在引导应用程序时需要通过 Angular 使组件知道,否则它不会呈现。如果您是新手,最好使用 ng generate 命令让 CLI 为您生成一个组件,如下所示:

ng generate component your-component-name

有关装饰器的更多信息,我参考了这篇博文: https ://www.netjstech.com/2020/03/angular-component-decorator.html

于 2021-05-25T06:47:28.937 回答