javascript - this.moveImage 不是函数
问题描述
我正在尝试将 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-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>
如您所见,我正在使用 vue.js 并收到错误“this.moveImage 不是函数”,请帮助我解决该问题!
解决方案
那是因为moveImage
is not a method is a computed property
. 作为一个计算属性,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);
}
推荐阅读
- javascript - 我的 React 函数被调用了两次,第二次调用设置值不正确
- java - 为什么我得到 java.lang.NullPointerException?
- python-3.x - 使用 Pymodbus (Modbus RTU) 读取寄存器
- c# - 从包含对象的 API 调用 JSON 数组
- javascript - 创建一个二维码图像然后转换为base64
- macos - MacOS 音频:使用子图的 AUGraph API 的多个输出?
- excel - VBA:仅当系列存在时按名称着色图表系列
- python - Anaconda Navigator 无法启动任何应用程序
- php - 在扩展主刀片的其他刀片中访问主刀片中定义的 php 变量。PHP Laravel
- react-native - ReactNative 可以在 Mac 上运行 Windows 应用模拟器吗?