詳解vue 配合vue-resource調(diào)用接口獲取數(shù)據(jù)
1.先用node+express+mysql簡單配置一下后臺(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ù)庫獲取的數(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ù)來渲染頁面(vue + vue-resource)
===》 js
// 引入 vue
<script src="http://cdn.bootcss.com/vue/2.1.0/vue.js" type="text/javascript" charset="utf-8"></script>
// 引入 vue-resource <script src="http://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}}的方式渲染出來
<h2>{{value.title}} <span>{{value.name}}</span></h2>
<p>{{value.content}}</p>
</div>
</div>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli3使用postcss-plugin-px2rem方式
這篇文章主要介紹了vue-cli3使用postcss-plugin-px2rem方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue+element-ui?校驗(yàn)開始時(shí)間與結(jié)束時(shí)間的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue+element-ui?校驗(yàn)開始時(shí)間與結(jié)束時(shí)間的代碼實(shí)現(xiàn),最主要的需求是開始時(shí)間不能早于當(dāng)前時(shí)間,感興趣的朋友跟隨小編一起看看吧2024-07-07
el-select如何獲取下拉框選中l(wèi)abel和value的值
在開發(fā)業(yè)務(wù)場(chǎng)景中我們通常遇到一些奇怪的需求,例如el-select業(yè)務(wù)場(chǎng)景需要同時(shí)獲取我們選中的label跟 value,下面這篇文章主要給大家介紹了關(guān)于el-select如何獲取下拉框選中l(wèi)abel和value的值,需要的朋友可以參考下2022-10-10
Vue 3 中使用 Element Plus 的 `el-t
在 Vue 3 中使用 Element Plus 的 `el-table` 組件實(shí)現(xiàn)自適應(yīng)高度,你可以根據(jù)容器的高度動(dòng)態(tài)設(shè)置表格的高度,下面通過示例代碼給大家展示,感興趣的朋友一起看看吧2024-12-12
Vue中@click.stop與@click.prevent、@click.native使用
這篇文章主要介紹了Vue中@click.stop與@click.prevent、@click.native使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
el-table-column疊加el-popover使用示例小結(jié)
el-table-column有一列展示多個(gè)tag信息,實(shí)現(xiàn)點(diǎn)擊tag展示tag信息以及tag對(duì)應(yīng)的詳細(xì)信息,本文通過示例代碼介紹el-table-column疊加el-popover使用示例小結(jié),感興趣的朋友跟隨小編一起看看吧2024-04-04
探索Vue中組合式API和選項(xiàng)式API的用法與比較
Vue3為我們開發(fā)提供了兩種組件邏輯實(shí)現(xiàn)方式:選項(xiàng)式API和組合式API,本文將嘗試為大家分析什么是選項(xiàng)式API和組合式API,以及兩種API的優(yōu)缺點(diǎn),希望對(duì)大家有所幫助2023-12-12

