vue中axios请求示例

<script>
  import axios from 'axios'
  export default {
    name: 'Configuration',
    data() {
      return {
        data: {
          pathBy: '',
          useful: '',
        },
        instance: null,
      }
    },
    methods: {
      submitForm(data) {
        this.$refs[data].validate((valid) => {
          if (valid) {
            alert("提交成功")
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(data) {
        this.$refs[data].resetFields();
      },
      cate1(data) {
        if(data==2){
          //页面中触发这个axios请求
          this.instance.get('/detail_nav?path_by=path_by').then(res=>{
              console.log(res);
              this.data.pathBy = res.data.data
            }
          )
        }else{
          this.data.pathBy = '';
          this.data.useful = ''
        }

      },
      }
    },
    //创建一个axios实例
    created() {
      this.instance = axios.create({
        baseURL: 'http://0.0.0.0:3652',  //这里写请求的前缀
        timeout: 1000,
      });
      #下面的请求在每次加载这个页面是默认请求
      this.instance.get('/api').then(res=>{
        console.log(res)
      });
    }
  }
</script>

 

posted @ 2020-04-29 15:29  乔儿  阅读(726)  评论(0编辑  收藏  举报