Learn the coding style of seniors---interface processing

What I do when accessing the interface:

// 正常引用
import { aaa } from '@/api/xxx'
// 写成方法
methods:{
    async aaaa(){
        const res = await aaa()
        ...业务处理
    }
}
// 调用方法
created(){
    this.aaaa()
}

// 将异步操作封装成一个单独的方法aaaa(),使得代码更加模块化和可读性更高。
// 在created()生命周期钩子函数中调用方法this.aaaa(),使得组件的创建和
// 方法的调用在代码结构上更加清晰。

The handling of the company’s seniors:

// 正常引用
import { aaa } from '@/api/xxx'

// 调用方法
async created(){
    try {
        const res = await aaa()
        ...业务处理
    }catch(error){
    
    }
}

// 使用async和await关键字来处理异步操作,使得代码看起来更加简洁和同步化。
// 直接在created()生命周期钩子函数中调用异步函数,将整个过程封装在一个函数中。

Summarize

How I write it:

More modular and reusable but there will be extra code required for manual encapsulation methods.

Senior’s way of writing:

Asynchronous operations are more concise and easier to read, but the inability to reuse await will lead to callback hell.

Guess you like

Origin blog.csdn.net/WDJ_webDeveloper/article/details/135166520