您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么在Vue項目中使用mock.js”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么在Vue項目中使用mock.js”吧!
開發工具選擇:Vscode
npm i element-ui -S
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';//樣式文件一定要引入
Vue.use(ElementUI)
<template>
<div>
<el-table
:data="tableData"
>
<el-table-column
label="日期"
width="180">
<template slot-scope="scope">
<i class="el-icon-time"></i>
<span >{{ scope.row.date }}</span>
</template>
</el-table-column>
<el-table-column
label="姓名"
width="180">
<template slot-scope="scope">
<el-popover trigger="hover" placement="top">
<p>姓名: {{ scope.row.name }}</p>
<p>住址: {{ scope.row.address }}</p>
<div slot="reference" class="name-wrapper">
<el-tag size="medium">{{ scope.row.name }}</el-tag>
</div>
</el-popover>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button
size="mini"
@click="handleEdit(scope.$index, scope.row)">編輯</el-button>
<el-button
size="mini"
type="danger"
@click="handleDelete(scope.$index, scope.row)">刪除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀區金沙江路 1518 弄'
}, {
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀區金沙江路 1517 弄'
}, {
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀區金沙江路 1519 弄'
}, {
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀區金沙江路 1516 弄'
}]
}
},
methods: {
handleEdit(index, row) {
console.log(index, row);
},
handleDelete(index, row) {
console.log(index, row);
}
}
}
</script>
import Vue from 'vue'
import VueRouter from 'vue-router'
//導入組件
import List from '../views/main/List.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'List',
component: List
},
]
const router = new VueRouter({
routes
})
export default router
npm install --save-dev mockjs
npm install --save axios
12
request.js
import axios from 'axios'
const service = axios.create({
baseURL : "http://localhost:8080",
})
export default service
12345
getData.js
import axios from '../request'
//數據列表接口
export const getList = () => axios.get("/list")
123
import Mock from 'mockjs'
//import data from './data.json'
Mock.mock('http://localhost:8080/list', {
code: 0, data:
{
'data|1000': [
{
id: '@id',//隨機id
name: '@name',//隨機名稱
nickName: '@last',//隨機昵稱
phone: /^1[34578]\d{9}$/,//隨機電話號碼
'age|11-99': 1,//年齡
address: '@county(true)',//隨機地址
email: '@email',//隨機郵箱
isMale: '@boolean',//隨機性別
createTime: '@datetime',//創建時間
avatar() {
//用戶頭像
return Mock.Random.image('100×100', Mock.Random.color(), '#757575', 'png', this.nickName)
}
}
]
}
})
import './mock/mockServer'
<template>
<div>
<el-table :data="tableData" >
<el-table-column label="隨機ID" width="200">
<template slot-scope="scope">
<i class="el-icon-time"></i>
<span >{{ scope.row.id }}</span>
</template>
</el-table-column>
<el-table-column label="姓名" width="180">
<template slot-scope="scope">
<el-popover trigger="hover" placement="top">
<p>姓名: {{ scope.row.name }}</p>
<p>住址: {{ scope.row.address }}</p>
<div slot="reference" class="name-wrapper">
<el-tag size="medium">{{ scope.row.name }}</el-tag>
</div>
<p>郵箱: {{ scope.row.email }}</p>
<p>性別: {{ scope.row.isMale }}</p>
<p>昵稱: {{ scope.row.nickName }}</p>
<p>手機號: {{ scope.row.phone }}</p>
<p>頭像:</p>
</el-popover>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
>編輯</el-button
>
<el-button
size="mini"
type="danger"
@click="handleDelete(scope.$index, scope.row)"
>刪除</el-button
>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import { getList } from "../../api/getData";
export default {
data() {
return {
tableData: [
// {
// date: "2016-05-02",
// name: "王小虎",
// address: "上海市普陀區金沙江路 1518 弄",
// },
// {
// date: "2016-05-04",
// name: "王小虎",
// address: "上海市普陀區金沙江路 1517 弄",
// },
// {
// date: "2016-05-01",
// name: "王小虎",
// address: "上海市普陀區金沙江路 1519 弄",
// },
// {
// date: "2016-05-03",
// name: "王小虎",
// address: "上海市普陀區金沙江路 1516 弄",
// },
],
};
},
methods: {
handleEdit(index, row) {
console.log(index, row);
},
handleDelete(index, row) {
console.log(index, row);
},
async getMockList() {
try {
const result = await getList();
//console.log(result);
if (result.data.code == 0) {
this.tableData = result.data.data.data;
}
//console.log(result.data.data.data);
} catch (error) {
console.log(error);
}
},
},
mounted() {
this.getMockList();
},
};
</script>
感謝各位的閱讀,以上就是“怎么在Vue項目中使用mock.js”的內容了,經過本文的學習后,相信大家對怎么在Vue項目中使用mock.js這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。