寫在前面的話,公司工作很久了,一直都沒有改過自己的技術(shù)棧,才覺得慢慢的落后于潮流,也知道自己的技術(shù)棧很老舊,想過要重構(gòu)項(xiàng)目,但是項(xiàng)目周期時(shí)間一直不許,學(xué)習(xí)vue只能在下班的時(shí)間里面,這兩年也斷斷續(xù)續(xù)的用過一些框架,但最后還是選中了vuetify。
vuetify
推薦指數(shù):star:25.4k
適用:移動(dòng)PC多端支持
GitHub地址:https://github.com/vuetifyjs/vuetify
官網(wǎng)地址:https://vuetifyjs.com/zh-Hans/
來看看官網(wǎng)是怎么介紹的吧:這是世界上最流行的 Vue.js 框架,用于構(gòu)建功能豐富、快速的應(yīng)用程序。
Vuetify確實(shí)是一款非常精致的UI框架,它提供了很多常用的組件,依靠Material Design的設(shè)計(jì)優(yōu)勢,讓你無需編寫一行css代碼就可以得到非常美觀的界面功能。響應(yīng)式做的不錯(cuò),移動(dòng)PC多端支持,配置靈活,組件也挺多的,足夠現(xiàn)代,功能全面vuetify,一直用一直爽,強(qiáng)烈推薦vuetify。
步驟:
1:以管理員的身份打開cmd,進(jìn)入d盤
使用 Vue CLI 創(chuàng)建一個(gè)新的 Vue.js 項(xiàng)目
vue create vuetify-app

完成以后,可以看到D盤出現(xiàn)的初始化的項(xiàng)目了

2:根據(jù)提示
運(yùn)行
cd vuetify-app
npm run serve

啟動(dòng)成功

打開瀏覽器,輸入地址,可以訪問了

3:將項(xiàng)目導(dǎo)入編輯器
在編輯器里面打開終端
vue add vuetify

一路回車,以下就是安裝完成了

4:根據(jù)文檔,寫一個(gè)表格分頁
<template>
<v-data-table
:headers="headers"
:items="desserts"
:items-per-page="5"
class="elevation-1"
></v-data-table>
</template>
<script>
export default {
data () {
return {
headers: [
{
text: 'Dessert (100g serving)',
align: 'start',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
desserts: [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
iron: '1%',
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
iron: '7%',
},
{
name: 'Cupcake',
calories: 305,
fat: 3.7,
carbs: 67,
protein: 4.3,
iron: '8%',
},
{
name: 'Gingerbread',
calories: 356,
fat: 16.0,
carbs: 49,
protein: 3.9,
iron: '16%',
},
{
name: 'Jelly bean',
calories: 375,
fat: 0.0,
carbs: 94,
protein: 0.0,
iron: '0%',
},
{
name: 'Lollipop',
calories: 392,
fat: 0.2,
carbs: 98,
protein: 0,
iron: '2%',
},
{
name: 'Honeycomb',
calories: 408,
fat: 3.2,
carbs: 87,
protein: 6.5,
iron: '45%',
},
{
name: 'Donut',
calories: 452,
fat: 25.0,
carbs: 51,
protein: 4.9,
iron: '22%',
},
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
iron: '6%',
},
],
}
},
}
</script>

5:上面是一個(gè)靜態(tài)的表格,怎么寫成一個(gè)請求json數(shù)據(jù),然后渲染數(shù)據(jù)的格式呢,這里就要用到常用的axios請求方法了。
安裝axios
npm install axios --save

在public底下新建一個(gè)static靜態(tài)文件夾,存放json數(shù)據(jù)
,準(zhǔn)備json數(shù)據(jù)數(shù)據(jù)格式如下:

[{
"name": "22物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
},{
"name": "23物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
},{
"name": "24物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
},{
"name": "25物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
},{
"name": "26物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
},{
"name": "27物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
},{
"name": "28物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
},{
"name": "29物聯(lián)",
"calories": 1,
"fat": "DDDDD",
"carbs": 1,
"protein": "DDDD",
"iron": "DDDDD"
}]
代碼示例
<template>
<v-data-table :headers="headers" :items="desserts" :items-per-page="5" class="elevation-1"></v-data-table>
</template>
<script>
import axios from "axios";
export default {
data() {
return {
headers: [
{
text: "Dessert (100g serving)",
align: "start",
sortable: false,
value: "name"
},
{ text: "Calories", value: "calories" },
{ text: "Fat (g)", value: "fat" },
{ text: "Carbs (g)", value: "carbs" },
{ text: "Protein (g)", value: "protein" },
{ text: "Iron (%)", value: "iron" }
],
desserts: []
};
},
mounted() {
this.getData();
},
methods: {
getData() {
axios.get("/static/mock.json").then(
response => {
console.log(response.data);
this.desserts = response.data;
},
error => {
console.log(error);
}
);
}
}
};
</script>
效果如下

OK,完成。