The first step: create a file in the utils directory as directive, create a new auto-height.js file,
export default {
componentUpdated(el, binding, vnode) {
const ctx = vnode.context;
if (!ctx || typeof ctx[binding.arg] === 'undefined' || ctx.autoHeightResizeListenner) return;
ctx.autoHeightResizeListenner = () => {
let top = el.offsetTop;
let cur = el.offsetParent;
while(cur !== null) {
top += cur.offsetTop;
cur = cur.offsetParent;
}
const h = (window.innerHeight - top) + binding.value;
ctx[binding.arg] = Math.max(h, 100 );
};
window.addEventListener('resize', ctx.autoHeightResizeListener, false);
setTimeout(ctx.autoHeightResizeListenner, 50);
},
unbind(el, binding, vnode){
const ctx = vnode.context;
if(ctx && ctx.autoHeightResizeListener) {
window.removeEventListener('resize', ctx.autoHeightResizeListener, false);
ctx.autoHeightResizeListener = null;
}
},
};
Step 2: Global configuration Import the auto-height.js file in main.js
import autoHeight from './utils/directive/auto-height';
Vue.directive('auto-height',autoHeight)
Step 3: Add two attributes to the label needed in the component v-auto-height:maxHeight ='-10' :max-height="maxHeight"
<el-table v-auto-height:maxHeight = '-10' :max-height="maxHeight"></el-table>