javascript - 如何在 Vue JS 中设置动态样式属性

标签 javascript vue.js

我正在尝试将元素的最小高度设置为 .vue 页面。 style 属性绑定(bind)到 styleObject

    <b-container class="mt-3" v-bind:style="styleObject">

我在我的脚本中使用 mounted 来设置 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";
      }
    }

容器高度不调整

最佳答案

当你声明它们时,你不应该使用其他数据 Prop 作为另一个数据 Prop 的值,因为它们不会是 react 性的。

避免:

styleObject: {
      minHeight: this.containerHeight
    }

只需将其设置为 null 即可

styleObject: {
      minHeight: null
    }
this.styleObject.minHeight = ht + "px";

关于javascript - 如何在 Vue JS 中设置动态样式属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589272/

相关文章:

javascript - JQuery 模态对话框确认不会向用户显示

javascript - 如何在Vue数据中存储FileList对象

javascript - 如何在 CasperJS 中打开新标签页

javascript - 如何在 Bioclipse javascript 编辑器中将字符串转换为输入流?

javascript - 在php中设置外部css/js/jpg文件的过期时间

javascript - Vuejs如何将点击功能设置为语义UI的关闭功能?

javascript - 如何向 Vue 路由器路径添加 float 验证?

javascript - 如何异步获取语言环境(nuxt.js + i18n + axios)

javascript - 为什么 Vue.js 使用单字标签 <component>,这违反了他们自己的风格指南?

javascript - 如何减小 babel-polyfill 的大小(缩小后 150 KB)?