javascript - 带有加载程序的 Vuetify 按钮继续处于加载状态
问题描述
我用按钮加载器制作了简单的 vuetify 登录表单。但是通过 axios 发送 POST 请求后,按钮不会恢复到正常状态。它继续加载状态。
<v-btn color="primary" block x-large depressed :loading="loading" :disabled="loading" color="secondary" @click="submit">Sign In</v-btn>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
email: 'admin@example.com',
password: '',
loading: false
}
},
methods: {
submit() {
this.loading = true;
axios({
method: 'post',
url: remote_url,
data: {
username: this.email,
password: this.password
}
})
.then(function (response) {
this.loading = false;
console.log(response.data);
})
.catch(function (response) {
this.loading = false;
});
}
}
})
难道我做错了什么 ?
解决方案
正确的解决方案是:
this.loading = true;
axios({
method: 'post',
url: remote_url,
data: {
username: this.email,
password: this.password
}
})
.then((response) => {
// response handler
})
.catch((error) => {
// error handler
})
.finally(() => {
this.loading = false;
});
知道,如果你使用function () {}
,你的范围将会改变:
myFunction()
.then(function () {
// myfunction() scope
// this = myFunction prototype
})
myFunction()
.then(() => {
// stay in current scope
})
推荐阅读
- ios - 如何检查今天小部件的数据
- python - TypeError: '(slice(None, None, None), array([ True, False, ... True, True]))' 是无效键
- matlab - 在 MATLAB 中使用 FFT 并在高斯上正确重新缩放
- java - ListView 未显示 Wifi 列表数组
- python - 子进程的 Python 替代方法
- android - Android Studio 可以很好地构建应用程序,但它在模拟器/设备上崩溃
- c++ - 如何将函数集合传递给客户端类以将它们用作客户端类本身的成员?
- python - 使用 x 轴格式绘制区域图
- java - Java 中的异步文件 NIO 有什么好处?
- excel - Excel 数据透视表列出重复条目(不计算它们)