Error: Unexpected value 'SocialmediaFeedComponent' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation

Error: Unexpected value 'SocialmediaFeedComponent' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation

我的 angular 项目出现错误。我检查了网络,据我所知,我正确地声明了每个组件和模块。

Error: Unexpected value 'SocialmediaFeedComponent' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation.

app.component.ts:

import { FormsModule } from '@angular/forms';

...;
import { SocialmediaComponent } from './socialmedia/socialmedia.component';
import { SocialmediaFeedComponent } from './socialmedia-feed/socialmedia-feed.component';
...;


import { CountdownModule } from 'ngx-countdown';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';




@NgModule({
  declarations: [
    ...,
    SocialmediaComponent,
    SocialmediaFeedComponent,
    ...,
  ],
  imports: [
    BrowserModule,
    CountdownModule,
    FormsModule,
    NgbModule,
  ],
  providers: [
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

社交媒体-feed.component.ts:

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

import * as Collections from 'typescript-collections';

@Component({
  selector: 'app-socialmedia-feed',
  templateUrl: './socialmedia-feed.component.html',
  styleUrls: ['./socialmedia-feed.component.scss']
})

class PostSocialmediaFeedComponent {

  constructor(url: string, date: Date, type: string) { }

  date: Date;
  url: string;
  type: string;

  getDate() {
    return this.date;
  }

  getURL() {
    return this.url;
  }

  getType() {
    return this.type;
  }
}

export class SocialmediaFeedComponent implements OnInit {



  constructor() {
    this.getLatestPosts();
   }

  ngOnInit() {
  }
      // METHODS TO SORT, MERGE, GET POSTS FROM SOCIALMEDIA

      // ...

      // ...
}

据我所知,您在 class 声明上方缺少 @Component 装饰器。 尝试在 class.

正上方添加 @Component 关键字

类似于:

@Component({
  selector: 'app-socialmedia-feed',
  templateUrl: './socialmedia-feed.component.html',
  styleUrls: ['./socialmedia-feed.component.scss']
})
export class SocialmediaFeedComponent implements OnInit {



  constructor() {
    this.getLatestPosts();
   }

  ngOnInit() {
  }
      // METHODS TO SORT, MERGE, GET POSTS FROM SOCIALMEDIA

      // ...

      // ...
}

您可以在官方文档中找到更多详细信息here