這篇文章給大家介紹如何在vue中使用vue-resource調(diào)用接口獲取數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)扎賚諾爾免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
1.先用node+express+MySQL簡(jiǎn)單配置一下后臺(tái)
const express = require('express');
const mysql = require('mysql');
const static = require('express-static');
const db = mysql.createPool({
host: 'localhost',
user: 'nodejs',
password: 'nodejs',
database:'resume',
port: 3306
});
var app = express();
// ====》設(shè)置了一個(gè) /resume 的接口,并將從數(shù)據(jù)庫(kù)獲取的數(shù)據(jù)data,send到前臺(tái)(接口名字隨便取的)
app.use('/resume', (req, res)=>{
db.query(`SELECT * FROM about_table`, (err, data)=>{
"use strict";
if(err){
res.status(500).send('databases error').end();
}else{
res.send(data).end();
}
})
})
app.listen(8080);
app.use(static('./static/'));2. 前臺(tái)請(qǐng)求接口,調(diào)用數(shù)據(jù)來渲染頁(yè)面(vue + vue-resource)
===》 js
// 引入 vue
<script src="//cdn.bootcss.com/vue/2.1.0/vue.js" type="text/javascript" charset="utf-8"></script>
// 引入 vue-resource <script src="//cdn.bootcss.com/vue-resource/1.0.3/vue-resource.js" type="text/javascript" charset="utf-8"></script>
<script>
window.onload = function () {
new Vue({
el: '#demo',
data:{
aboutData:[] //建一個(gè)空數(shù)組,用來保存調(diào)用接口獲取的數(shù)據(jù)
},
created: function () {
this.getRoute()
},
methods: {
getRoute: function () {
var that = this;
that.$http({
method: 'GET',
url: '/resume' //這里填寫剛剛后臺(tái)設(shè)置的接口
}).then(function(response){
this.aboutData = response.data; // promise的then成功之后,將response返回的數(shù)據(jù)data,保存到aboutData數(shù)組里
},function (error) {
console.log(error);
})
}
}
})
}
</script>===》 html
<div id="demo">
<div class="item" v-for="value in aboutData"> // v-for 遍歷數(shù)組后,即可將數(shù)據(jù)以{{value.xxx}}的方式渲染出來
<h3>{{value.title}} <span>{{value.name}}</span></h3>
<p>{{value.content}}</p>
</div>
</div>關(guān)于如何在vue中使用vue-resource調(diào)用接口獲取數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)題目:如何在vue中使用vue-resource調(diào)用接口獲取數(shù)據(jù)
文章源于:http://chinadenli.net/article12/jiggdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、微信公眾號(hào)、網(wǎng)站排名、虛擬主機(jī)、網(wǎng)站改版、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)