使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼
剛接觸vue.js框架的時(shí)候,很傷腦筋。今天整理一下post/get兩種方式,簡(jiǎn)單的調(diào)取數(shù)據(jù)庫數(shù)據(jù),并進(jìn)行渲染,希望幫助大家!
首先,在HTML頁面引入:
//引入vue.js文件 <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script> 引入vue-resource.min.js文件,就可以引入接口方法了 <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
然后,在body中書寫div:
//id在下面js中進(jìn)行引用
<div id="box">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>序號(hào)</td>
<td>姓名</td>
<td>頭像</td>
</tr>
//v-for 循環(huán)數(shù)據(jù)表中的數(shù)據(jù)
<tr v-for="v in msg">
<td>{{v.id}}</td>
<td>{{v.username}}</td>
<td>{{v.photo}}</td>
</tr>
</table>
</div>
第三,js代碼:
<script type = "text/javascript">
window.onload = function(){
//實(shí)例化vue類
var vm = new Vue({
//綁定box
el:'#box',
data:{
//設(shè)置msg內(nèi)容為空,在請(qǐng)求數(shù)據(jù)前為空的狀態(tài)
msg:'',
},
mounted:function () {
//調(diào)取本地的get(就在下面)
this.get();
},
methods:{
get:function(){
//發(fā)送get請(qǐng)求
this.$http.post('http://你的IP/api/方法',{key:"密鑰"},{emulateJSON:true}).then(function(res){
//msg等于回調(diào)函數(shù)返回的res(值)
this.msg=res.body.data;
//在打印臺(tái)測(cè)試打印,無誤后一定要?jiǎng)h除
console.log(res);
},function(){
console.log('請(qǐng)求失敗處理');
});
}
}
});
}
</script>
控制器:
public function index()
{
// //引入秘鑰
$pwd=new ApisModel();
$passwd=$pwd->passwd();
// print_r($passwd);die;
//空的數(shù)組,等待輸入秘鑰與存儲(chǔ)在model層的秘鑰對(duì)比
$date=request()->get();
// print_r($date);die;
// 對(duì)比秘鑰是否一致
if($date['key']==$passwd){
$model=new ApisModel();
$data=$model->role_show();
return json(array('data'=>$data,'code'=>1,'message'=>'操作完成'));
}else{
$data = ['name'=>'status','message'=>'操作失敗'];
return json(['data'=>$data,'code'=>2,'message'=>'秘鑰不正確']);
}
}
model:
public function passwd(){
$key='存放在本地的密鑰';
return $key;
}
//簡(jiǎn)單的測(cè)試接口
public function role_show(){
return Db::name('role_power')->select();
}
OK,post方式搞定了,下面是vue使用get方法進(jìn)行接口調(diào)用,渲染數(shù)據(jù)
簡(jiǎn)單粗暴,大致一樣,就不一一詳解了,上代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測(cè)試實(shí)例 - 菜鳥教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
</head>
<body>
<div id="box">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td style="width:130px;height:30px;">ROLE_ID</td>
<td style="width:130px;height:30px;">POWER_ID</td>
<td style="width:130px;height:30px;">創(chuàng)建時(shí)間</td>
</tr>
<tr v-for="v in msg">
<td style="width:130px;height:30px;">{{v.role_id}}</td>
<td style="width:130px;height:30px;">{{v.power_id}}</td>
<td style="width:130px;height:30px;">{{v.create_time}}</td>
</tr>
</table>
</div>
<script type = "text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
data:{
msg:'',
},
mounted:function () {
this.get();
},
methods:{
get:function(){
//發(fā)送get請(qǐng)求
this.$http.get("http://ip?key=密鑰",{emulateJSON:true}).then(function(res){
console.log(res.body);
this.msg=res.body.data;
},function(){
console.log('請(qǐng)求失敗處理');
});
}
}
});
}
</script>
</body>
</html>
ok,都測(cè)試好了,可以使用,千萬別搞錯(cuò)id哦。
以上這篇使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VUE+axios+php實(shí)現(xiàn)圖片上傳
這篇文章主要為大家詳細(xì)介紹了VUE+axios+php實(shí)現(xiàn)圖片上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
詳解vue-cli項(xiàng)目中用json-sever搭建mock服務(wù)器
這篇文章主要介紹了詳解vue-cli項(xiàng)目中用json-sever搭建mock服務(wù)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
iview-table組件嵌套input?select數(shù)據(jù)無法雙向綁定解決
這篇文章主要為大家介紹了iview-table組件嵌套input?select數(shù)據(jù)無法雙向綁定解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程
Iconfont國(guó)內(nèi)功能很強(qiáng)大且圖標(biāo)內(nèi)容很豐富的矢量圖標(biāo)庫,提供矢量圖標(biāo)下載、在線存儲(chǔ)、格式轉(zhuǎn)換等功能,下面這篇文章主要給大家介紹了關(guān)于Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程,需要的朋友可以參考下2023-05-05
搭建vue3項(xiàng)目以及按需引入element-ui框架組件全過程
element是基于vue.js框架開發(fā)的快速搭建前端的UI框架,下面這篇文章主要給大家介紹了關(guān)于搭建vue3項(xiàng)目以及按需引入element-ui框架組件的相關(guān)資料,文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
vue中this.$http.post()跨域和請(qǐng)求參數(shù)丟失的解決
這篇文章主要介紹了vue中this.$http.post()跨域和請(qǐng)求參數(shù)丟失的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
在vue項(xiàng)目實(shí)現(xiàn)一個(gè)ctrl+f的搜索功能
剛剛接到領(lǐng)導(dǎo)通知,需要實(shí)現(xiàn)搜索功能,因?yàn)轫?xiàng)目是vue的而且是手機(jī)端,對(duì)我來說有點(diǎn)小難度。經(jīng)過小編的一番思索最終還是解決了,今天小編把實(shí)現(xiàn)過程分享到腳本之家平臺(tái),需要的朋友參考下2020-02-02
Vue實(shí)現(xiàn)contenteditable元素雙向綁定的方法詳解
contenteditable是所有HTML元素都有的枚舉屬性,表示元素是否可以被用戶編輯。本文將詳細(xì)介紹如何實(shí)現(xiàn)contenteditable元素的雙向綁定,需要的可以參考一下2022-05-05

