Vue-CLI front and rear end modules interactive project -axios (similar ajax submission)

08.31 self-summary

Front and rear end interactive Vue-CLI project -axios

A. Mounting module

npm install axios --save
#--save可以不用写

II. Configuration main.js

import axios from 'axios'
Vue.prototype.$axios = axios;

III. Use

created() {  // 组件创建成功的钩子函数
    // 拿到要访问课程详情的课程id
    let id = this.$route.params.pk || this.$route.query.pk || 1;
    this.$axios({
        url: `http://127.0.0.1:8000/course/detail/${id}/`,  // 后台接口
        method: 'get',  // 请求方式
    }).then(response => {  // 请求成功
        console.log('请求成功');
        console.log(response.data);
        this.course_ctx = response.data;  // 将后台数据赋值给前台变量完成页面渲染
    }).catch(error => {  // 请求失败
        console.log('请求失败');
        console.log(error);
    })
}

Some submit different settings and ajax

  • ajax in tyleheremethod
  • The ajax successhere is themnot then back braces .out
  • catchPlease failure
  • Content is $axiosbefore

Guess you like

Origin www.cnblogs.com/pythonywy/p/11440622.html