javascript - 如何在 Vue JS 中设置动态样式属性
问题描述
我正在尝试将元素的最小高度设置为.vue
页面。style 属性绑定到styleObject
<b-container class="mt-3" v-bind:style="styleObject">
我在我的脚本中使用 mount 来设置 styleObject。
export default {
data: function() {
containerHeight: null,
styleObject: {
minHeight: this.containerHeight
}
},
mounted() {
let headerHeight = document.querySelector('#header').offsetHeight;
let navHeight = document.querySelector('#main-menu').offsetHeight;
let footerHeight = document.querySelector('#footer').offsetHeight;
let ht = screen.height - (headerHeight + navHeight + footerHeight);
alert(ht);//the calculated height is displayed
this.containerHeight = ht + "px";
}
}
容器高度不调整
解决方案
当您声明它们时,不应将其他数据道具用作另一个数据道具的值,因为它们不会是反应性的。
避免:
styleObject: {
minHeight: this.containerHeight
}
只需将其设置为 null 并执行
styleObject: {
minHeight: null
}
this.styleObject.minHeight = ht + "px";
推荐阅读
- windows-10 - Docker:运行使用依赖项构建的图像时出现超时错误
- php - jQuery表单提交不起作用php
- googlemock - 模拟对象泄漏
- java - 杰克逊 - 抽象参考列表无法反序列化
- robotframework - 如何使用机器人框架验证 excel 表(数学和基本功能)
- android - RecyclerView 优化问题
- react-native - React Native + Expo:从 url 保存 pdf/word 文档并使用链接在不同的应用程序中打开文件
- c# - Linq to SQL 不会对所有行求和
- c# - 懒惰的
为什么会得到 Func - angular - Angular Universal (SSR) 不预渲染语言