Multiple components in Vue can share data and method mixin usage

Project requirements: calculate the height of the table, mixin is divided into global reference and local reference

 

New mixin.js

export const publicMinxin= {
  data() {
    return {
      screenHeight: document.body.clientHeight, // 默认值
      tableHeight: null, // 表格高度 
    }
  },
  methods: {
    getTbHeight() {
      var toolbarFormH=document.getElementById("toolbar-form").offsetHeight;
      this.tableHeight =this.screenHeight -toolbarFormH-331-14;// 初始化表格高度
    },
  },
 
}

 

Partial use

Use in index.vue:

<el-table border :data="tableData" style="width: 100%" highlight-current-row :height="tableHeight">
</el-table>
<script>
    import {publicMinxin} from "../../utils/mixin"
    export default{
        mounted(){
            this.getTbHeight();
        }
    }
</script>

Global use

Main.js introduction: Note that the introduction should be placed before the Vue instance

import {publicMinxin} from "./utils/mixin"
Vue.mixin(publicMinxin)

Use in index.vue:

<el-table border :data="tableData" style="width: 100%" highlight-current-row :height="tableHeight">
</el-table>
<script>
    //直接使用
    export default{
        mounted(){
            this.getTbHeight();
        }
    }
</script>

Use global mixin objects with caution, because it will affect each Vue instance created separately (including third-party templates)

Guess you like

Origin blog.csdn.net/SmartJunTao/article/details/108344604