【发布时间】:2019-09-17 19:21:14
【问题描述】:
我尝试为我的一个学生制作一个打字程序。 我使用 SVG 生成单词,并使用与 vuejs 的类绑定。 当用户按下右键时,字符的颜色应该会改变。 但由于某种原因,它不起作用。 谁能告诉我哪里出错了?
当我将更改分配给数组时,只需单击按钮,它就会起作用。 当我在按键后 console.log 数组时,数组改变了,但字符的颜色没有!
export default {
data(){
return{
word:["a","p","p","e","l","m","o","e","s"],
letterId:0,
kleur:[false,false,false,false,false,false,false,false,false],
}
},
methods:{
checkLetter(event){
console.log('key pressed: ' + event.which)
console.log('letter' +this.letterId+' is: ' + this.letter)
if(event.key == this.letter){
console.log("correct key was pressed")
this.kleur[this.letterId] = true
this.letterId++
}
}
},
computed:{
letter(){
//determine which letter is expected
return this.word[this.letterId]
},
viewbox(){
//adjust viewbox according to the length of the word
var width = (this.word.length * 50) + 50
return "0 0 "+width + " 70"
}
},
created: function () {
window.addEventListener('keyup', this.checkLetter)
},
}
.green{
font: bold 35px Courier;
fill:green;
}
.normaal{
font: bold 35px Courier;
fill:black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<template>
<div>
<svg :viewBox="viewbox" width="100%">
<text v-for="letter,index in word" :x="index*40" y="45" :id="index" :class="{green:kleur[index], normaal:!kleur[index]}">{{letter}}</text>
</svg>
</div>
</template>
【问题讨论】:
-
我不确定这个模板中的什么是反应式的?这样看:有一个观察者检查你的 DOM 是否需要重绘。而且,如果您更改 Array 内部的内容,则没有任何改变。您的模板中没有任何更改。如果您要在
标签: javascript vue.js svg