如何动态设置vue组件的宽度和高度?

3 篇文章 0 订阅
  1. 在组件最外层加上div
  2. 给div加上属性:style=“{ height: toggleHeight ? toggleHeight : ‘2000px’, width: toggleWidth ? toggleWidth : ‘1188px’ }”
  3. 使用js修改toggleHeight 和 toggleWidth 的值 实现动态修改宽高

例 在父组件中动态修改子组件的宽度
auditInformation子组件中
在这里插入图片描述
index父组件中


 1. 使用this.$refs.mainTable.$el.offsetWidth 获取mainTable组件的宽度
 2. 使用this.$refs.auditInformation.toggleWidth = width + 'px' 修改auditInformation组件的宽度 使auditInformation组件和mainTable组件宽度相同
 3. 显示auditInformation组件

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,你可以使用插槽(slot)和props来实现父组件向子组件传递参数,并动态设置组件中的div元素的宽度高度。 首先,在子组件中定义一个div元素,并使用props接收父组件传递的宽度高度参数。可以像这样定义子组件: ```html <template> <div :style="{ width: width, height: height }"> <!-- 子组件的内容 --> </div> </template> <script> export default { props: { width: { type: String, required: true }, height: { type: String, required: true } } } </script> ``` 然后,在父组件中引入子组件,并通过v-bind指令将父组件宽度高度参数传递给子组件。可以像这样引入子组件: ```html <template> <div> <!-- 父组件的内容 --> <child-component :width="parentWidth" :height="parentHeight"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { parentWidth: '200px', parentHeight: '300px' } } } </script> ``` 在上面的例子中,父组件通过v-bind指令将parentWidth和parentHeight传递给子组件的width和height props。子组件接收到这些props后,将会动态设置div元素的宽度高度,从而实现子组件中的div元素的变化。 请注意,父组件中的宽度高度参数可以是动态的,你可以根据需要在父组件中进行修改。同时,你也可以根据具体需求在子组件中添加其他的样式或逻辑。 希望这个例子能够帮助你理解如何在Vue动态设置组件中的div元素的宽度高度参数。 #### 引用[.reference_title] - *1* *2* *3* [uniapp 子组件 props拿不到数据_Vue 组件通信 12 种解决方案](https://blog.csdn.net/weixin_39638929/article/details/110124842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值