typescript 未捕获的错误:模板解析错误:Angular 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45219140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:44:35  来源:igfitidea点击:

Uncaught Error: Template parse errors: Angular 4

javascriptangulartypescriptweb-component

提问by UnKn0wn27

I have been trying to make a simple app in Angular, I was able to make it work in Plunker. Unfortunately, it gives me this error

我一直在尝试用 Angular 制作一个简单的应用程序,我能够让它在Plunker 中工作。不幸的是,它给了我这个错误

Can't bind to 'joke' since it isn't a known property of 'app-root'.

无法绑定到“笑话”,因为它不是“app-root”的已知属性。

that I don't know how to handle.

我不知道如何处理。

What is the problem?

问题是什么?

joke.component.ts

笑话.component.ts

import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
import { Joke } from '../jokes'

@Component({
  selector: 'app-joke',
  templateUrl: './joke.component.html',
  styleUrls: ['./joke.component.css']
})
export class JokeComponent implements OnInit {

  constructor() {}

  @Input("joke") joke: Joke;
  @Output() jokeDeleted = new EventEmitter<Joke>();

  deleteItem() {
    this.jokeDeleted.emit(this.joke)
  }

  ngOnInit() {}
}

joke-form.component.spec

笑话form.component.spec

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { JokeFormComponent } from './joke-form.component';

describe('JokeFormComponent', () => {
  let component: JokeFormComponent;
  let fixture: ComponentFixture<JokeFormComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ JokeFormComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(JokeFormComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});

joke-list.component

笑话列表组件

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

@Component({
  selector: 'app-joke-list',
  templateUrl: './joke-list.component.html',
  styleUrls: ['./joke-list.component.css']
})
export class JokeListComponent implements OnInit{
  jokes: Joke[];

  constructor() {
    this.jokes = [
      new Joke("I am telling a joke.", "Haha, that's funny!"),
      new Joke("I am telling an even funnier joke.", "Hahahahaha!!"),
      new Joke("I am telling the funniest joke.", "HAHAHAHAHAHA!!!!")
    ]
  }
  addJoke(joke) {
    this.jokes.unshift(joke);
  }
  deleteJoke(joke) {
    let indexToDelete = this.jokes.indexOf(joke)
    if (indexToDelete !== -1) {
      this.jokes.splice(indexToDelete, 1);
    }
  }

  ngOnInit() {}
}

app.component

应用组件

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

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

app.module.ts

app.module.ts

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

import { AppComponent } from './app.component';
import { JokeFormComponent } from './joke-form/joke-form.component';
import { JokeListComponent } from './joke-list/joke-list.component';
import { JokeComponent } from './joke/joke.component';

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

采纳答案by Nehal

From the code you have posted I see that your AppComponentclass is empty :

从您发布的代码中,我看到您的AppComponent班级为空:

export class AppComponent {}

export class AppComponent {}

Since you haven't posted your html code, I am guessing you are doing something similar to the plunker, where my-appin plunker is equivalent to app-rootin your question's code:

由于您还没有发布您的 html 代码,我猜您正在做类似于 plunker 的事情,其中my-appplunker 相当于app-root您问题的代码:

<app-root *ngFor="let j of jokes" [joke]="j" (jokeDeleted)="deleteJoke($event)"></app-root>

Once you add @Input("joke") joke: Joketo AppComponentclass, it should not throw that error anymore:

添加@Input("joke") joke: JokeAppComponent类后,它不应再抛出该错误:

export class AppComponent {
  @Input("joke") joke: Joke;
  @Output() jokeDeleted = new EventEmitter<Joke>();

  deleteItem() {
    this.jokeDeleted.emit(this.joke)
  }
}

回答by Ganni Georgiev

You can try to delete this OnInit method that angular generates for us in this child joke.component.ts class that implements this @Input method for Property binding [property]. And also restart the server.

您可以尝试在这个子 joke.component.ts 类中删除 angular 为我们生成的这个 OnInit 方法,该类为属性绑定 [property] 实现了这个 @Input 方法。并重新启动服务器。