Javascript 此.moveImage不是函数
我正在尝试将setInterval与我的函数moveImage一起使用,这会改变我图像的位置! 这是我的密码:Javascript 此.moveImage不是函数,javascript,function,vue.js,ecmascript-6,setinterval,Javascript,Function,Vue.js,Ecmascript 6,Setinterval,我正在尝试将setInterval与我的函数moveImage一起使用,这会改变我图像的位置! 这是我的密码: <template> <div class="randImg"> <img v-bind:style="{top: imgTop + 'px', left: imgLeft + 'px',height: imgHeight + 'px', width: imgWidth + 'px'}" class="image" v-bin
<template>
<div class="randImg">
<img v-bind:style="{top: imgTop + 'px', left: imgLeft + 'px',height: imgHeight + 'px', width: imgWidth + 'px'}"
class="image" v-bind:src="vue">
</div>
</template>
<script>
const vue = require("../assets/images/vue.png");
export default {
name: "randImg",
data() {
return {
vue,
imgTop: 0,
imgLeft: 0,
imgHeight: 64,
imgWidth: 64,
changeInterval: 1000
}
},
created() {
setInterval(this.moveImage(), this.changeInterval);
},
computed: {
moveImage() {
this.imgTop = Math.round(Math.random() * (screen.height - this.imgHeight));
this.imgLeft = Math.round(Math.random() * (screen.width - this.imgWidth));
}
}
}
</script>
const vue=require(“../assets/images/vue.png”);
导出默认值{
名称:“randImg”,
数据(){
返回{
vue,
imgTop:0,
imgLeft:0,
imgHeight:64,
imgWidth:64,
更换间隔:1000
}
},
创建(){
setInterval(this.moveImage()、this.changeInterval);
},
计算:{
moveImage(){
this.imgTop=Math.round(Math.random()*(screen.height-this.imghight));
this.imgLeft=Math.round(Math.random()*(screen.width-this.imgWidth));
}
}
}
如您所见,我正在使用vue.js,并收到一个错误“this.moveImage不是函数”
请帮我解决那个问题 这是因为
moveImage
不是一个方法,而是一个计算属性。作为计算属性,vue将为其生成一个getter
您需要将定义移动到methods
methods: {
moveImage() {
this.imgTop = Math.round(Math.random() * (screen.height - this.imgHeight));
this.imgLeft = Math.round(Math.random() * (screen.width - this.imgWidth));
}
}
在调用setTimeout
时,您需要返回值的函数,因此需要像这样调用它
created() {
setInterval(this.moveImage, this.changeInterval);
}
这是因为moveImage
不是一个方法,而是一个计算属性。作为计算属性,vue将为其生成一个getter
您需要将定义移动到methods
methods: {
moveImage() {
this.imgTop = Math.round(Math.random() * (screen.height - this.imgHeight));
this.imgLeft = Math.round(Math.random() * (screen.width - this.imgWidth));
}
}
在调用setTimeout
时,您需要返回值的函数,因此需要像这样调用它
created() {
setInterval(this.moveImage, this.changeInterval);
}