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

时间:2017-07-20 15:29:05

标签: javascript angular typescript web-component

我一直在尝试在Angular中创建一个简单的应用程序,我能够使它在Plunker中运行。不幸的是,它给了我这个错误

  

无法绑定到'joke',因为它不是'app-root'的已知属性。

我不知道如何处理。

有什么问题?

joke.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() {}
}

笑话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();
  });
});

笑话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

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 { }

2 个答案:

答案 0 :(得分:1)

从您发布的代码中我看到您的AppComponent类为空:

export class AppComponent {}

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

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

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

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

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

答案 1 :(得分:0)

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