欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > Vue引入外部异步js函数并接收返回值

Vue引入外部异步js函数并接收返回值

2025/6/17 7:31:19 来源:https://blog.csdn.net/qq_59626859/article/details/145519949  浏览:    关键词:Vue引入外部异步js函数并接收返回值
 export default 和export :
 myModule.js//export default 只能出现一次,在引入的时候直接写,不用在花括号里面结构赋值
export default function defaultFunction() {console.log('This is the default function');
}export function anotherFunction() {console.log('This is another function');
}export const myVariable = 'This is myVariable';// 另一个文件
import defaultFunc, { anotherFunction, myVariable } from './myModule';
defaultFunc(); // 输出:This is the default function
anotherFunction(); // 输出:This is another function
console.log(myVariable); // 输出:This is myVariable
场景:

在Vue项目里面,外部定义一个upload.js文件,在里面执行异步上传的upFile函数。在Fille.vue里面引用并接受函数返回值。

test.js写法一 async 与 await:
const axios = require('axios');
export async function up(data) {let result = await axios({method: 'post',url: `api接口`,data: { eventNam: data },headers: {token: localStorage.getItem('token')}})return result   需要在外部返回}写法二 new Promise:const axios = require('axios');
export function up(data) {return new Promise((resolve) => {axios({method: 'post',url: `https://lendingapi-sit.dowsure.com/lending/event/add`,data: { eventNam: data },headers: {token: localStorage.getItem('token')}}).then(res => {resolve(res.data)});})
}写法三:const axios = require('axios');export async function up(data) {let result = ''  //获取函数返回值await axios({method: 'post',url: `https://lendingapi-sit.dowsure.com/lending/event/add`,data: { eventNam: data },headers: {token: localStorage.getItem('token')}}).then(res => {result = res.data});return result //获取函数返回值   需要在外部返回
}
File.vue<el-button @click="getTest">调用异步函数</el-button>import { up } from "@/api/test"
async getTest() {let res = await up(111)}

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com