添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
const requrl = '/user/find/1'
axios.get(requrl).then(response => {
    const user = response.data
    this.username = user.username
    this.age = user.age
}).catch(
    function (error) {
        // 请求失败处理
        alert('请求失败!')

解决方案1
main.js加上Vue.prototype.$axios = axios
axios.get改为this.$axios.get调用,修改后不再报错

import axios from 'axios'
//其他vue组件中就可以this.$axios调用使用
Vue.prototype.$axios = axios
const requrl = '/user/find/1'
this.$axios.get(requrl).then(response => {
    const user = response.data
    this.username = user.username
    this.age = user.age
}).catch(
    function (error) {
        // 请求失败处理
        alert('请求失败!')

解决方案2:结合vue-axios插件,安装插件后,就不需要将axios绑定到Vue原型链上了,组件内通过this.axios调用
npm install axios vue-axios --save

import axios from 'axios'
import VueAxios from "vue-axios";
Vue.use(VueAxios,axios)
//其他vue组件中就可以this.$axios调用使用
//Vue.prototype.$axios = axios
const requrl = '/user/find/1'
this.axios.get(requrl).then(response => {
    const user = response.data
    this.username = user.username
    this.age = user.age
}).catch(
    function (error) {
        // 请求失败处理
        alert('请求失败!')
问题2,并发测试时,TypeError: Cannot read property ‘$axios’ of undefined

在这里插入图片描述
报错代码如下:

function getUserget() {
    return this.$axios.get('/user/find/1')
function getUserpost() {
    return this.$axios.post('/user/find',{id:2})
this.$axios.all([getUserget(), getUserpost()])
    .then(this.$axios.spread(function (res1, res2) {
        // 两个请求现在都执行完成
        const user1 = res1.data
        const user2 = res2.data
        console.log(user1.username)
        console.log(user2.username)
    }));

原因未知,有知道的大佬还望不吝赐教
无奈只得在app.vue(我是在这里做的测试)中再次引入
import axios from 'axios'
再次引入,直接用axios就行

function getUserget() {
    return axios.get('/user/find/1');
function getUserpost() {
    return axios.post('/user/find',{id:2});
                    问题1:ReferenceError: axios is not defined问题代码:const requrl = '/user/find/1'axios.get(requrl).then(response => {    const user = response.data    this.username = user.username    this.age = user.age}).catch(    function (error) {        // 请求失败处
// or
yarn add vue-axios-interceptors
 // Make sure you import this package after you've imported Vue:
window . Vue = require ( 'vue' ) ;
require ( 'vue-axios-interceptors' ) ;
// Make sure the axios package is available globally on the window object:
window . axios = require ( 'axios' ) ;
程序包注册一个新的全局事件总线,该事件在window对象上被intercepted ,并在ajax调用导致错误时在其上发出多个事件。 您可以轻松地侦听这些事件以构建流畅的错误处理工作流,例如在负责显示错误消息的全局组件中:
一个项目学会vue全家桶+axios实现登录、拦截、登出功能,以及利用axios的http拦截器拦截请求和响应。
该项目是利用了Github 提供的personal token作为登录token,通过token访问你的Repository List。通过这个项目学习如何实现一个前端项目中所需要的
登录及拦截、登出、token失效的拦截及对应 axios 拦截器的使用。
你需要先生成自己的 Github Personal Token()。
Token 生成后 访问 ,即可查看你的Repository List。
├── README.md
├── dist  // 打包构建后的文件夹
│   ├── build.js
│   └── build.js.map
├── index.html
├── package.json
├── src
本案例是调用GitHub官方提供的API,基于axios,pubsub-js实现一个演示,目的是让自己更加熟悉vue组件化开发思想,和前分离开发中调用接口来获取数据渲染页面。
本文最初发表在博客园,喜欢看博客的同学也可以前往---
# install dependencies
npm install
# serve with hot reload at localhost:8080
npm run dev
# build for production with minification
npm run build
# build for production and view 
				
vue 编译的时候可能会出现 Error in v-on handler: “ReferenceError: XXX is not defined” 这个问题, 这个可能是因为你没有引用相应的组件, vue 的 js 方法报错, 引用后就可以了 写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个关于 前端开发 的学习思路及方向。从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习
白瞎一天后,开启列文虎克式检查,发这个奇葩的字符问题 <!-- 在VScode手打的 script --> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> ①npm install axiosvue add axios 注意:有些人vue add axios无效,但这没什么大碍,请执行第三步,区别就是系统不能自动生成plugins的axios.js ③在自己需要使用axios页面的script模块中添加:const axios = require('axios'); 注意:前端想获后端的数据,名称以后端的端口名字为主,而不是以自己数据库的字段为主,因为在IJ运行项目后,会自动把大写变成小写。 后端的代码:
1.自己在网上下载axios文件,放入项目src目录下的plugins文件(没有可新建),在main.js文件中引入 import axios from './plugins/axios 2.命令行切换到项目文件夹使用npm安装axios npm install axios 在如图位置使用如下语句声明(是在export default外声明全局变量) const axios = require('axios'); 2.以 axios 官方文档get请求为例子,发现运行时还是会报上述错误 axios.get('/user?ID=12345') .then(function (response) { console.log(response); .catch(function (error) { console.log(error); 3.查阅资料发现在 main.js 中添加以下代码即可解决
这个错误通常是因为在浏览器环境中使用了 Node.js 特定的全局变量。在浏览器中,没有像 `process` 这样的全局变量。 如果你的代码中使用了 `process`,你可以尝试使用条件语句来检查环境,避免在浏览器中使用它。例如: ```javascript if (typeof process === 'undefined') { // 在浏览器环境中执行的代码 } else { // 在 Node.js 环境中执行的代码 如果是第三方库中引起的错误,你可以考虑查看库的文档或者提交一个 issue 来获取支持。他们可能有提供适用于浏览器环境的替代方法或者解决方案。 另外,如果你正在使用 Vue.js 和 vue-pdf 插件,你可能需要确保正确引入和配置了相关的依赖。可以参考官方文档和示例代码来了解正确的用法。 希望这些信息对你有帮助!如果你还有其他问题,请随时提问。