我正在处理旋转甚至change
:
<div @change="handleRotate"></div>
<script>
export default {
data: {
rotate = 0
},
methods: {
handleRotate () {
this.rotate = this.rotate + this.getRotateAngle(e.clientX, e.clientY)
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
现在,第二个this.rotate
运行在每个change
.我该怎么做才能使第二次this.rotate
只handleRotate
运行第二次?
Sau*_*abh 14
你可以使用$ once,它只会监听一次事件.
听一下自定义事件,但只能听一次.一旦第一次触发,侦听器将被删除.
你只需要添加.once
到@change
喜欢以下内容:
<div @change.once="handleRotate"></div>
<script>
export default {
//No Change here
}
</script>
Run Code Online (Sandbox Code Playgroud)
检查演示是否在小提琴中.
旧答案:
如果您不想设置初始值rotate
,则可以再增加一个变量:hasRotated
跟踪旋转是否已更改.最初设置hasRotated
为true,一旦将rotate更改设置hasRotated
为false,如下所示:
<div @change="handleRotate"></div>
<script>
export default {
data: {
rotate: 123,
hasRotated: false
},
methods: {
handleRotate () {
if(this.hasRotated){
this.rotate = this.rotate + this.getRotateAngle(e.clientX, e.clientY)
this.hasRotated = false
}
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
一个简单的解决方案是添加一个标记,如下所示:
<script>
export default {
data: {
rotate = 0
},
methods: {
handleRotate () {
if(!this.rotated){
this.rotate = this.rotate + this.getRotateAngle(e.clientX, e.clientY);
this.rotated = true;
}
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
当然,您需要启动this.rotated为false