gpt4 book ai didi

javascript - Uncaught Error : Template parse errors: Angular 4

转载 作者:太空狗 更新时间:2023-10-29 19:28:39 26 4
gpt4 key购买 nike

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

Can't bind to 'joke' since it isn't a known property of '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() {}
}

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

笑话列表组件

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

最佳答案

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

导出类 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)
}
}

关于javascript - Uncaught Error : Template parse errors: Angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45219140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com