简体   繁体   English

Angular 组件默认样式 css 显示块

[英]Angular component default style css display block

I am sick of all my angular elements being 0x0 pixels, because they have names like app-card, app-accordion, which the browser does not recognise as HTML5 compliant elements and as thus, will not give any default styles to.我厌倦了所有 0x0 像素的 angular 元素,因为它们具有类似 app-card、app-accordion 之类的名称,浏览器无法将其识别为 HTML5 兼容元素,因此不会为其提供任何默认样式。

This is means that inspecting it in Chrome, I fail to see the container dimensions and when the DOM is really deep, it is hard to understand which element encompasses which area on the screen, etc.这意味着在 Chrome 中检查它时,我看不到容器尺寸,并且当 DOM 非常深时,很难理解哪个元素包含屏幕上的哪个区域等。

It feels logical to me that all angular elements should be block displayed by default, because for the majority, it makes sense.对我来说,默认情况下所有角度元素都应该被阻止显示是合乎逻辑的,因为对于大多数人来说,这是有道理的。

As an example, consider these elements例如,考虑这些元素

bbs-accordion-header //(width 0px, height 0px)

contains包含

bbs-accordion-header-regular //(width 1920px, height 153px)

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

So bbs-accordion-header does not have any dimensions, even though it's children do have them.所以 bbs-accordion-header 没有任何尺寸,即使它的孩子们有。

I solve this, by manually adding one line to each elements .scss file我通过手动向每个元素 .scss 文件添加一行来解决这个问题

:host { display: block; }

But it is very tedious to add this manually every time.但是每次都手动添加这个很繁琐。 Does anyone know of a better solution?有谁知道更好的解决方案?

My pull-request has been merged .我的 pull-request 已合并

With the upcoming release of Angular CLI v9.1.0 a new option is going to be available:随着即将发布的 Angular CLI v9.1.0 ,将提供一个新选项:

--displayBlock=true|false

Docs: https://next.angular.io/cli/generate#component文档: https : //next.angular.io/cli/generate#component

For the impatient: It's available right now in v9.1.0-next.0对于不耐烦的人:它现在在v9.1.0-next.0 中可用


When using the CLI:使用 CLI 时:

ng generate component dashboard --displayBlock=true

Settting a default value in angular.json :angular.json 中设置默认值:

...
"projectType": "application",
"schematics": {
    "@schematics/angular:component": {
      "displayBlock": true
   }
 }
...

From now on any generated component, be it with the css inlined or not, will contain the css:从现在开始,任何生成的组件,无论是否内联 css,都将包含 css:

:host { display: block; }

More detailed information is available here: https://blog.rryter.ch/2020/01/19/angular-cli-generating-block-components-by-default/更详细的信息可在此处获得: https : //blog.rryter.ch/2020/01/19/angular-cli-generate-block-components-by-default/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM