Angular 7:获取“ AppComponent不能用作输入组件”错误

时间:2019-05-17 19:36:28

标签: angular components entry-point

(关于这个问题-ERROR in AppComponent cannot be used as an entry component,我包括下面在AppModule上声明的AppComponent ...)

我正在使用Angular7。我有这个app.module.ts文件

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后我有了这个app.component.ts文件...

import { Component,trigger,animate,style,transition,keyframes } from '@angular/animations';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  animations:[
   trigger("moveInLeft",[
      transition("void=> *",[style({transform:"translateX(300px)"}),
        animate(200,keyframes([
         style({transform:"translateX(300px)"}),
         style({transform:"translateX(0)"})

          ]))]),


          transition("*=>void",[style({transform:"translateX(0px)"}),
        animate(100,keyframes([
         style({transform:"translateX(0px)"}),
         style({transform:"translateX(300px)"})

          ]))])    

    ])

  ]
})
export class AppComponent {
  todoArray=[];
  todo;
  //todoForm: new FormGroup()


   addTodo(value){
    if(value!==""){
     this.todoArray.push(value)
    //console.log(this.todos) 
  }else{
    alert('Field required **')
  }

  }

  /*delete item*/
  deleteItem(todo){
    for(let i=0 ;i<= this.todoArray.length ;i++){
        if(todo== this.todoArray[i]){
            this.todoArray.splice(i,1)
        }
    }
  }

  // submit Form
  todoSubmit(value:any){
     if(value!==""){
    this.todoArray.push(value.todo)
     //this.todoForm.reset()
    }else{
      alert('Field required **')
    }

  }

}

但是,当我启动本地服务器时,出现错误“ AppComponent不能用作输入组件”:

localhost:Todo-app-in-Angular davea$ ng serve
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **

Date: 2019-05-17T19:32:57.591Z
Hash: 72544b311fe9298f38ef
Time: 3030ms
chunk {main} main.js, main.js.map (main) 1.95 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 92.1 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.08 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 16.3 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 327 kB [initial] [rendered]

ERROR in AppComponent cannot be used as an entry component.
ℹ 「wdm」: Failed to compile.

我以为Angular可以根据@NgModule中的声明找出入口点?

编辑:以下是我的app.component.html文件..

<div class="container">
        <form #todoForm= "ngForm"(submit)="todoSubmit(todoForm.value); todoForm.resetForm()" >
                <div class="form-group">
                <h1 class="text-center ">Todo App</h1>
                        <div class="input-group-prepend">
                            <input type="text" #todo  class="form-control" placeholder="Add Todo" name="todo" ngModel>
                                <span class="input-group-text" (click)="addTodo(todo.value)">
                                <i class="material-icons">add</i></span>
                        </div>
                </div>
                <div class="data">
                <ul class="list-unstyled">
                        <li [@moveInLeft]  *ngFor="let todo of todoArray">{{todo}} <i (click)="deleteItem(todo)" class="material-icons">delete</i></li>
                </ul>
        </div>
        </form>

</div>

和我的index.html文件...

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>TodoApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <!-- material icons -->
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

0 个答案:

没有答案