vue-resource与 axios

vue-resourcevue-resource vue.js的一个插件作用:就是发送网络请求 和jQuery相比 功能更加纯粹(只做一件事)发送请求是通过向vue的实例化对象里面添加$http属性 调用get方法 可以发送get请求 post请求 还可以请求跨域资源为什么不推荐使用vue-resource库?1.vue-resource 不需要与vue深度结合 可以使用第三方库2.第三方库或持续迭代更新 功能更全面3.vue团队结合前面两点 觉得没必要花精力去维护vue
摘要由CSDN通过智能技术生成

vue-resource

  • vue-resource vue.js的一个插件
    • 作用:
      • 就是发送网络请求 和jQuery相比 功能更加纯粹(只做一件事)
    • 发送请求
      • 是通过向vue的实例化对象里面添加$http属性 调用get方法 可以发送get请求 post请求 还可以请求跨域资源
    • 为什么不推荐使用vue-resource库?
      • 1.vue-resource 不需要与vue深度结合 可以使用第三方库
      • 2.第三方库或持续迭代更新 功能更全面
      • 3.vue团队结合前面两点 觉得没必要花精力去维护vue-resource
      • 4.尤雨汐推荐使用axios
html:
	<div id="app">
        {{weatherList}}
        <input type="button" value="点我发请求" @click="search">
    </div>
js:
	<script src="vue.min.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>

    <script>
        const app =new Vue({
            el : "#app",
            data : {
                weatherList : []
            },
            methods : {
                search(){
                    this.$http.get('http://wthrcdn.etouch.cn/weather_mini?city=武汉').then(res => {
                        this.weatherList = res.body.data.forecast;
                        console.log(this.weatherList);
                    },err => {
                        console.log(err);
                    })
                }
            }
        })
    </script>

axios

  • Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
  • 案例
// 为给定 city 的 http://wthrcdn.etouch.cn/weather_min创建请求
	axios.get("http://wthrcdn.etouch.cn/weather_mini?city=武汉").then(res => {
        console.log(res.data);
    }).catch(err => {
        console.log(err);
    })
// 上面的请求也可以这样做
	axios.get("http://wthrcdn.etouch.cn/weather_mini", {
    	params: {
      		city : 武汉
    	}
  	}).then(res => {
        console.log(res.data);
    }).catch(err => {
        console.log(err);
    })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值