Vue 3 不更新内联样式
Vue 3 not updating inline style
我为 vue 3 设置了一些演示代码,用于更新计算对象的内联样式,但它没有更新。
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<link rel="stylesheet" href="main.css">
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.26/dist/vue.global.prod.min.js"></script>
</head>
<body>
<div id="map">
<div id="infantry" class="unit" :style="style">
Position: {{ x }} {{ y }}
</div>
</div>
<script>
const Infantry = {
data() {
return {
x: 0,
y: 0
}
},
mounted() {
setInterval(() => {
this.x++;
this.y++;
}, 1000);
},
computed : {
style() {
return {
top : this.x
};
}
}
}
Vue.createApp(Infantry).mount('#infantry');
</script>
</body>
</html>
这部分不工作
:style="style"
我检查了 dom,它没有设置要使用的样式 top
。有人知道怎么回事吗?
问题是您绑定了安装 vue 应用程序的元素的属性,然后将 px
添加到返回的最高值:
.unit {
position: absolute
}
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<link rel="stylesheet" href="main.css">
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.26/dist/vue.global.prod.min.js"></script>
</head>
<body>
<div id="map">
<div class="unit" :style="style">
Position: {{ x }} {{ y }}
</div>
</div>
<script>
const Infantry = {
data() {
return {
x: 0,
y: 0
}
},
mounted() {
setInterval(() => {
this.x++;
this.y++;
}, 1000);
},
computed: {
style() {
return {
top: this.x + "px"
};
}
}
}
Vue.createApp(Infantry).mount('#map');
</script>
</body>
</html>
我为 vue 3 设置了一些演示代码,用于更新计算对象的内联样式,但它没有更新。
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<link rel="stylesheet" href="main.css">
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.26/dist/vue.global.prod.min.js"></script>
</head>
<body>
<div id="map">
<div id="infantry" class="unit" :style="style">
Position: {{ x }} {{ y }}
</div>
</div>
<script>
const Infantry = {
data() {
return {
x: 0,
y: 0
}
},
mounted() {
setInterval(() => {
this.x++;
this.y++;
}, 1000);
},
computed : {
style() {
return {
top : this.x
};
}
}
}
Vue.createApp(Infantry).mount('#infantry');
</script>
</body>
</html>
这部分不工作
:style="style"
我检查了 dom,它没有设置要使用的样式 top
。有人知道怎么回事吗?
问题是您绑定了安装 vue 应用程序的元素的属性,然后将 px
添加到返回的最高值:
.unit {
position: absolute
}
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<link rel="stylesheet" href="main.css">
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.26/dist/vue.global.prod.min.js"></script>
</head>
<body>
<div id="map">
<div class="unit" :style="style">
Position: {{ x }} {{ y }}
</div>
</div>
<script>
const Infantry = {
data() {
return {
x: 0,
y: 0
}
},
mounted() {
setInterval(() => {
this.x++;
this.y++;
}, 1000);
},
computed: {
style() {
return {
top: this.x + "px"
};
}
}
}
Vue.createApp(Infantry).mount('#map');
</script>
</body>
</html>