您现在的位置:首页 >> 前端 >> 内容

前端框架Vue(3)

时间:2017/4/4 9:17:00 点击:

  核心提示:前端框架Vue(3):vue 中如何支持异步请求,1、vue 支持开发者引入 jquery 使用 $.ajax()。2、vue.resource( 2.0后不再更新)1、 npm 安装 vue-res...
前端框架Vue(3):vue 中如何支持异步请求,1、vue 支持开发者引入 jquery 使用 $.ajax()。

2、vue.resource( 2.0后不再更新)

1、 npm 安装 vue-resource

 npm install vue-resource

2、 main.js 中引入

 import VueResource from 'vue-resource'
  Vue.use(VueResource)

3、使用

this.$http.get('../src/data/a.txt')
    .then(function(res){
              alert(res.data);
          },function(){
              alert('false')
          });

3、推荐使用axios

github地址:https://github.com/mzabriskie/axios
1、npm 安装

npm install axios

2、main.js 中引入

import Axios from 'axios'
Vue.use(Axios)

3、使用

axios.get('url')
     .then(function(res){
    alert(res);
     })
     .catch(function(err){
    alert(err);
     })
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
axios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone'
  }
});

作者:网络 来源:不详