vue
+ vue-router
+ vuex
实现电商网站javascript
git clone https://github.com/chenchangyuan/shopping.git
npm install
npm run dev
运行环境: node v9.11.1 npm 5.6.0
product.js
存放商品数据(生产环境需经过接口调用获取数据){ id: 1, name: 'AirPods', brand: 'Apple', image: '/src/images/airPods.jpg', imageDetail: '/src/images/airPods_detail.jpg', sales: 10000, cost: 1288, color: '白色' },
window.localStorage
实现数据存储与验证let username = window.localStorage.getItem('username'); let password = window.localStorage.getItem('password'); if(!util.trim(this.username) || !util.trim(this.username) ){ window.alert('帐号或密码不能为空'); return; } if(username === this.username && password === this.password){ this.login = false; window.localStorage.setItem('loginStatus', 'login'); this.$store.commit('getUser', this.username); window.alert('登录成功,肯定进入网站首页'); window.location.href = '/list'; }else{ window.alert('帐号或密码错误'); }
数据过滤与排序处理html
filteredAndOrderedList(){ //拷贝原数组 let list = [...this.list]; //品牌过滤 if(this.filterBrand !== ''){ list = list.filter(item => item.brand === this.filterBrand); } //颜色过滤 if(this.filterColor !== ''){ list = list.filter(item => item.color === this.filterColor); } //排序 if(this.order !== ''){ if(this.order === 'sales'){ list = list.sort((a, b) => b.sales - a.sales); }else if(this.order === 'cost-desc'){ list = list.sort((a, b) => b.cost - a.cost); }else if(this.order === 'cost-asc'){ list = list.sort((a, b) => a.cost - b.cost); } } return list; }
实时显示应付总额与商品数vue
//购物车商品总数 countAll(){ let count = 0; this.cartList.forEach(item => { count += item.count; }); return count; }, //购物车商品总价 costAll(){ let cost = 0; this.cartList.forEach(item => { cost += this.productDictList[item.id].cost * item.count; }); return cost; }
购物车结算处理java
//通知Vuex,完成下单 handleOrder(){ this.$store.dispatch('buy').then(() => { window.alert('购买成功'); }) },
vue-router路由管理/src/views/
目录下的vue
组件进行设置,router-views
挂载全部路由,登陆界面与商品列表页面之间header作隐藏显示处理,登陆状态下刷新页面跳转至列表页,其余页面设置默认跳转node
跳转处理git
const router = new VueRouter(RouterConfig); //跳转前设置title router.beforeEach((to, from, next) => { window.document.title = to.meta.title; next(); }); //跳转后设置scroll为原点 router.afterEach((to, from, next) => { window.scrollTo(0, 0); });
routers配置github
//商品列表路由配置 const routers = [ { path: '/list', meta: { title: '商品列表' }, component: (resolve) => require(['./views/list.vue'], resolve) }, { path: '/product/:id', meta: { title: '商品详情' }, component: (resolve) => require(['./views/product.vue'], resolve) }, { path: '/cart', meta: { title: '购物车' }, component: (resolve) => require(['./views/cart.vue'], resolve) }, { path: '/login/:loginStatus', meta: { title: '登陆注册' }, component: (resolve) => require(['./views/login.vue'], resolve) }, { path: '*', redirect: '/login/login' } ]; export default routers;
vuex状态管理,各组件共享数据在state
中设置,mutation
实现数据同步,action
异步加载ajax
//配置Vuex状态管理 const store = new Vuex.Store({ state: { //商品列表信息 productList: [], //购物车数据,数组形式,数据元素为对象(商品id,购买数量count) cartList: [], //当前用户帐号 username: window.localStorage.getItem('username'), //登陆状态 loginStatus: !!window.localStorage.getItem('loginStatus'), }, getters: { //品牌、颜色筛选 brands: state => { const brands = state.productList.map(item => item.brand); return util.getFilterArray(brands); }, colors: state => { const colors = state.productList.map(item => item.color); return util.getFilterArray(colors); } }, //mutations只能以同步方式 mutations: { //添加商品列表 setProductList(state, data){ state.productList = data; }, //添加购物车 addCart(state, id){ const isAdded = state.cartList.find(item => item.id === id); //若是不存在设置购物车为1,存在count++ if(isAdded){ isAdded.count++; }else{ state.cartList.push({ id: id, count: 1 }) } }, //修改购物车商品数量 editCartCount(state, payload){ const product = state.cartList.find(item => item.id === payload.id); product.count += payload.count; }, //删除购物车商品 deleteCart(state, id){ const index = state.cartList.findIndex(item => item.id === id); state.cartList.splice(index, 1) }, //清空购物车 emptyCart(state){ state.cartList = []; }, getUser(state, username){ console.log('username',username) state.username = username; }, getLoginStatus(state, flag){ state.loginStatus = flag; } }, actions: { //异步请求商品列表,暂且使用setTimeout getProductList(context){ setTimeout(() => { context.commit('setProductList', product_data) }, 500); }, //购买 buy(context){ //生产环境使用ajax请求服务端响应后再清空购物车 return new Promise(resolve => { setTimeout(() => { context.commit('emptyCart'); resolve(); }, 500); }); }, } });
项目地址: 阅读完本文若是对vue的理解有所帮助,请给颗star,谢谢~vue-router
笔者我的微信 gm4118679254
欢迎加好友一块儿交流技术vuex