AppComponent中的ERROR不能用作输入组件

时间:2018-10-20 23:20:02

标签: angular typescript firebase firebase-realtime-database components

嘿,我是不熟悉Angular和Web开发的新手,我已经遍历了一些教程。

当尝试合并使用Firebase数据库时,在本地编译(使用ng serve)时,我的应用程序失败。返回:“ AppComponent不能用作输入组件”

如果您能提供任何帮助,那就太好了。在此先感谢:)

//app.component.ts
import { Component } from '@angular/core';
import {AuthService} from "./services/auth.service";
import {Router} from "@angular/router";
import { AngularFireDatabase } from 'angularfire2/database';
import { AngularFireList } from 'angularfire2/database';

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

class Event {
  constructor(public title) { }
}


export class AppComponent {
  title = 'qoqa';
  private eventCounter = 0;
  public events:AngularFireList<Event[]>;
  constructor(db: AngularFireDatabase, private authService: AuthService, private router: Router) {
    this.events= db.list('/events');
  }
  public AddEvent(): void {
    let newEvent = new Event(`My event #${this.eventCounter++}`);
    this.events.push([newEvent]);
  }
  signInWithFacebook() {
    this.authService.FacebookSignIn()
      .then((res) => {
        //this.router.navigate(['dashboard'])
      })
      .catch((err) => console.log(err));
  };
  signInWithGoogle() {
    this.authService.GoogleSignIn()
      .then((res) => {
        //this.router.navigate(['dashboard'])
      })
      .catch((err) => console.log(err));
  }
}

//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { environment } from '../environments/environment';
import { LoginComponent } from './login.component';
import { AppRoutingModule } from './app-routing';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { AuthService } from './services/auth.service';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { CreateEventComponent } from './create-event/create-event.component';
import { EventsComponent } from './events/events.component';
import { ProfileComponent } from './profile/profile.component';
import { SavedEventsComponent } from './saved-events/saved-events.component';
import { InvitationsComponent } from './invitations/invitations.component';
import { CreateQoqaComponent } from './create-qoqa/create-qoqa.component';



@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    HomeComponent,
    CreateEventComponent,
    EventsComponent,
    ProfileComponent,
    SavedEventsComponent,
    InvitationsComponent,
    CreateQoqaComponent
  ],
  imports: [
    AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule,
    AngularFireAuthModule,
    AngularFireDatabaseModule,
    AppRoutingModule,
    BrowserModule
  ],
  providers: [AuthService],
  bootstrap: [AppComponent]
})
export class AppModule { }

3 个答案:

答案 0 :(得分:5)

@Component装饰应在AppComponent而非Event上使用。

还要检查AppComponent上是否声明了AppModule

答案 1 :(得分:0)

我只想提供一个由see解释的问题的示例。

@Component({ ... })虽然看起来有点像一个函数,但它是一个装饰器,因此它必须在您希望Angular视为组件的类之前。因此,请确保不要将类放在@Component({ ... })export class YourComponent之间。

因此将Event移到AppComponent之前/之后:

class Event {
    constructor(public title) { }
}

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    ...

答案 2 :(得分:0)

此问题归因于语法失败或小错误。将新组件添加到我的app.component.ts文件时,我遇到了同样的问题。在解决问题时,我来到了StackOverflow,并想分享自己的经验。

在检查所有文件时,我在app.component.ts文件中发现了一些语法错误和小错误。

解决它们并编译后,我能够获得确切的错误消息。

一个问题是没有正确安装文件路径,所以我希望这可能是导致应用程序在编译时失败的原因。

import { AngularFireDatabase } from '**angularfire2/database**';
import { AngularFireList } from '**angularfire2/database**';