Angular 2隐形组件容器

Mic*_*ick 5 angular2-template angular

像容器一样<app-root></app-root>可以破坏css布局,尤其是在使用flexbox时.

是否可以使用隐形组件容器?我selector: '[app-root]'已尝试使用以下标记,但它们都创建了一个dom元素:

<template>, <ng-template>, <ng-template>, <ng-container>
Run Code Online (Sandbox Code Playgroud)

如果确实需要这个容器,那么如果Angular只呈现HTML注释会很棒.

lom*_*boo 4

selector我个人认为最好的方法是作为属性使用。例如,您有这样的@Component:

...
@Component({
    selector: '[your-component]'
})
...
Run Code Online (Sandbox Code Playgroud)

这样,your-component就是一个属性,您可以像这样使用它:

<div your-component>Some content...</div>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您可以保证样式不会破坏。

PS据我所知,这个功能(replace在 Angular 1 中)在 Angular 1 中已被弃用,因此您不会期望从 Angular 2 中获得该功能。