简体   繁体   English

将样式绑定到vue中的子组件

[英]binding style into child component in vue

I have a child component.我有一个子组件。 I want to bind inline style into that component.我想将内联样式绑定到该组件中。 I want to pass these style properties( height: 200px; overflow-y: scroll).我想传递这些样式属性(高度:200px;溢出-y:滚动)。

I have tried by passing like this:我试过这样传递:

<Childcomponent style="height: 200px; overflow-y: scroll;" /> <Childcomponent style="height: 200px; overflow-y: scroll;" /> but not working. <Childcomponent style="height: 200px; overflow-y: scroll;" />但不工作。

Then i have tried like this:然后我试过这样:

<Childcomponent:style="{'height': '200px'; 'overflow-y': 'scroll'}" /> it is also not working. <Childcomponent:style="{'height': '200px'; 'overflow-y': 'scroll'}" />它也不起作用。

How to bind inline style to this child component?如何将内联样式绑定到这个子组件?

Yo can do following:哟可以做到以下几点:

<div style="height: 200px; overflow-y: scroll;">
   <Childcomponent />
</div>

If you want it to be done in your way, you have to create prop in your component, pass styles into it and then apply those on tag inside of your component.如果你希望它以你的方式完成,你必须在你的组件中创建prop ,将 styles 传递给它,然后将它们应用到组件内部的标签上。

Like that:像那样:

In Childcomponent file addChildcomponent文件中添加

prop {
   myStyles: String;
   ...
}

Those styles you will use in tag you want (I believe, in root one).那些 styles 您将在您想要的标签中使用(我相信,在根目录中)。 Then you can pass styles from parent in this way:然后您可以通过这种方式从父级传递 styles :

<Childcomponent my-styles="height: 200px; overflow-y: scroll;"/>

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

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