1.可以通过给标签设置ref="menuWrap"属性,然后在this.$refs.menuWrap可以访问这个标签的原生DOM,
同样,给组件绑定ref=”组件名”就可以访问组件的函数,例如this.$refs.shopCart.drop(target)
2.在需要计算DOM相关的东西时,一定要保证DOM已经渲染了,其实DOM真正发生变化时是在this.$nextTick()这个回调函数后,所以在操作DOM时需在$nextTick()函数下
3. 在Vue中给予对象一个不存在的属性字段时,直接赋值不会触发视图更新
例子:
写一个点击事件的方法,方法具体内容:
if(!this.food.count){
this.food.count = 1;
}
因为count字段不在this.food(由父组件通过axios传递给子组件的数据)里,所以点击时不会触发视图更新
解决问题:
import Vue from 'vue'
if(!this.food.count){
Vue.set(this.food,'count',1)
}
参数解读:
target:要更改的数据源(可以是对象或者数组)
key:要更改的具体数据
value :重新赋的值
4.使用better-scroll时要触发点击事件一定要在初始化滚动条时加click: ture
<template>
<div class="goods">
<div class="menu-wrap" ref="menuWrap">
<ul class="menu">
<li
class="menu-item border-1px"
v-for="(item, index) of goods"
:key="index"
:class="{'current':currentIndex === index}"
@click="foodMove(index,$event)"
>
<span class="text">
<span v-if="item.type>0" class="icon" :class="classMap[item.type]"></span>
<span>{{item.name}}</span>
</span>
</li>
</ul>
</div>
<div class="main-wrap" ref="mainWrap">
<div>
<ul class="main food-list-hook" v-for="(good, index) of goods" :key="index">
<div class="title-wrap">
<div class="title">{{good.name}}</div>
</div>
<ul class="foods-wrap">
<li class="food-item border-1px" v-for="(item, index) of good.foods" :key="index">
<div class="food-img-wrap">
<img :src="item.image" width="57" height="57">
</div>
<div class="food-content">
<div class="name">{{item.name}}</div>
<div class="desc" v-if="item.description">{{item.description}}</div>
<div class="desc2">
<span class="sellCount">月售{{item.sellCount}}份</span>
<span class="rating">好评率{{item.rating}}%</span>
</div>
<div class="price-all">
<span class="price-icon">¥</span>
<span class="price">{{item.price}}</span>
<span class="old-price" v-if="item.oldPrice">¥{{item.oldPrice}}</span>
</div>
<div class="add">
<span class="iconfont" v-show="false"></span>
<span class="num" v-show="false">0</span>
<span class="iconfont"></span>
</div>
</div>
</li>
</ul>
</ul>
</div>
</div>
</div>
</template>
<script>
import BScroll from 'better-scroll'
const ERR_OK = 0
export default {
data () {
return {
goods: {},
listHeight: [],
scrollY: 0
}
},
created () {
this.classMap = ['decrease', 'discount', 'special', 'invoice', 'guarantee']
this.$http.get('/api/goods').then((response) => {
response = response.body
if (response.errno === ERR_OK) {
this.goods = response.data
// 由于vue中数据更新是异步的,在dom解构没有加载完成,BScroll无法获取目标容器的高度,会出现无法滚动的现象,
vue中的$nextTick()可以解决这个问题,在页面数据变化完成后才执行的函数需要写在$nextTick中 。
this.$nextTick(() => {
this._initScroll()
this._calculateHeight()
})
}
}
)
},
computed: {
// 计算当Y值在listHeight列表的某个区间时,返回这个标题的索引,将这个索引绑定到menu-wrap下的所有标签,
当这个索引等于所有标签中的某个索引时,将这个标签绑定class="current",就可以设置样式了
currentIndex () {
for (let i = 0; i < this.listHeight.length; i++) {
let height1 = this.listHeight[i]
let height2 = this.listHeight[i + 1]
if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
return i
}
}
return 0
}
},
methods: {
// 初始化2个滚动条
// better-scroll会将点击事件去掉,传入click: true后这个滚动项中的子标签才能被点击
// probeType设置为3可以监听scroll
_initScroll () {
this.menuScroll = new BScroll(this.$refs.menuWrap, {
click: true
})
this.mainScroll = new BScroll(this.$refs.mainWrap, {
click: true,
probeType: 3
})
// 实时计算当前滚动的Y值
this.mainScroll.on('scroll', (pos) => {
this.scrollY = Math.abs(Math.round(pos.y))
})
},
// 获取每个标题的Y值,存储于listHeight
_calculateHeight () {
let foodList = this.$refs.mainWrap.getElementsByClassName('food-list-hook')
let height = 0
this.listHeight.push(height)
for (let i = 0; i in foodList; i++) {
height += foodList[i].clientHeight
this.listHeight.push(height)
}
},
foodMove (index, event) {
// 在非移动端模式下,better-scroll也派发了一个click事件,所以有2个事件,
使用event._constructed将js原生事件return掉
// better-scroll派发的event事件和原生js的event有属性上的区别,其中有一个属性为event._constructed。
better-scroll派发的事件中event._constructed为true,原生点击事件中没有这个属性。
if (!event._constructed) {
return
}
// better-scroll有一个接口scrollToElement可以操作滚动到指定元素,第二个参数为滚动动画时间
let food = this.$refs.mainWrap.getElementsByClassName('food-list-hook')[index]
this.mainScroll.scrollToElement(food, 300)
}
}
}
</script>