前言:学习Vue.js高仿饿了么课程过程当中,总结了这个Web App项目从准备到开发完毕本身以为很重要的知识点。这一篇主要介绍:项目准备、页面骨架开发、header组件开发。javascript
项目github地址:https://github.com/66Web/ljq_eleme,欢迎Star。css
![]() |
![]() |
App | header |
1、项目分析&学习目标 |
当前最火的MVVM框架html
高仿上线外卖App标准来开发前端
开发一个webApp的全流程vue
以线上生产环境的代码质量做标准html5
功能技术分析java
学习目标node
学习内容react
2、Vue.js介绍 |
近年来前端开发趋势webpack
(前者传统MVC:更新数据会刷新页面 后者前端MV*: 向后端REST API异步请求数据,局部刷新页面)
MV* —— MVC、MVP、MVVM
MVVM框架
View ViewModel Model
视图 通信 数据
对比Anglar React
vue.js 核心思想
组件设计原则
3、Vue-cli开启Vue项目 |
Vue-cli 是Vue的脚手架工具 —— 帮助写好Vue基础代码的工具
安装使用
(sudo) npm install -g vue-cli // sudo:mac环境下有关管理权限的命令
vue init webpack my-project
项目文件
→ scripts: 配置一些须要执行的命令
→ dependencies:开发环境中的依赖
→ devdependencies: 编译过程当中的依赖
项目运行
npm run dev
【小知识点】sublime自动格式化 —— Command+option+L 或 Control+alt+L |
import Hello from './compoments/Hello'
export default{ components: { Hello //es6语法 至关于 'Hello': Hello
} }
<hello><hello>
开发时的Webpack配置与编译
→ 配置默认识别的路径
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
}
4、准备工做 |
图标字体制做
项目目录设计
【css的stylus语法】
npm install stylus stylus-loader --save-dev
|
<link rel="stylesheet" type="text/css" href="static/css/reset.css">
先后端分离
{ "seller":{} //商家相关字段
"goods":{} //商品相关字段
"rattings":{} //评论相关字段
}
webpack.dev.conf.js中配置
const express = require('express')//开启一个node server
const app = express() //定义一个对象,包含express返回的数据
var appData = require('../data.json') //定义一个对象引入data数据
var seller = appData.seller; var goods = appData.goods; var ratings = appData.ratings; app.use('/api', apiRoutes); //调用app对象
before(app) { app.get('/api/seller', (req, res) => { res.json({ errno: 0, //错误码:其实是业务方根据业务本身定的
data: seller }) //接口返回json数据,上面配置的数据seller就赋值给data请求后调用
}), app.get('/api/goods', (req, res) => { res.json({ errno: 0, data: goods }) }), app.get('/api/ratings', (req, res) => { res.json({ errno: 0, data: ratings }) }) }
查看json数据
【Google安装第三方插件】
—— 转载自【小白白打酱油博客】 |
![]() |
![]() |
5、页面骨架开发 |
移动端视口
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
App.vue 中把页面拆为三个区块
<div id="app">
<div class="header">header</div>
<div class="tab">tab</div>
<div class="content">content</div>
</div>
而后,分别抽成一个组件,引用 —— 全部组件自定义标签名不可与html自己标签重合 'v-header': header
移动端经典布局 flex
<div class="tab"> <div class="tab-item">商品</div> <div class="tab-item">评论</div> <div class="tab-item">商家</div> </div>
.tab
display: flex
width: 100%
height: 40px
line-height: 40px
.tab-item
flex:1
text-align: center
vueRouter
<router-link :to="{ path: '/goods' }">商品</router-link>
<router-view></router-view>
main.js 中设置单页面应用路由的【挂载组件】—— 默认App.vue 也能够自定义组件如layout.vue
/* eslint-disable no-new */
new Vue({ el: '#app', router, components: { App }, template: '<App/>' })
配置【路由map】:router->index.js
export default new Router({ mode: 'history', routes: [ { path: '/', redirect: '/goods',//默认页面重定向
}, { path: '/goods', component: goods }, { path: '/ratings', component: ratings }, ] })
#app .tab .tab-item>a{ display: block; font-size: 14px; color: rgb(77, 85, 93);
} #app .tab .tab-item>a.router-link-active{ color: rgb(240, 20, 20) }
1像素border实现
.tab{ border-bottom: 1px solid rgba(7,17,27,0.1) }
【PC开发中用手机实时预览的小技巧】
|
<div class="tab border-1px">
border-1px($color)
position: relative
&:before
display: block
position: absolute
left:0
top: 0
width: 100%
border-bottom: 1px solid $color
content: ''
&:after
display: block
position: absolute
left:0
bottom: 0
width: 100%
border-top: 1px solid $color
content: ''
@media(-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5)//DPR为1.5的缩放0.7倍
.border-1px
&:before
-webkit-transform: scaleY(0.7)
transform:scaleY(0.7)
&:after
-webkit-transform: scaleY(0.7)
transform:scaleY(0.7)
@media(-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2)//DPR为2的缩放0.5倍
.border-1px
&:before
-webkit-transform: scaleY(0.5)
transform:scaleY(0.5)
&:after
-webkit-transform: scaleY(0.5)
transform:scaleY(0.5)
@import"./mixin" //import后无空格
@import"./icon"
@import"./base"
main.js: import '@/common/stylus/index.styl' //import后有空格
6、header组件开发 |
vue-resource
npm install vue-resource --save
注意:每次install完插件等以后须要从新启动项目
import VueResource from 'vue-resource' Vue.use(VueResource)
以后就能够在项目任何地方:使用 this.$http 命令
const ERR_OK = 0; //定义常量,加强程序可读性
data() { return { seller:{} //维护数据 seller
} }
created: function () { this.$http.get('/api/seller') //发送get请求,
.then(function(res){ //.then方法 请求完成后调用
//第一个函数是请求成功后方法
}, function (err) { //第二个函数是请求失败后方法
}) }
使用ES6 箭头函数:箭头函数先后必须有空格
created: function () { this.$http.get('/api/seller') .then((res) => { res = res.body //拿到response返回的promise对象的body(Data Object)
if (res.errno === ERR_OK) { this.seller = res.data; //console.log(this.seller)
} }, (err) => { }) }
外部组件
<v-header :seller="seller"></v-header>
props: {
seller: {
type: Object
}
}
:src="seller.avatar"
{{seller.name}}
<div class="support" v-if="seller.supports">
<span class="icon" :class="this.classMap[seller.supports[0].type]"></span>
<span class="text">{{seller.supports[0].description}}</span>
</div>
created (){ this.classMap = ['decrease','descount','guarantee','invoice','special'] }
<span class="icon" :class="this.classMap[seller.supports[0].type]"></span>
bg-image($url)
background-image: url($url+"@2x.png")
@media (-webkit-min-device-pixel-ratio: 3),(min-device-pixel-ratio: 3)
background-image: url($url+"@3x.png")
white-space: nowrap
overflow: hidden
text-overflow: ellipsis
.background position: absolute top: 0 left: 0 width: 100% height: 100% z-index: -1 filter: blur(10px)
详情弹层页
<div class="detail" v-show="detailShow"></div>
data () { return { detailShow: false //经过改变数据detailShow 的true/false,控制元素的显示/隐藏
} }
<div class="bulletin-wrapper" @click="showDetail>
methods: { showDetail () { this.detailShow = true; } }
【Css Sticky footers布局】
|
Star组件抽象
<div class="star" :class="starType">
<span v-for="itemClass in itemClasses" :key="itemClass.value" :class="itemClass" class="star-item">
</span>
</div>
props:{
size: {
type: Number
},
score: {
type: Number
}
}
@import "../../common/stylus/mixin"
.star
.star-item
display: inline-block
background-repeat: no-repeat
&.star-48
.star-item
width: 20px
height: 20px
margin-right: 22px
background-size: 20px 20px
&:last-child
margin-right: 0
&.on
bg-image('star48_on')
&.half
bg-image('star48_half')
&.off
bg-image('star48_off')
&.star-36
.star-item
width: 15px
height: 15px
margin-right: 16px
background-size: 15px 15px
&:last-child
margin-right: 0
&.on
bg-image('star36_on')
&.half
bg-image('star36_half')
&.off
bg-image('star36_off')
&.star-24
.star-item
width: 10px
height: 10px
margin-right: 3px
background-size: 10px 10px
&:last-child
margin-right: 0
&.on
bg-image('star24_on')
&.half
bg-image('star24_half')
&.off
bg-image('star24_off')
const LENGTH = 5; const CLS_ON = 'on'; const CLS_HALF = 'half'; const CLS_OFF = 'off'; computed: { starType() { return 'star-' + this.size; //根据size 计算出动态的class
}, itemClasses() { let result = []; let score = Math.floor(this.score*2)/2;
let hasDecimal = score % 1 !== 0; let integar = Math.floor(score); for(let i=0; i<integar; i++){ result.push(CLS_ON) //根据score 在itemClasses中push进对应个数的全亮星星class
} if(hasDecimal) { result.push(CLS_HALF);//判断若是有半分或不足5分的,push进半星class和灰色星class
} while (result.length < LENGTH) { result.push(CLS_OFF) } return result; //根据itemClasses中对应的class显示对应的星星图片
} }
小标题自适应线
<div class="title">
<div class="line"></div>
<div class="text">优惠信息</div>
<div class="line"></div>
</div>
.title
display: flex
width: 80%
margin: 30px auto 24px auto
.line
flex: 1
position: relative
top: -6px
border-bottom: 1px solid rgba(255, 255, 255, 0.2)
.text
padding: 0 12px
font-size: 14px
【Postcss工具】
|
过渡动画组件 transition
<transition name="fade">
<div class="detail">
</transition>
.detail
opacity: 1
background: rgba(7, 17, 27, 0.8)
&.fade-enter-active, &.fade-leave-active
transition: all 0.5s ease
&.fade-enter, &.fade-leave-active
opacity: 0
background: rgba(7, 17, 27, 0)
iPhone手机背景模糊效果
backdrop-filter: blur(10px) // PC端和其它手机看不出效果
注:项目来自慕课网