7.Vue中的Http訪問


axios用法:
1.安裝
npm install axios --save-dev

2.導入:
import axios from 'axios';


3.使用($(form)需要先按裝jQuery) //.serialize為提交的form表單中的數據。
axios.post('http://localhost:3000/users/zhuce',$(form).serialize())
.then(function(res){
console.log(res);
alert(res.data);
})
.catch(function(err){
console.log(err);
})


4.$(form)需要先按裝jQuery
npm install jquery --save-dev
到build/webpack.base.conf.js中,
導入:var webpack = require('webpack');
最下面添加:
plugins: [
new webpack.ProvidePlugin({
$:"jquery",
jQuery:"jquery",
"window.jQuery":"jquery"
})
]

vue文件中無需導入,可直接使用$和jQuery,按以上操作就可以實現,不行在把下面的也執行了。

````````````````````````````````````````````````````````````````````````````````

1.安裝
npm install vue-resource --save-dev


2.main.js中
Vue.use(require('vue-resource')); //--------添加


3.vue中使用
callKa:function(event){
this.$http.get('http://localhost:3000/',{}).then(function(data){
alert(data.body);
}).catch(function(err){
alert('出錯');
for(let key in err){
console.log(key+':'+err[key]);
}
})
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM