vue2中安装和使用axios

vue2中安装和使用axios

安装axios:npm i axios
使用App.vue:

<template>
  <div>
    <button @click="getStudentmsg()">获取学生信息</button>
    <button @click="getCarmsg()">获取汽车信息</button>
  </div>
</template>

<script>
import axios from 'axios' 
export default {
    
    
  name: 'App',
  components: {
    
    
    
  },
  methods: {
    
    
    getStudentmsg(){
    
    
        axios.get('http://localhost:8080/api/students').then(response=>{
    
    
          console.log('请求成功了',response.data)
        },
        error=>{
    
    
          console.log('请求失败了',error.message)
        })
    },
     getCarmsg(){
    
    
        axios.get('http://localhost:8080/demo/cars').then(response=>{
    
    
          console.log('请求成功了',response.data)
        },
        error=>{
    
    
          console.log('请求失败了',error.message)
        })
    }
  },
}
</script>

<style>

</style>

猜你喜欢

转载自blog.csdn.net/weixin_44747173/article/details/125983262