Javascript 无法在角度视图中使用子组件的选择器

Javascript 无法在角度视图中使用子组件的选择器,javascript,angular,angular-components,Javascript,Angular,Angular Components,我想将task.component的视图插入到我的主app.component.html根模板中,如下所示: app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { TaskComponent } from '

我想将task.component的视图插入到我的主app.component.html根模板中,如下所示:

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

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

@NgModule({
  declarations: [
    AppComponent,
    TaskComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'planner';
}
import { Component, OnInit } from '@angular/core';

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

export class TaskComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    console.log("Task comonent initialized!");
  }

}
task.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'planner';
}
import { Component, OnInit } from '@angular/core';

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

export class TaskComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    console.log("Task comonent initialized!");
  }

}
app.component.html


要将新组件添加到您正在处理的模块(在本例中为AppModule),您首先要创建一个组件,只需使用AngularCLI通过运行命令ng g c任务来执行此操作,AngularCLI会自动将其添加到AppModule的声明数组中。然后,您只需将选择器标记添加到app.component.html即可加载它


如果您遗漏了什么,只需为您的ref添加,以进行交叉检查。

您在app.component.html中添加的选择器位置到底显示了什么??控制台中出现了什么错误。此选择器应该可以工作并呈现到TaskComponent html视图

尝试在停止ng serve后重新运行它。希望您使用Angular Cli命令添加组件-
ng生成组件任务

我不知道链接组件是什么意思

按照您共享的结构,如果您想查看某些内容,则必须创建task-component.html,因为它不存在

组件的思想是,每个组件都必须有其逻辑独立性才能工作

共享资源的方法是使用EventEmitter或绑定属性

这是我看到的场景,我可以看到创建的组件


事实上,使用ng serve重新启动服务器并没有解决问题,因此不对组件模板的缺失插值负责

重建应用程序是我唯一最丑陋的选择,当然,这会导致额外的工作

删除项目中的所有文件以及放置节点_模块所在图层中的所有文件 正在使用ng new设置项目。。。 使用终端命令ng g c…/正在生成组件。。。
很抱歉,我没有在问题中包含这一行,它也不起作用。您的TaskComponent html中有任何html标记吗?请尝试中断本地服务。然后使用ng serveDid再次运行它您是否尝试中断已服务的应用程序并使用ng serve再次运行它?请尝试清除缓存。是的,我确实看到了@see answer。Angular会输出一个空白页和错误,可以在编辑的问题中看到。是的,我看到了。我还使用了-model app.model.tswell,您需要导入组件。如果你没有将它导入到你想要使用的组件中,你希望如何使用它?但是我在根模块中添加了该组件?我做错了什么?