这段代码有几个问题:

  1. User 函数没有定义返回类型,应该使用 async function User(): Promise<void> 来指定返回类型为 Promise<void>。

  2. User 函数中的 await 关键字需要在 async 函数内使用。你可以将其放在一个 async 函数中,或者使用 then 和 catch 处理 Promise。

  3. 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);
    }
}
TypeScript 用户退出功能代码优化

原文地址: https://www.cveoy.top/t/topic/qkmm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录