Vue —— Axios 发送请求(常用格式)

Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求。

基本格式

	axios
	.get('./carList.json') 
	.then(res => {
    
    
		this.info = res.data
	})
	.catch(function(error){
    
    
		console.log(error)
	})
  1. .get():链接地址,字符串表示
  2. .then():获取数据,存放到 info 数组里
  3. .catch():请求失败处理

例子

	new Vue({
    
    
		data: {
    
    
			carlist: [] //购物车列表
		},
		mounted() {
    
    
			// 在这里使用axios 发送请求
			axios
			.get('./carList.json') //文件相对路径
			.then(res => {
    
    
				this.carlist = res.data
			})
			.catch(function(error){
    
    
				console.log(error)
			})
		}
	})

不积跬步无以至千里 不积小流无以成江海

猜你喜欢

转载自blog.csdn.net/qq_45902692/article/details/123953987