javascript - Angular 2. 无法将一个组件插入另一组件。模板解析错误 :. ..不是已知元素

标签 javascript angular

我尝试重复教程中的操作,将一个组件添加到另一个组件中,但失败了。

项目结构:

enter image description here

app.component.ts:

import {Component} from "@angular/core"
import {TodoListComponent} from "./todo-list/todo-list.component"

....
@Component({
    moduleId: module.id,
    selector: "app",
    templateUrl: "app.component.html",
    styleUrls: ['app.component.css'],
    directives:[ TodoListComponent ]
})
export class AppComponent {
   ...
}

app.component.html:

...
<todo-list ></todo-list> <!--[todos]="todos"-->
...

todo-list.component.ts

import {Component, Input} from "@angular/core"
//import {Input} from "../../node_modules/@angular/core/src/metadata/directives";

@Component({
    selector: "todo-list",
    templateUrl: "./app/todo/todo-list.component.html"
})
export class TodoListComponent {
    //@Input() todos:Todo[];
}

todo-list.component.html

ololololo

Rmain.ts

import {platformBrowserDynamic} from "@angular/platform-browser-dynamic";

import {AppModule} from "./app.module";

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

app.module.ts

import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from "@angular/core";
import {AppComponent} from "./app.component"
import {FormsModule} from "@angular/forms";

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

}

我打开浏览器并看到以下内容:

zone.js:388 Unhandled Promise rejection: Template parse errors:
'todo-list' is not a known element:
1. If 'todo-list' is an Angular component, then verify that it is part of this module.
2. If 'todo-list' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("lass="todo-item" *ngFor="let todo of todos" [ngClass]="{'completed': todo.completed }">-->
        [ERROR ->]<todo-list ></todo-list> <!--[todos]="todos"-->
    </section>
</main>
"): AppComponent@14:8 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors:
'todo-list' is not a known element:
1. If 'todo-list' is an Angular component, then verify that it is part of this module.
2. If 'todo-list' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("lass="todo-item" *ngFor="let todo of todos" [ngClass]="{'completed': todo.completed }">-->
        [ERROR ->]<todo-list ></todo-list> <!--[todos]="todos"-->
    </section>
</main>
"): AppComponent@14:8
    at TemplateParser.parse (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:7728:21)
    at RuntimeCompiler._compileTemplate (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:17503:53)
    at eval (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:17423:64)
    at Set.forEach (native)
    at RuntimeCompiler._compileComponents (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:17423:21)
    at createResult (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:17319:21)
    at ZoneDelegate.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:232:26)
    at Zone.run (http://localhost:3000/node_modules/zone.js/dist/zone.js:114:43)
    at http://localhost:3000/node_modules/zone.js/dist/zone.js:502:57
    at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:265:35)consoleError @ zone.js:388_loop_1 @ zone.js:417drainMicroTaskQueue @ zone.js:421ZoneTask.invoke @ zone.js:339
zone.js:390 Error: Uncaught (in promise): Error: Template parse errors:(…)consoleError @ zone.js:390_loop_1 @ zone.js:417drainMicroTaskQueue @ zone.js:421ZoneTask.invoke @ zone.js:339

请指出错误。我重新检查了几次教程,没有发现错误。

最佳答案

AppComponent 声明中删除 directives:[ TodoListComponent ] 并将该组件的声明添加到模块中,如下所示:

declarations: [AppComponent, TodoListComponent],

将 todo-list.component.ts 的 URL 更改为

@Component({
    selector: "todo-list",
    templateUrl: "./app/todo-list/todo-list.component.html"
})

关于javascript - Angular 2. 无法将一个组件插入另一组件。模板解析错误 :. ..不是已知元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41117599/

相关文章:

javascript - 模拟浏览器和服务器不同时区进行本地测试

javascript - 错误 : EPERM: operation not permitted while running puppeteer JavaScript node

javascript - 忽略输入但需要触发保存按钮的文本区域

javascript - 使用 *ngFor 和 ContentChildren 时出现 ExpressionChangedAfterItHasBeenCheckedError

javascript - 管道内的映射被忽略并且不会触发http调用

javascript - 在 Vue 项目中使用 jQuery 是否更可取?

javascript - Angular 5 httpClient 版本的代码

javascript - 如果 div B 为空则隐藏 div A

android - 我正在尝试发布到本地主机 API,我得到 --> java.net.ConnectException : Connection timed out

angular - 如何在 nginx 或 apache httpd 中构建 angular 4 应用程序?