zoukankan      html  css  js  c++  java
  • vue发送ajax请求

    一、vue-resource

    1、简介

      一款vue插件,用于处理ajax请求,vue1.x时广泛应用,现不被维护。

    2、使用流程

    step1:安装

    【命令行输入】
    npm install vue-resource --save

    step2:引入

    【main.js】
    // 引入vue-resource
    import VueResource from 'vue-resource'
    
    // 使用vue-resource
    Vue.use(VueResource)

    step3:编码

    【格式:】
        this.$http.get().then()    返回的是一个Promise对象   

    step4:完整代码

    【使用vue-cli创建项目】
    https://www.cnblogs.com/l-y-h/p/11241503.html
    
    【main.js】
    import Vue from 'vue'
    import App from './App.vue'
    // 引入vue-resource
    import VueResource from 'vue-resource'
    
    // 使用vue-resource
    Vue.use(VueResource)
    Vue.config.productionTip = false
    
    new Vue({
      render: h => h(App),
    }).$mount('#app')
    
    
    【App.vue】
    <template>
        <div>
            <div v-if="!repositoryUrl">loading...</div>
            <div v-else>most star repository is <a :href="repositoryUrl">{{repositoryName}}</a></div>
        </div>
        <!--App -->
    </template>
    
    <script>
        export default {
            data() {
                return {
                    repositoryUrl : '',
                    repositoryName : ''
                }
            },
            
            mounted() {
                // 发ajax请求,用以获取数据,此处地址意思是查询 github中 vue 星数最高的项目
                const url = 'https://api.github.com/search/repositories?q=vue&sort=stars';
                this.$http.get(url).then(
                    response => {
                        const result = response.data.items[0];
                        console.log(result)
                        this.repositoryUrl = result.html_url;
                        this.repositoryName = result.name;
                    },
    
                    response => {
                        alert('请求失败');
                    },
                );
            }
        }
    </script>
    
    <style>
    
    </style>

    step5:截图:

     请求正常

     点击链接跳转

     使用错误的地址

     弹出错误提示框

    二、axios

    1、简介

      一款vue库,用于处理ajax请求,vue2.x时广泛应用。

    2、流程

    step1:安装

    【命令行输入】
    npm install axios --save

    step2:引入

    【在哪里使用,就在哪里引入】
    import axios from 'axios';

    step3:完整代码

    【main.js】
    import Vue from 'vue'
    import App from './App.vue'
    
    Vue.config.productionTip = false
    
    new Vue({
      render: h => h(App),
    }).$mount('#app')
    
    
    
    【App.vue】
    <template>
        <div>
            <div v-if="!repositoryUrl">loading...</div>
            <div v-else>most star repository is <a :href="repositoryUrl">{{repositoryName}}</a></div>
        </div>
        <!--App -->
    </template>
    
    <script>
        import axios from 'axios';
        
        export default {
            data() {
                return {
                    repositoryUrl : '',
                    repositoryName : ''
                }
            },
            
            mounted() {
                // 发ajax请求,用以获取数据,此处地址意思是查询 github中 vue 星数最高的项目
                const url = 'https://api.github.com/search/repositories?q=vue&sort=stars';
                
                axios.get(url).then(
                    response => {
                        const result = response.data.items[0];
                        console.log(result)
                        this.repositoryUrl = result.html_url;
                        this.repositoryName = result.name;
                    }
                ).catch(
                    response => {
                        alert('请求失败');
                    },
                );
            }
        }
    </script>
    
    <style>
    
    </style>

    step5:截图与上面的 vue-resource 一样,此处不重复截图。

    3、axios 解决跨域问题

    参考: https://www.cnblogs.com/l-y-h/p/11815452.html

    4、axios 项目中封装使用

    参考:

      https://www.cnblogs.com/l-y-h/p/12955001.html#_label1

      https://www.cnblogs.com/l-y-h/p/12973364.html#_label1

  • 相关阅读:
    数学之美:判断两个随机信号序列相似度
    为什么自己设计的嵌入式系统不如工业级产品稳定?
    由static来谈谈模块封装
    算法类书籍汇总
    Linux驱动:手把手教hello world驱动配置、编译进内核或为模块
    刨根问底之链表数据结构
    Redis进阶
    构建高可用的写服务
    再推荐 5 款私藏的优质 Chrome 插件
    MySQL-SQL优化
  • 原文地址:https://www.cnblogs.com/l-y-h/p/11656129.html
Copyright © 2011-2022 走看看