api
let myFirstPromise = new Promise((resolve, reject) => {
// We call resolve(...) when what we were doing asynchronously was successful, and reject(...) when it failed.
// In this example, we use setTimeout(...) to simulate async code.
// In reality, you will probably be using something like XHR or an HTML5 API.
setTimeout( function() {
resolve("Success!") // Yay! Everything went well!
}, 250)
})
myFirstPromise.then((successMessage) => {
// successMessage is whatever we passed in the resolve(...) function above.
// It doesn't have to be a string, but if it is only a succeed message, it probably will be.
console.log("Yay! " + successMessage)
});
export function getRoutes() {
return request({
url: '/vue-element-admin/routes',
method: 'get'
})
}
await必须用在异步函数里.async 是异步函数,会先返回一个[object Promise]
import { getRoutes, getRoles, addRole, deleteRole, updateRole } from '@/api/role'
methods: {
async getRoutes() {
const res = await getRoutes() //会执行完这句再执行下面.
this.serviceRoutes = res.data
this.routes = this.generateRoutes(res.data)
},
async getRoles() {
const res = await getRoles()
this.rolesList = res.data
},
}
评论区