TypeScript 用户退出功能代码优化
这段代码有几个问题:
-
User函数没有定义返回类型,应该使用async function User(): Promise<void>来指定返回类型为Promise<void>。 -
User函数中的await关键字需要在async函数内使用。你可以将其放在一个async函数中,或者使用then和catch处理Promise。 -
ElNotification应该被调用为ElNotification.success()。
根据以上修改,你可以尝试这样重写代码:
import { UserLogin } from '../api'
import { removeToken } from '@/utils/token'
import { ElNotification } from 'element-plus'
// 刷新页面
const refreshPage = () => {
location.reload();
}
export async function User(): Promise<void> {
try {
await UserLogin.logout();
removeToken('token');
ElNotification.success({
title: '成功',
message: '退出成功',
});
setTimeout(refreshPage, 1000);
} catch (error) {
console.log(error);
}
}
原文地址: https://www.cveoy.top/t/topic/qkmm 著作权归作者所有。请勿转载和采集!