【发布时间】:2021-05-19 09:23:27
【问题描述】:
我尝试使用动态属性呈现页面。我的代码
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="root">
<div v-for="current in 2" :key="current">
<p :style="currentObject.color">
{{ current }}
{{ currentObject.text }}
</p>
</div>
</div>
脚本是
let vm = new Vue({
el : "#root",
created: function () {
console.log(this.current)
},
data : {
current : 0,
arrs : [
{
color : "background-color: blue;",
text : "Dabodee Dabodai"
},
{
color : "background-color: red;",
text : "Angry"
},
{
color : "background-color: green;",
text : "See it works!"
}
]
},
computed : {
currentObject : function() {
return this.arrs[this.current];
}
}
});
我想通过 currentObject 使用不同的颜色和容器对象文本来 p 标记,但是当渲染页面时,计算的行为就像 current 总是 0。输出为蓝色,currentObject 中的文本为“Dabodee Dabodai”
我做错了什么?
【问题讨论】:
标签: javascript html vue.js