【快应用】父组件中如何设置子组件样式

问题背景:

子组件被引用时,如果在父组件的布局效果不理想,需要去修改子组件对应的样式,是比较麻烦的,现在快应用在1100版本之后推出了externalClasses属性,可以将外部样式直接传给自定义组件,便于开发者调试。

使用方式:

1、子组件中声明传递的样式externalClasses: [‘childstyle’]和class=”childstyle”。

2、父组件里调用childstyle=”parentstyle。”

相关代码:

child.ux:

<template>

  <div class="container">

    <text class="txt" onclick="click">child button</text>

  </div>

</template>

<style>

  .container {

    flex: 1;

    flex-direction: column;

    align-items: center;

  }

</style>

<script>

  import prompt from '@system.prompt';

  export default {

    externalClasses: ['txt'],

    click() {

      prompt.showToast({

        message: 'this is child',

      })

    }

  }

</script>

parent.ux:

<import name="child" src="../New/child.ux"></import>

<template>

  <div class="container">

    <text class="txt">hello</text>

    <child txt="parentxt"></child>

  </div>

</template>

<style>

  .container {

    flex: 1;

    flex-direction: column;

    align-items: center;

  }

  .txt {

    height: 150px;

    width: 85%;

    margin-bottom: 15px;

    border: 1px solid #000000;

  }

//父组件设置样式

  .parentxt {

    background-color: #00fa9a;

    height: 150px;

    width: 85%;

    align-items: flex-start;

    margin-bottom: 15px;

    border-color: #9400D3;

    border-width: 5px;

  }

</style>

运行截图:

在这里插入图片描述

欲了解更多更全技术文章,欢迎访问 https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh

### React 中通过父组件控制子组件高度 在 React 应用程序中,可以通过 `props` 将样式传递给子组件。下面展示了一个简单的例子,在此例子中,父组件子组件传递内联样式的对象。 ```jsx // ParentComponent.js import React from 'react'; import ChildComponent from './ChildComponent'; function ParentComponent() { const heightStyle = { height: "200px", backgroundColor: "#f8d7da" }; return ( <div> <ChildComponent style={heightStyle} /> </div> ); } export default ParentComponent; ``` ```jsx // ChildComponent.js import React from 'react'; function ChildComponent(props) { return ( <div style={props.style}> This is a child component with controlled height. </div> ); } export default ChildComponent; ``` 这种方式利用了 React 的特性,即父组件能够通过属性向下传递数据[^1]。 ### Vue 中通过父组件控制子组件高度 对于 Vue 来说,同样可以在父级组件里定义好样式并通过属性的方式传入到子组件内部去应用。这里给出一个基于单文件组件(SFC)[^3]的例子: ```vue <!-- ParentComponent.vue --> <template> <child-component :style="heightStyle"></child-component> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent, }, data() { return { heightStyle: { height: '200px', background: '#bfefff' } }; } } </script> ``` ```vue <!-- ChildComponent.vue --> <template> <div :style="passedStyles"> 这是一个具有受控高度的子组件。 </div> </template> <script> export default { props: ['style'], computed: { passedStyles() { return this.style || {}; } } }; </script> ``` 在这个案例中,父组件设置了特定的高度和其他 CSS 属性作为绑定的数据,并将其作为一个名为 `style` 的 prop 发送给子组件子组件接收该 prop 并应用于自身的根元素上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值