vue商城路由配置及组件的使用
2018-01-03 16:09
836 查看
1.vue路由的配置
1)vue项目import Vue from 'vue' //引入vue import Router from 'vue-router' //引入路由信息 Vue.use(Router) //初始化路由信息 const MemberStore = (resolve) => { import('components/memberStore/memberStore').then((module) => { resolve(module) }) } const More = (resolve) => { import('components/more/more').then((module) => { resolve(module) }) } export default new Router({ routes: [ //规定默认的路径,定义进入默认的路由页面 { path: '/', redirect: '/more' }, { path: '/memberStore', component: MemberStore }, { path: '/order', component: Order, children: [ //子路由 { path: 'allOrder', component: AllOrder }, { path: 'orderDetail', component: OrderDetail } ] }, { path:'/delivery', component:Delivery }, { path: '/rateShop', component: RateShop } ] })
注:如果使用ECMAScript6语法,webStorm会提示错误,因此需要在webStorm设置
2)路由切换的时候,导航的颜色随着路由的切换而改变;可以在less文件里,下面的导航代码
<footer class="footer"> <ul class=" ui-rows ui-rows-bottom footer-menu"> <li class="ui-cols"> <router-link to="/memberStore"> <i class="iconfont icon-huiyuantuijian"> </i> <div>优选推荐</div> </router-link> </li> <li class="ui-cols"> <router-link to="/more"> <i class="iconfont icon-gengduoshangpin"> </i> <div>更多商品</div> </router-link> </li> <li class="ui-cols" ref="shopcart"> <router-link to="/cart"> <i class="iconfont icon-gouwuche"> </i> <div>购物车</div> </router-link> </li> <li class="ui-cols"> <router-link to="/my"> <i class="iconfont icon-wode"> </i> <div>我的</div> </router-link> </li> </ul> </footer>
<router-link></router-link>标签的DOM结构如下图所示
可以看到当前的路由显示的DOM中class名为
router-link-active,导航使用的图标是iconfont,在less里添加
.router-link-active{ color:#ec6941; }
2)组件的编写和使用
在vue项目中组件的使用是必须的,可以分为基础组件(项目中经常使用的轮播组件、loading组件、地址组件等)和项目的组件(m-header组件),基础组件是开发项目经常使用的组件,在以后的项目开发中也可以使用。
import Scroll from 'base/scroll/scroll' import TopNews from 'base/top-news/top-news' import Weather from 'base/weather/weather' import nomore from 'base/nomore/nomore' export default { components:{ nomore, Scroll, TopNews, Weather } }
也可以是用一些开源的css框架如mint-ui,
链接地址http://mint-ui.github.io/#!/zh-cn,它适用于移动端组件库,其中的一些组件库下拉刷新、弹出框等
2)封装slide轮播组件
slide轮播是在BScroll组件的基础上封装的,BScroll可直接使用
npm install 安装,package.json "better-scroll": "^1.5.6",
<template> <div class="slider" ref="slide"> <div class="slider-group" ref="slideGroup"> <slot></slot> </div> </div> </template> <script type="text/ecmascript-6"> //引入基础的移动端滑动组件BScroll import BScroll from 'better-scroll' import {addClass} from 'common/js/dom' export default { name: 'slider', props: { loop: { type: Boolean, default: true }, autoPlay: { type: Boolean, default: true }, interval: { type: Number, default: 3000 }, showDot: { type: Boolean, default: false }, click: { type: Boolean, default: true } }, data() { return { dots: [], currentPageIndex: 0 } }, mounted() { this.update() window.addEventListener('resize', () => { if (!this.slide || !this.slide.enabled) { return } clearTimeout(this.resizeTimer) this.resizeTimer = setTimeout(() => { if (this.slide.isInTransition) { this._onScrollEnd() } else { if (this.autoPlay) { this._play() } } this.refresh() }, 60) }) }, activated() { if (!this.slide) { return } this.slide.enable() let pageIndex = this.slide.getCurrentPage().pageX if (pageIndex > this.dots.length) { pageIndex = pageIndex % this.dots.length } this.slide.goToPage(pageIndex, 0, 0) if (this.loop) { pageIndex -= 1 } this.currentPageIndex = pageIndex if (this.autoPlay) { this._play() } }, deactivated() { this.slide.disable() clearTimeout(this.timer) }, beforeDestroy() { this.slide.disable() clearTimeout(this.timer) }, methods: { update() { if (this.slide) { this.slide.destroy() } this.$nextTick(() => { this.init() }) }, refresh() { this._setSlideHeight(true) this.slide.refresh() }, next() { this.slide.next() }, init() { clearTimeout(this.timer) this.currentPageIndex = 0 this._setSlideWidth() if (this.showDot) { this._initDots() } this._initSlide() if (this.autoPlay) { this._play() } }, _setSlideWidth(isResize) { this.children = this.$refs.slideGroup.children let width= 0 let slideWidth = this.$refs.slide.clientWidth for (let i = 0; i < this.children.length; i++) { let child = this.children[i] addClass(child, 'slide-item') child.style.width= slideWidth + 'px' width+= slideWidth } if (this.loop && !isResize) { width+= 2 * slideWidth } this.$refs.slideGroup.style.width = width+ 'px' }, _initSlide() { //初始化BScroll this.slide = new BScroll(this.$refs.slide, { scrollX:true, scrollY: false, momentum: false, snap: { loop: this.loop, threshold: 0.3, speed: 400 }, click: this.click }) this.slide.on('scrollEnd', this._onScrollEnd) this.slide.on('touchend', () => { if (this.autoPlay) { this._play() } }) this.slide.on('beforeScrollStart', () => { if (this.autoPlay) { clearTimeout(this.timer) } }) }, _onScrollEnd() { let pageIndex = this.slide.getCurrentPage().pageX //获取当前的页数 if (this.loop) { pageIndex -= 1 } this.currentPageIndex = pageIndex if (this.autoPlay) { this._play() } }, _initDots() { this.dots = new Array(this.children.length) }, _play() { let pageIndex = this.slide.getCurrentPage().pageX + 1 if (this.loop) { pageIndex += 1 } clearTimeout(this.timer) this.timer = setTimeout(() => { this.slide.goToPage(pageIndex, 0, 400) }, this.interval) } } } </script> <style scoped lang="less" rel="stylesheet/less"> .slider{ height:0.47rem; min-height:0.01rem; .slider-group{ position:relative; overflow:hidden; white-space:nowrap; } } </style>
轮播组件是很多项目中经常会用到的,自己也可以封装上拉刷新、下拉加载等组件,这些基础的组件在以后开发项目中会经常使用。
3)下面的这个商城示例就是使用vue开发的移动端微商城
相关文章推荐
- VUE 路由及组件使用
- Vue.js路由组件vue-router使用方法详解
- Vue的路由功能(1)简单路由与vue-router的基础使用(同步/异步组件)
- 使用vue-router为每个路由配置各自的title
- Vue的路由功能(1)简单路由与vue-router的基础使用(同步/异步组件)
- Vue.js路由组件vue-router的使用方法
- Vue的路由功能(1)简单路由与vue-router的基础使用(同步/异步组件)
- vue教程3-01 路由、组件、bower包管理器使用
- Vue-路由配置和使用步骤整理
- Vue的路由功能(1)简单路由与vue-router的基础使用(同步/异步组件)
- 日志组件logback的介绍及配置使用方法(一)
- 日志组件logback的介绍及配置使用方法(二)
- 日志组件logback的介绍及配置使用方法(二)
- 日志组件logback的介绍及配置使用方法(一)
- 【小玩意儿】使用C#的“贫血模型”快速配置组件QuickConfig
- 使用iproute2配置force-onlink路由
- 使用iproute2配置force-onlink路由
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用
- SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 s
- FSO组件不可用,各种与FSO相关的功能都将出错!请运行Install.asp或者到后台网站配置处设置好FSO组件名称。或无法使用vbscript