使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼
剛接觸vue.js框架的時(shí)候,很傷腦筋。今天整理一下post/get兩種方式,簡(jiǎn)單的調(diào)取數(shù)據(jù)庫(kù)數(shù)據(jù),并進(jìn)行渲染,希望幫助大家!
首先,在HTML頁(yè)面引入:
//引入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è)試打印,無(wú)誤后一定要?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í)例 - 菜鳥(niǎo)教程(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è)試好了,可以使用,千萬(wàn)別搞錯(cuò)id哦。
以上這篇使用Vue調(diào)取接口,并渲染數(shù)據(jù)的示例代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在vue中獲取token,并將token寫進(jìn)header的方法
今天小編就為大家分享一篇在vue中獲取token,并將token寫進(jìn)header的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue中ref引用操作DOM元素的實(shí)現(xiàn)
本文主要介紹了vue中ref引用操作DOM元素的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
vue+echarts5實(shí)現(xiàn)中國(guó)地圖的示例代碼
本文主要介紹了vue+echarts5實(shí)現(xiàn)中國(guó)地圖的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
vue+Element-ui實(shí)現(xiàn)分頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了vue+Element-ui實(shí)現(xiàn)分頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
elementUI vue this.$confirm 和el-dialog 彈出框 移動(dòng) 示例demo
這篇文章主要介紹了elementUI vue this.$confirm 和el-dialog 彈出框 移動(dòng) 示例demo,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vue實(shí)現(xiàn)在v-html的html字符串中綁定事件
今天小編就為大家分享一篇vue實(shí)現(xiàn)在v-html的html字符串中綁定事件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
vue-resource post數(shù)據(jù)時(shí)碰到Django csrf問(wèn)題的解決
這篇文章主要介紹了vue-resource post數(shù)據(jù)時(shí)碰到Django csrf問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

