我正在尝试使用加载功能制作一个简单的 Vuetify 按钮。Vuetify 已预制组件。我正在尝试将他们提供的代码转换为与组合 api 一起使用。
我遇到了问题this
。
这是 Vuetify 提供的:
<template>
<div class="text-center">
<v-btn
class="ma-2"
:loading="loading"
:disabled="loading"
color="secondary"
@click="loader = 'loading'"
>
Accept Terms
</v-btn>
</div>
</template>
<script>
export default {
data () {
return {
loader: null,
loading: false,
}
},
watch: {
loader () {
const l = this.loader
this[l] = !this[l]
setTimeout(() => (this[l] = false), 3000)
this.loader = null
},
},
}
</script>
<style>
.custom-loader {
animation: loader 1s infinite;
display: flex;
}
@-moz-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
@-webkit-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
@-o-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
@keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
</style>
所以转换为组合 API,这就是我所在的位置。我的watch
功能显然是非常错误的。不确定这是否是使用这个新的正确方法watch
。另一个问题是如何处理this
这种格式的情况:
import { ref, watch } from '@vue/composition-api'
setup () {
let loader = ref(null)
let loading = ref(false)
watch( () => {
loader () {
const l = loader.value
this[l] = !this[l]
setTimeout(() => (this[l] = false), 3000)
loader.value = null
}
})
return {
loader,
loading,
}
}