Skip to content

Commit 2498ba9

Browse files
author
alistar
committed
移动端适配 路由懒加载 css书写方案
1 parent 03d02d4 commit 2498ba9

16 files changed

+772
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# 94club.github.io(移动端demo, 完全学习https://github.com/zaofeng/vue2-elm)
1+
# 94club.github.io
2+
#### 移动端demo, 完全学习https://github.com/zaofeng/vue2-elm
23

34
# 项目布局
45

src/App.vue

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ export default {
1010
name: 'App'
1111
}
1212
</script>
13-
14-
<style>
15-
#app {
16-
font-family: 'Avenir', Helvetica, Arial, sans-serif;
17-
-webkit-font-smoothing: antialiased;
18-
-moz-osx-font-smoothing: grayscale;
19-
text-align: center;
20-
color: #2c3e50;
21-
margin-top: 60px;
22-
}
13+
<style lang='scss'>
14+
@import './style/index.scss';
2315
</style>
16+

src/NOTE.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
####关于css的书写放不放在.vue文件中
2+
同意: (https://www.mmxiaowu.com/article/5a105d5e1b60f4043271122e)
3+
1. 构建速度会变慢
4+
写在组件的 style 里的样式会先经过vue-loader再走less-loader或者css-loader, 无形中增加了编译时间
5+
6+
2. 在用了 scope 属性后, 生成的 js 文件和 css 文件都会正常臃肿一些
7+
scope 属性, 其实就是在经过vue-loader编译的时候, 组件模版的对应元素和 css 样式添加一个随机的属性,
8+
这样会造成在代码中添加大量类似data-v-33053b70这样的代码上去, 无形中使打包后的文件变大, 组件越多, 越明显
9+
10+
3. scope 属性, 对当前组件不存在的元素不起作用 原因同上条, 这种情况在用了组件库的情况下, 但是需要修改组件库组件样式时, 会经常遇到
11+
12+
4. 在用了 scope 属性后, css 解析效率会变低 css 中, 属性选择器的效率是最低的, 特别是直接用元素选择器的情况下
13+
style.less 文件是入口文件, 负责引入其他各个子样式文件, 最后由 App.vue 根组件引入, home 文件夹下所有以下划线开头的文件为各个组件的样式,
14+
如一些 less 配置, 可以放在 _base.less 文件里, 布局样式可以放在 _layout.less 文件里, 其他的一个路由组件一个文件, 再配合现在编辑的分屏功能,
15+
也是一样可以很快定位到相关文件, 甚至一些编辑器, 可以直接从模版中跳到对应的样式文件
16+
17+
反对:
18+
暂无
19+
20+
####关于路由懒加载
21+
解释:当打包构建应用时,Javascript 包会变得非常大,影响页面加载。如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了
22+
1结合 Vue 的异步组件和 Webpack 的代码分割功能,轻松实现路由组件的懒加载 https://panjiachen.github.io/vue-element-admin-site/#/zh-cn/lazy-loading
23+
区分开发环境和生产环境的话 通过环境变量,分别是用require和import
24+
副作用:https://github.com/PanJiaChen/vue-element-admin/issues/292
25+
1(加强版)使用babel 的 plugins babel-plugin-dynamic-import-node
26+
2使用require.ensure 不区别开发和生产,基本被1代替 webpack 在编译时,会静态地解析代码中的 require.ensure(),同时将模块添加到一个分开的 chunk 当中
27+
这个新的 chunk 会被 webpack 通过 jsonp 来按需加载
28+
建议使用1的加强版
29+
30+
### 移动端点透事件
31+
https://segmentfault.com/a/1190000003848737
32+
https://www.jianshu.com/p/6f85e957a725
33+
http://www.cnblogs.com/yexiaochai/p/3462657.html
34+
target.addEventListener(type, listener, useCapture);
35+
useCapture 可选
36+
Boolean,是指在DOM树中,注册了该listener的元素,是否会先于它下方的任何事件目标,接收到该事件。
37+
沿着DOM树向上冒泡的事件不会触发被指定为useCapture(也就是设为true)的listener。
38+
当一个元素嵌套了另一个元素,两个元素都对同一个事件注册了一个处理函数时,所发生的事件冒泡和事件捕获是两种不同的事件传播方式。
39+
事件传播模式决定了元素以哪个顺序接收事件。进一步的解释可以查看 事件流 及 JavaScript Event order 文档。 如果没有指定, useCapture 默认为 false 。

src/commitDetail.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
## commit详情
2-
1 vue项目初始化
3-
2 css初始化 结构目录创建
2+
vue项目初始化 32b85057ab3eeaee10446d4727f165c9da1d5f46
3+
css初始化 结构目录创建 03d02d43f7825424569c1b1b638255565026ca4e
4+
安装依赖 移动端适配 路由懒加载 css书写方案

src/components/common/401.vue

Whitespace-only changes.

src/components/common/404.vue

Whitespace-only changes.

src/config/rem.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(function(doc, win) {
2+
let docEl = doc.documentElement
3+
let resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
4+
let recalc = function() {
5+
let clientWidth = docEl.clientWidth
6+
if (!clientWidth) return
7+
docEl.style.fontSize = 20 * (clientWidth / 320) + 'px'
8+
}
9+
if (!doc.addEventListener) return
10+
win.addEventListener(resizeEvt, recalc, false)
11+
doc.addEventListener('DOMContentLoaded', recalc, false)
12+
})(document, window)

src/main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import Vue from 'vue'
44
import App from './App'
55
import router from './router'
66

7-
Vue.config.productionTip = false
7+
import './config/rem'
8+
import FastClick from 'fastclick'
9+
if ('addEventListener' in document) {
10+
document.addEventListener('DOMContentLoaded', function() {
11+
FastClick.attach(document.body);
12+
}, false);
13+
}
814

15+
Vue.config.productionTip = false // 生产环境去掉vue的警告信息
916
/* eslint-disable no-new */
1017
new Vue({
1118
el: '#app',

src/router/_import_development.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = file => require('@/views/' + file + '.vue').default // vue-loader at least v13.0.0+

src/router/_import_productions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = file => () => import('@/views/' + file + '.vue')

src/router/index.js

Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
import Vue from 'vue'
2+
import Router from 'vue-router'
3+
import {routerMode} from './config/env'
4+
5+
const home = () => import('../page/home/home')
6+
const city = () => import('../page/city/city')
7+
const msite = () => import('../page/msite/msite')
8+
const search = () => import('../page/search/search')
9+
const shop = () => import('../page/shop/shop')
10+
const login = () => import('../page/login/login')
11+
const profile = () => import('../page/profile/profile')
12+
const forget = () => import('../page/forget/forget')
13+
const order = () => import('../page/order/order')
14+
const orderDetail = () => import('../page/order/children/orderDetail')
15+
const vipCard = () => import('../page/vipcard/vipCard')
16+
const invoiceRecord = () => import('../page/vipcard/children/invoiceRecord')
17+
const useCart = () => import('../page/vipcard/children/useCart')
18+
const vipDescription = () => import('../page/vipcard/children/vipDescription')
19+
const food = () => import('../page/food/food')
20+
const confirmOrder = () => import('../page/confirmOrder/confirmOrder')
21+
const remark = () => import('../page/confirmOrder/children/remark')
22+
const payment = () => import('../page/confirmOrder/children/payment')
23+
const userValidation = () => import('../page/confirmOrder/children/userValidation')
24+
const invoice = () => import('../page/confirmOrder/children/invoice')
25+
const chooseAddress = () => import('../page/confirmOrder/children/chooseAddress')
26+
const addAddress = () => import('../page/confirmOrder/children/children/addAddress')
27+
const searchAddress = () => import('../page/confirmOrder/children/children/children/searchAddress')
28+
const foodDetail = () => import('../page/shop/children/foodDetail')
29+
const shopDetail = () => import('../page/shop/children/shopDetail')
30+
const shopSafe = () => import('../page/shop/children/children/shopSafe')
31+
const info = () => import('../page/profile/children/info')
32+
const setUserName = () => import('../page/profile/children/children/setUserName')
33+
const address = () => import('../page/profile/children/children/address')
34+
const add = () => import('../page/profile/children/children/children/add')
35+
const addDetail = () => import('../page/profile/children/children/children/children/addDetail')
36+
const balance = () => import('../page/balance/balance')
37+
const balanceDetail = () => import('../page/balance/children/detail')
38+
const benefit = () => import('../page/benefit/benefit')
39+
const coupon = () => import('../page/benefit/children/coupon')
40+
const hbDescription = () => import('../page/benefit/children/hbDescription')
41+
const hbHistory = () => import('../page/benefit/children/hbHistory')
42+
const exchange = () => import('../page/benefit/children/exchange')
43+
const commend = () => import('../page/benefit/children/commend')
44+
const points = () => import('../page/points/points')
45+
const pointsDetail = () => import('../page/points/children/detail')
46+
const service = () => import('../page/service/service')
47+
const questionDetail = () => import('../page/service/children/questionDetail')
48+
const find = () => import('../page/find/find')
49+
const download = () => import('../page/download/download')
50+
const notFound = () => import('../components/common/404.vue')
51+
const notAllow = () => import('../components/common/401.vue')
52+
53+
Vue.use(Router)
54+
55+
const router = new Router ({
56+
mode: routerMode, // 默认值: "hash" (浏览器环境) | "abstract" (Node.js 环境)可选值: "hash" | "history" | "abstract"
57+
// hash: 使用 URL hash 值来作路由。支持所有浏览器,包括不支持 HTML5 History Api 的浏览器
58+
// history: 依赖 HTML5 History API 和服务器配置。查看 HTML5 History 模式
59+
// abstract: 支持所有 JavaScript 运行环境,如 Node.js 服务器端。如果发现没有浏览器的 API,路由会自动强制进入这个模式
60+
scrollBehavior (to, from, savedPosition) {
61+
if (savedPosition) {
62+
return savedPosition
63+
} else {
64+
if (from.meta.keepAlive) {
65+
from.meta.savedPosition = document.body.scrollTop
66+
}
67+
return { x: 0, y: to.meta.savedPosition || 0
68+
}
69+
}
70+
},
71+
routes: [{
72+
path: '/',
73+
component: App, // 顶层路由,对应index.html
74+
children: [ // 二级路由。对应App.vue
75+
// 地址为空时跳转home页面
76+
{
77+
path: '',
78+
redirect: '/home'
79+
},
80+
// 首页城市列表页
81+
{
82+
path: '/home',
83+
component: home
84+
},
85+
// 当前选择城市页
86+
{
87+
path: '/city/:cityid',
88+
component: city
89+
},
90+
// 所有商铺列表页
91+
{
92+
path: '/msite',
93+
component: msite,
94+
meta: { keepAlive: true }
95+
},
96+
// 特色商铺列表页
97+
{
98+
path: '/food',
99+
component: food
100+
},
101+
// 搜索页
102+
{
103+
path: '/search/:geohash',
104+
component: search
105+
},
106+
// 商铺详情页
107+
{
108+
path: '/shop',
109+
component: shop,
110+
children: [{
111+
path: 'foodDetail', // 食品详情页
112+
component: foodDetail
113+
}, {
114+
path: 'shopDetail', // 商铺详情页
115+
component: shopDetail,
116+
children: [{
117+
path: 'shopSafe', // 商铺安全认证页
118+
component: shopSafe
119+
}]
120+
}]
121+
},
122+
//确认订单页
123+
{
124+
path: '/confirmOrder',
125+
component: confirmOrder,
126+
children: [{
127+
path: 'remark', // 订单备注
128+
component: remark
129+
}, {
130+
path: 'invoice', // 发票抬头
131+
component: invoice
132+
}, {
133+
path: 'payment', // 付款页面
134+
component: payment
135+
}, {
136+
path: 'userValidation', // 用户验证
137+
component: userValidation
138+
}, {
139+
path: 'chooseAddress', // 选择地址
140+
component: chooseAddress,
141+
children: [{
142+
path: 'addAddress', // 添加地址
143+
component: addAddress,
144+
children: [{
145+
path: 'searchAddress', // 搜索地址
146+
component: searchAddress
147+
}]
148+
}]
149+
}]
150+
},
151+
// 登录注册页
152+
{
153+
path: '/login',
154+
component: login
155+
},
156+
// 个人信息页
157+
{
158+
path: '/profile',
159+
component: profile,
160+
children: [{
161+
path: 'info', // 个人信息详情页
162+
component: info,
163+
children: [{
164+
path: 'setUserName',
165+
component: setUserName
166+
}, {
167+
path: 'address',
168+
component: address, // 编辑地址
169+
children:[{
170+
path:'add',
171+
component:add,
172+
children:[{
173+
path:'addDetail',
174+
component:addDetail
175+
}]
176+
}]
177+
}]
178+
},
179+
{
180+
path: 'service', // 服务中心
181+
component: service
182+
}]
183+
},
184+
// 修改密码页
185+
{
186+
path: '/forget',
187+
component: forget
188+
},
189+
// 订单列表页
190+
{
191+
path: '/order',
192+
component: order,
193+
children: [{
194+
path: 'orderDetail', // 订单详情页
195+
component: orderDetail
196+
}]
197+
},
198+
// vip卡页
199+
{
200+
path: '/vipCard',
201+
component: vipCard,
202+
children: [{
203+
path: 'invoiceRecord', // 开发票
204+
component: invoiceRecord
205+
}, {
206+
path: 'useCart', // 购买会员卡
207+
component: useCart
208+
}, {
209+
path: 'vipDescription', // 会员说明
210+
component: vipDescription
211+
}]
212+
},
213+
// 发现页
214+
{
215+
path: '/find',
216+
component: find
217+
},
218+
// 下载页
219+
{
220+
path: '/download',
221+
component: download
222+
},
223+
// 服务中心
224+
{
225+
path: '/service',
226+
component: service,
227+
children: [{
228+
path: 'questionDetail', // 订单详情页
229+
component: questionDetail
230+
}]
231+
},
232+
// 余额
233+
{
234+
path: 'balance',
235+
component: balance,
236+
children: [{
237+
path: 'detail', // 余额说明
238+
component: balanceDetail
239+
}]
240+
},
241+
// 我的优惠页
242+
{
243+
path: 'benefit',
244+
component: benefit,
245+
children: [{
246+
path: 'coupon', // 代金券说明
247+
component: coupon
248+
}, {
249+
path: 'hbDescription', // 红包说明
250+
component: hbDescription
251+
}, {
252+
path: 'hbHistory', // 历史红包
253+
component: hbHistory
254+
}, {
255+
path: 'exchange', // 兑换红包
256+
component: exchange
257+
}, {
258+
path: 'commend', // 推荐有奖
259+
component: commend
260+
}]
261+
},
262+
// 我的积分页
263+
{
264+
path: 'points',
265+
component: points,
266+
children: [{
267+
path: 'detail', // 积分说明
268+
component: pointsDetail
269+
}]
270+
},
271+
{
272+
path: '401',
273+
component: notAllow
274+
},
275+
{
276+
path: '404',
277+
component: notFound
278+
}
279+
]
280+
}]
281+
})
282+
export default router

0 commit comments

Comments
 (0)