• VUE3 使用axios网络请求


    1.新建工程

    参考,VUE3 环境搭建:https://blog.csdn.net/LQ_001/article/details/136293795,运行命令 vue create vue-demo

    2.引入axios

    不管何种引用,都要在工程中安装 axios 包。安装命令:npm install --save axios

    2.1 组件引用

    直接看代码,代码有注释:

    <template>
      <div class="hello"></div>
    </template>
    
    <script>
    import axios from "axios"  // 组间引用 axios
    import querystring from "querystring"  // POST方式,,需要安装 npm install --save querystring
    
    export default {
      name: 'HelloWorld',
      mounted(){
    
        // get请求方式
        axios({
            method:"get",  // 1. 使用get的请求方式
            url:"http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php"  // 2. 输入请求网址
        }).then(res =>{
            // 3.控制台输出请求结果
            console.log(res.data);
        })
    
        // post 请求方式
        axios({
            method:"post",
            url:"http://iwenwiki.com/api/blueberrypai/login.php",
            data:querystring.stringify({  // 此处使用 querystring
                user_id:"iwen@qq.com",
                password:"iwen123",
                verification_code:"crfvw"
            })
        }).then(res => {
            console.log(res.data)
        })
    
        // 快捷get方案
        axios.get("http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php").then(res => {
            console.log(res.data);
        })
    
    	// 快捷POST方案
        axios.post("http://iwenwiki.com/api/blueberrypai/login.php", querystring.stringify({
          user_id:"iwen@qq.com",
          password:"iwen123",
          verification_code:"crfvw"
        })).then(res => {
            console.log(res.data)
        })
      }
    }
    
    </script>
    <style scoped>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    结果如下图,四种方式均请求到数据。

    2.2 全局引用

    全局引用更加简单。首先,src\main.js文件中,axios挂载到全局。

    import { createApp } from 'vue'
    import App from './App.vue'
    import './registerServiceWorker'
    import axios from 'axios'
    
    const app = createApp(App)
    
    // 将axios挂载到全局
    app.config.globalProperties.$axios=axios
    app.mount('#app')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    其次,在布局文件里面去掉 import axios from "axios" ,将所有axios函数名替换为this.$axios。修改后的代码如下:

    <template>
      <div class="hello"></div>
    </template>
    
    <script>
    import querystring from "querystring"  // POST方式,,需要安装 npm install --save querystring
    
    export default {
      name: 'HelloWorld',
      mounted(){
    
        // get请求方式
        this.$axios({  // 使用了全局挂载方式
            method:"get",
            url:"http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php"
        }).then(res =>{
            // 3.控制台输出请求结果
            console.log(res.data);
    
        })
    
        // post 请求方式
        this.$axios({  // 使用了全局挂载方式
            method:"post",
            url:"http://iwenwiki.com/api/blueberrypai/login.php",
            data:querystring.stringify({  // 此处使用 querystring
                user_id:"iwen@qq.com",
                password:"iwen123",
                verification_code:"crfvw"
            })
        }).then(res => {
            // 注意这次接收的是对象类型数据,要将其转化为字符串类型
            // 转化需要安装 npm install --save querystring
            console.log(res.data)
        })
    
    
        // 快捷get方案
        this.$axios.get("http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php").then(res => {
            console.log(res.data);
        })
    	
    	// 快捷POST方案
        this.$axios.post("http://iwenwiki.com/api/blueberrypai/login.php", querystring.stringify({
          user_id:"iwen@qq.com",
          password:"iwen123",
          verification_code:"crfvw"
        })).then(res => {  // 转化需要安装 npm install --save querystring
            console.log(res.data)
        })
      }
    }
    </script>
    
    <style scoped>
    
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    结果如下图,四种方式均同样请求到数据。

  • 相关阅读:
    【6 进程间通信】
    【C++从0到王者】C++11(全文三万字,超详解)
    Git基础教程详解
    推出一系列GaN功率放大器: QPA2211、QPA2211D、QPA2212、QPA2212D、QPA2212T,支持卫星通信和5G基础设施。
    top 修改进程的优先级
    轻量级的搜索引擎MeiliSearch
    MQ消息的自动应答和手动应答| RabbitMQ系列(三)
    Linux常用命令总结
    COLE HERSEE 48408 工业4.0、制造业X和元宇宙
    一篇五分生信临床模型预测文章代码复现——Figure1 差异表达基因及预后基因筛选——火山图,Venn图,热图绘制(二)
  • 原文地址:https://blog.csdn.net/LQ_001/article/details/136590601