一、主地址:

  1. //src/utils/config.js
  2. import axios from 'axios'
  3. axios.defaults.baseURL = 'http://192.168.14.49:5000';
  4. Vue.prototype.axios = axios; // 将主地址赋值绑定给 axios 方便不同页面发送请求
  5. export default Vue; //导出Vue

二、用法:

  1. //.vue
  2. //在data中定义要循环的数组
  3. <script>
  4. export default {
  5. data() {
  6. return {
  7. tracks:[]
  8. }
  9. }
  10. }
  11. </script>
  12. /*需写在vue文件的script标签中,并写在mounted里面*/
  13. // 用then触发
  14. <script>
  15. export default {
  16. mounted(){
  17. this.axios.get(`playlist/detail?id=${this.id}`).then(res=>{
  18. this.tracks = res.data.playlist.tracks;
  19. console.log(this.tracks);
  20. })
  21. }
  22. </script>

三、axios-get

parmas参数传递数据给后端

  1. methods: {
  2. handlePrice(gt, lt) {
  3. console.log(gt, lt);
  4. this.$http({
  5. url: "/goods/price",
  6. method: "get",
  7. params: {
  8. gt,
  9. lt
  10. }
  11. }).then(res => {
  12. this.goodsList = res.data.result;
  13. });
  14. }
  15. }