首页 > 解决方案 > 为什么不是没有body标签的已知元素

问题描述

我是这个角度 6 的新手。这可能很愚蠢,但我想明确我的概念。下面是 appComponent typescript 的代码 my

   import { Component, OnInit, Inject } from '@angular/core';
import {ToastrService} from './services/toastr.service';


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

当我<app-root>以以下格式输入 index.html 时。它工作得很好。

 <html>

    <body>
            <app-root></app-root>
    </body>
</html>

但是当我删除 html 和 body 标签时,它只会给我一个下面给出的错误。

main.ts:12 Error: The selector "app-root" did not match any elements
    at DefaultDomRenderer2.push../node_modules/@angular/platform-browser/fesm5/platform-browser.js.DefaultDomRenderer2.selectRootElement (platform-browser.js:1073)
    at BaseAnimationRenderer.push../node_modules/@angular/platform-browser/fesm5/animations.js.BaseAnimationRenderer.selectRootElement (animations.js:229)
    at DebugRenderer2.push../node_modules/@angular/core/fesm5/core.js.DebugRenderer2.selectRootElement (core.js:20837)
    at createElement (core.js:17499)
    at createViewNodes (core.js:19737)
    at createRootView (core.js:19690)
    at callWithDebugContext (core.js:20722)
    at Object.debugCreateRootView [as createRootView] (core.js:20208)
    at ComponentFactory_.push../node_modules/@angular/core/fesm5/core.js.ComponentFactory_.create (core.js:18029)
    at ComponentFactoryBoundToModule.push../node_modules/@angular/core/fesm5/core.js.ComponentFactoryBoundToModule.create 

所以我的问题是..我必须总是<app-root>用html或body标签写吗?或者有没有其他方法可以 <app-root> 在不写html或body标签的情况下写?

谢谢

标签: angularangular5angular6

解决方案


推荐阅读