小编Mel*_*Nik的帖子

如何在带有 vue 的 web worker 中使用 axios?

我已经尝试通过vue 插件在 web worker 中使用 axios 请求

我的代码如下所示:

//worker.js

import axios from 'axios';

export default function getApiData (args) {
    axios.get('/api/test').then(response => {
        console.log(response);
    });
}
Run Code Online (Sandbox Code Playgroud)

和 Vue 的主文件

//main.js

import getApiData from './worker.js';

Vue.use(VueWorker);

window.vueApp = new Vue({
    //...
    created: function () {
        this.updateWorker = this.$worker.create([
            {
                message: 'getApiData ',
                func: getApiData 
            }
        ]);

        this.testWorker.postMessage('getApiData ', [this.args])
            .then(result => {
                console.log(result);
            })
    },
    //...
}
Run Code Online (Sandbox Code Playgroud)

我得到了这个错误

未捕获的 ReferenceError: axios__WEBPACK_IMPORTED_MODULE_0___default 未定义

我做错了什么?

web-worker vue.js axios

5
推荐指数
1
解决办法
3344
查看次数

标签 统计

axios ×1

vue.js ×1

web-worker ×1