vue3.0使用axios报错

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

vue3.0使用axios报错

cg-cg   2022-09-28 我要评论

vue-cli3.0使用axios的时候出现错误,记录一下

报错信息:

Uncaught TypeError: Cannot set property $axios of #<Vue> which has only a getter
at eval (main.js?56d7:12)
at Module../src/main.js (app.js:11102)
at __webpack_require__ (app.js:724)
at fn (app.js:101)
at Object.0 (app.js:11283)
at __webpack_require__ (app.js:724)
at app.js:791
at app.js:794

vue-cli3.0安装插件的时候要注意区分vue-cli2.0的命令

安装:

//vue-cli2.0命令
$ npm install axios
//vue-cli3.0命令
$ npm add axios

使用:

1.首先在main.js里边把axios实例挂载到Vue原型上
  坑点: 命名的时候不能使用"axios"这个字段
import axios from 'axios'
Vue.prototype.$ajax = axios
2.在需要发送请求的地方使用"$ajax"
this.axioss.get('/api/seller')
        .then(function(res){
            console.log(res);
            this.seller = res.data;
        })
        .catch(function(error){
            console.log(error);
        })

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们