技術(shù)棧
Vue.js 2 + Webpack + Element + Axios + vueRouter.
效果圖


圖標(biāo) Iconfont

在項(xiàng)目開發(fā)過程中經(jīng)常會(huì)遇到小圖標(biāo)的使用問題,小圖標(biāo)的使用可以讓程序更美觀。在這里推薦使用 IconFont,這是阿里巴巴的矢量圖標(biāo)庫。
在 Iconfont 中有很多圖標(biāo),我們可以像逛超市一樣,挑選自己喜歡的商品,然后放入購物車。
使用步驟如下:
第一步:拷貝項(xiàng)目下面生成的 fontclass 代碼
//at.alicdn.com/t/font_6fwsxglzew154s4i.css
第二步:挑選相應(yīng)圖標(biāo)并獲取類名,應(yīng)用于頁面
<i class="iconfont icon-hanbao"></i>
側(cè)邊欄導(dǎo)航組件
LeftNav.vue
<template>
<div class="left-nav">
<ul>
<li>
<i class="iconfont icon-wodezichan"></i>
<div>收銀</div>
</li>
<li>
<i class="iconfont icon-dianpu"></i>
<div>店鋪</div>
</li>
<li>
<i class="iconfont icon-hanbao"></i>
<div>商品</div>
</li>
<li>
<i class="iconfont icon-31huiyuan"></i>
<div>會(huì)員</div>
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'LeftNav'
}
</script>
<style>
.left-nav {
color: #fff;
font-size: 10px;
background-color: #1D8ce0;
float: left;
height: 100%;
width: 5%;
}
.iconfont {
font-size: 24px;
}
.left-nav ul {
padding: 0px;
margin: 0px;
}
.left-nav li {
list-style: none;
text-align: center;
border-bottom: 1px solid #20a0ff;
padding: 10px;
}
</style>
使用 Element 布局
- npm 安裝
npm install element-ui --save
- 引入項(xiàng)目
在main.js中加入以下內(nèi)容:
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
Vue.use(ElementUI)
右側(cè)內(nèi)容組件
Main.vue
<template>
<div class="content">
<el-row class="row">
<el-col :span="9" class="col1">
<el-tabs>
<el-tab-pane label="點(diǎn)餐">
<el-table :data="goods" style="width: 100%" stripe>
<el-table-column prop="goodsName" label="商品名稱">
</el-table-column>
<el-table-column prop="count" label="數(shù)量" width="70">
</el-table-column>
<el-table-column prop="price" label="金額" width="70">
</el-table-column>
<el-table-column label="操作" width="100" fixed="right">
<template scope="scope">
<el-button type="text" size="small" @click="del(scope.row)">刪除</el-button>
<el-button type="text" size="small" @click="add(scope.row)">增加</el-button>
</template>
</el-table-column>
</el-table>
<div class="summarize">
<small>數(shù)量:</small>{{totalCount}}
<small>金額:</small>{{totalMoney}}元
</div>
<div class="block">
<span class="wrapper">
<el-button type="warning">掛單</el-button>
<el-button type="danger" @click="goods=[]">刪除</el-button>
<el-button type="info" @click="checkout">結(jié)賬</el-button>
</span>
</div>
</el-tab-pane>
<el-tab-pane label="掛單">掛單</el-tab-pane>
<el-tab-pane label="外賣">外賣</el-tab-pane>
</el-tabs>
</el-col>
<el-col :span="15" class="col2">
<div class="common-goods">
<div class="title">常用商品</div>
<div class="goods-list">
<ul>
<li v-for="good in commonGoods" @click="add(good)">
<span v-text="good.goodsName"></span>
<span class="good-price">¥{{good.price}}元</span>
</li>
</ul>
</div>
</div>
<div class="category">
<el-tabs>
<el-tab-pane label="漢堡">
<ul class='type-list'>
<li v-for="hanbao in hanbaoGoods" @click="add(hanbao)">
<span class="img">

</span>
<span class="name">{{hanbao.goodsName}}</span>
<span class="price">¥{{hanbao.price}}元</span>
</li>
</ul>
</el-tab-pane>
<el-tab-pane label="小食">
<ul class='type-list'>
<li v-for="xiaoshi in xiaoshiGoods" @click="add(xiaoshi)">
<span class="img">

</span>
<span class="name">{{xiaoshi.goodsName}}</span>
<span class="price">¥{{xiaoshi.price}}元</span>
</li>
</ul>
</el-tab-pane>
<el-tab-pane label="飲料">
<ul class='type-list'>
<li v-for="yinliao in yinliaoGoods" @click="add(yinliao)">
<span class="img">

</span>
<span class="name">{{yinliao.goodsName}}</span>
<span class="price">¥{{yinliao.price}}元</span>
</li>
</ul>
</el-tab-pane>
<el-tab-pane label="套餐">
<ul class='type-list'>
<li v-for="taocan in taocanGoods" @click="add(taocan)">
<span class="img">

</span>
<span class="name">{{taocan.goodsName}}</span>
<span class="price">¥{{taocan.price}}元</span>
</li>
</ul>
</el-tab-pane>
</el-tabs>
</div>
</el-col>
</el-row>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'main',
data() {
return {
goods: [],
commonGoods: [],
hanbaoGoods: [],
xiaoshiGoods: [],
yinliaoGoods: [],
taocanGoods: []
}
},
methods: {
add(good) {
let data = this.goods.filter(g => g.goodsId == good.goodsId)
if (data.length > 0) {
data[0].count++
} else {
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
let newGood = Object.assign({}, good)
newGood.count = 1
this.goods.push(newGood)
}
},
del(good) {
this.goods = this.goods.filter(g => g.goodsId != good.goodsId)
},
checkout() {
if (this.goods.length != 0) {
this.goods = []
this.totalCount = 0
this.totalMoney = 0
this.$message({
message: '結(jié)賬成功^_^',
type: 'success'
});
} else {
this.$message.error('不能空結(jié)⊙o⊙');
}
}
},
computed: {
totalCount() {
let count = 0
this.goods.forEach(function (element) {
count += element.count
}, this);
return count
},
totalMoney() {
let money = 0
this.goods.forEach(function (element) {
money += element.count * element.price
}, this);
return money
}
},
created() {
// 讀取常用商品列表
axios.get('http://jspang.com/DemoApi/oftenGoods.php').then(response => {
this.commonGoods = response.data
}).catch(error => {
console.log(error)
});
// 讀取分類商品列表
axios.get('http://jspang.com/DemoApi/typeGoods.php').then(response => {
this.hanbaoGoods = response.data[0]
this.xiaoshiGoods = response.data[1]
this.yinliaoGoods = response.data[2]
this.taocanGoods = response.data[3]
}).catch(error => {
console.log(error)
})
}
}
</script>
<style>
.content,
.row,
.col1,
.col2,
.card {
height: 100%;
}
.col1 {
border-right: 1px solid;
}
.block {
margin-top: 20px;
text-align: center;
}
.title {
height: 42px;
line-height: 42px;
border-bottom: 1px solid #D3DCE6;
background-color: #F9FAFC;
color: #20a0ff;
padding-left: 15px;
}
.goods-list ul li {
list-style: none;
float: left;
border: 1px solid #E5E9F2;
padding: 10px;
margin: 5px;
background-color: #fff;
cursor: pointer;
}
.good-price {
color: #58B7FF;
}
.category {
clear: both;
}
.type-list li {
list-style: none;
width: 30%;
border: 1px solid #E5E9F2;
height: auot;
overflow: hidden;
background-color: #fff;
padding: 2px;
float: left;
margin: 2px;
cursor: pointer;
}
.type-list li span {
display: block;
float: left;
}
.img {
width: 40%;
}
.name {
font-size: 18px;
padding-left: 10px;
color: brown;
}
.price {
font-size: 16px;
padding-left: 10px;
padding-top: 10px;
}
.summarize {
text-align: center;
padding: 10px;
border-bottom: 1px solid #E5E9F2;
}
.summarize small {
margin-left: 40px;
}
</style>
其余代碼
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>awesomepos</title>
<link rel="stylesheet" >
<style>
html,
body {
margin: 0px;
padding: 0px;
height: 100%;
}
</style>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import router from './router'
Vue.config.productionTip = false
Vue.use(ElementUI)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
template: '<App/>',
components: {
App
}
})
App.vue
<template>
<div id="app">
<!--左側(cè)導(dǎo)航-->
<LeftNav></LeftNav>
<div class="main">
<router-view></router-view>
</div>
</div>
</template>
<script>
import LeftNav from '@/components/LeftNav'
export default {
name: 'app',
components: {
LeftNav
}
}
</script>
<style>
#app {
font-family: 'Microsoft YaHei', 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
height: 100%;
}
.main {
float: left;
width: 95%;
height: 100%;
overflow: auto;
}
</style>
index.js
import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/components/Main'
Vue.use(Router)
export default new Router({
routes: [{
path: '/',
name: 'main',
component: Main
}]
})
項(xiàng)目打包
注意事項(xiàng):
打開 config/index.js 會(huì)看到一個(gè) build 屬性,這里就是我們打包的基本配置。你在這里可以修改打包的目錄,文件名。最重要的是一定要把絕對(duì)路徑改為相對(duì)路徑。
把
assetsPublicPath: '/'
改為
assetsPublicPath: './'