Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Ho采用带角度和x27的flexbox布局;s孤立css_Angular_Flexbox - Fatal编程技术网

Angular Ho采用带角度和x27的flexbox布局;s孤立css

Angular Ho采用带角度和x27的flexbox布局;s孤立css,angular,flexbox,Angular,Flexbox,给定以下示例html: <!doctype html> <html lang="en"> <head> <meta charset=" utf-8 "> <title>AngularFlex</title> <meta name="viewport " content="width=device-width, initial-scale=1 "> </head> <b

给定以下示例html:

<!doctype html>
<html lang="en">

<head>
    <meta charset=" utf-8 ">
    <title>AngularFlex</title>
    <meta name="viewport " content="width=device-width, initial-scale=1 ">
</head>

<body style="display: flex;">
    <div style="background: red; height: 100px; flex:1 "></div>
    <div style="background: green; height: 100px; flex:2 "></div>
</body>
</html>

角弯
结果:

现在我想用角度和多个组件来实现这种布局。以下html由angular生成:

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>AngularFlex</title>
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body style="display: flex;">
    <app-root>
        <app-component1>
            <div style="background: red; height: 100px; width: 100px; flex:1"></div>
        </app-component1>
        <app-component2>
            <div style="background: green; height: 100px; width: 100px; flex:2"></div>
        </app-component2>
    </app-root>
</body>

</html>

角弯

如您所见,由于angular为每个组件添加了一个额外的html节点,并且隔离了组件的CSS,因此布局被破坏

我知道有不同的方法来解决这个问题:

  • 使用全局样式并将CSS添加到生成的html节点
  • 在任何子组件中使用:主机选择器来设置生成节点的样式

  • 然而,我想知道,对于大型项目来说,哪种做法才是最好的?如何在无痛苦的英语中从头开始使用flexbox?

    常见做法

    为了能够无缝地使用flex布局和组件,您可以使用
    :host
    -选择器。
    :host
    允许您将样式直接应用于组件元素

    /* app.component1.scss */
    :host {
      /* <app-component1> becomes a flex container */
      display: flex;
    }
    
    /*app.component1.scss*/
    :主持人{
    /*成为flex容器*/
    显示器:flex;
    }
    
    希望这对你有所帮助

    全球风格

    如果您想在不同的子组件中重用样式,可以考虑使用mixin。 只需全局提供一个mixin并将其导入组件的样式文件。 这使您的样式可重用,而且您不会失去
    视图封装的好处。仿真的

    有很多方法可以帮助您。它使弯曲更容易在角度