我已经构建了一个VueJS组件,并且在同一个页面上多次使用它。这些组件完全独立地工作,但是组件中有一个方法来获得它的offsetWidth,并将它(带有前缀)作为一个类应用到组件中(例如width-800)。
当页面上只有一个组件时,它可以正常工作。
当有多个事件时,只有最后一个事件应用类。它确实用正确的宽度正确地计算了类,但是没有将它应用于页面上的其他类。
<template>
<div id="app-medialib" v-bind:class="[this.breakpoint]">
<p>{{ this.breakpoint }}</p>this.breakpoint是一个数据属性,它保存类名。
mounted: function(){
var self = this;
this.calculateBreakpoint();
window.onresize = function(event) {
self.calculateBreakpoint();
};
}断点法:
calculateBreakpoint: function(){
var mediaLibraryWidth = null;
var elems = document.getElementsByClassName('header medialib');
for( var i=0; i<elems.length; i++ ){
if( elems[i].offsetParent !== null ){
console.log('elems[i]', elems[i]);
mediaLibraryWidth = elems[i].offsetWidth;
break;
}
}
console.log('calculateBreakpoint', mediaLibraryWidth);
if( mediaLibraryWidth > 956 ) {
this.breakpoint = 'md';
} else if( mediaLibraryWidth < 957 && mediaLibraryWidth > 700 ){
this.breakpoint = 'sm';
} else {
this.breakpoint = 'xs';
}
console.log('calculateBreakpoint', this.breakpoint);
},在这方面,任何帮助都是非常感谢的。一直在挠我的头。
谢谢。
发布于 2017-05-04 17:57:59
当您执行window.onresize并将一个函数绑定到它时,前面的绑定会被重写,所以当您在应用程序中有多个组件时,它们的挂载函数被调用,它将覆盖window.onresize。这就是为什么只更改最后一个组件类的原因。
在您的控制台中尝试以下操作。
window.onresize = function (e) {
console.log('a')
}
window.onresize = function (e) {
console.log('b')
}当你调整尺寸时,只有'b‘会被记录下来。
结帐:What is the best practice to not to override other bound functions to window.onresize?
在您的例子中,使用jquery在window.onresize上绑定函数将是一种更好的方法。
尝试:
$(window).resize(function() {
console.log('a')
})
$(window).resize(function() {
console.log('b')
})当你调整大小时,它会同时记录'a‘和'b’。
因此,将window.onresize更改为:
$(window).resize(function() {
self.calculateBreakpoint();
})或者按照Daniel的建议,如果您想避免使用jquery,请使用:
window.addEventListener('resize', self.calculateBreakpoint )发布于 2017-05-04 19:51:13
在模板外部应用v-bind:class,如下所示
Vue.component("foo", {
template: "<div>{{item}}</div>",
props: ["item"]
});
var app = new Vue({
el: "#app",
data: {
background: "blue",
items: ["a", "b", "c", "d"]
},
mounted: function() {
var self = this;
this.calculateBreakpoint();
window.onresize = function(event) {
self.calculateBreakpoint();
};
},
methods: {
calculateBreakpoint: function() {
var mediaLibraryWidth = null;
var elems = document.getElementsByClassName("app");
for (var i = 0; i < elems.length; i++) {
if (elems[i].offsetParent !== null) {
console.log("elems[i]", elems[i]);
mediaLibraryWidth = elems[i].offsetWidth;
break;
}
}
console.log("calculateBreakpoint", mediaLibraryWidth);
if (mediaLibraryWidth > 956) {
this.background = "red";
} else if (mediaLibraryWidth < 957 && mediaLibraryWidth > 700) {
this.background = "blue";
} else {
this.background = "green";
}
console.log("calculateBreakpoint", this.breakpoint);
}
}
});.red {
background: red;
}
.blue {
background: blue;
}
.green {
background: green;
}
.widget {
margin: 10px;
padding: 20px;
width: 200px;
font-weight: bold;
font-size: 16px;
color: white;
}<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.min.js"></script>
<div id="app" class="app">
<foo v-for="item in items" :class="[background]" class="widget" :item="item">
</div>
https://stackoverflow.com/questions/43786439
复制相似问题