【问题标题】:Error: Unexpected value 'SocialmediaFeedComponent' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation错误:模块“AppModule”声明的意外值“SocialmediaFeedComponent”。请添加@Pipe/@Directive/@Component 注解
【发布时间】:2019-07-02 19:00:51
【问题描述】:

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

错误:模块“AppModule”声明的意外值“SocialmediaFeedComponent”。请添加@Pipe/@Directive/@Component 注解。

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

      // ...

      // ...
}

【问题讨论】:

  • 可以发SocialmediaFeedComponent的代码吗?
  • 当然,有点长

标签: angular syntax-error angular-module


【解决方案1】:

正如我所见,您在类声明上方缺少 @Component 装饰器。 尝试在您的班级正上方添加 @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中找到更多详细信息

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    相关资源
    最近更新 更多