简体   繁体   English

宿主样式应用于Angular2中的组件

[英]Host Styles Applied to Component in Angular2

I am currently in the process of creating a custom form component for my Angular component and am running into some styling issues. 我目前正在为Angular组件创建自定义表单组件,并且遇到了一些样式问题。

I'd like for my input to be able to take variable widths and for them to be controlled by the host component. 我希望我的输入能够采用可变宽度,并由主机组件控制它们。

For example: <my-input class="input"> would have a corresponding class as such: 例如: <my-input class="input">将具有相应的类,例如:

.input {
    width: 250px;
}

For some reason, if I do this, the rules are just ignored and not applied to my component. 出于某种原因,如果执行此操作,则规则将被忽略并且不会应用于我的组件。

I've seen others wrap their components in what seems like unnecessary divs and then styling those parent divs so that the content inside fits. 我已经看到其他人将它们的组件包装在似乎不必要的div中,然后对这些父div进行样式设置,以使内容适合内部。 I don't want to write wrappers just for styling purposes when a CSS rule would suffice. 当CSS规则足够时,我不想只为样式编写包装器。

Would doing this involve using :host ? 这样做是否需要使用:host Also, how about, host-context(.input) and then applying the classes inside of the consumers CSS ? 此外,怎么样, host-context(.input)然后在消费者CSS内应用这些类?

I would advice against using ::ng-deep since the Angular team is planning on deprecating it . 我建议不要使用::ng-deep since Angular团队计划弃用它

If you want to style something within your component from whatever component is hosting yours I suggest you create a host-binding/input and then use that value to bind to your component using the style-directive. 如果要从托管您的组件的任何组件中为组件中的样式设置样式,建议您创建一个主机绑定/输入,然后使用样式指令使用该值绑定到您的组件。

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

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