老师,下拉刷新错误,麻烦看下
src/base/scroll/index.vue <template> <swiper :options="swiperOption" ref='swiper'> <div class="mine-scroll-pull-down" v-if="pullDown"> <me-loading :text="pullDownText" inline ref="pullDownLoading"/> </div> <swiper-slide> <slot></slot> </swiper-slide> <div class="swiper-scrollbar" v-if="scrollbar" slot="scrollbar"></div> </swiper> </template> <script> import {swiper, swiperSlide} from 'vue-awesome-swiper'; import MeLoading from 'base/loading'; import { PULL_DOWN_HEIGHT, PULL_DOWN_TEXT_INIT, PULL_DOWN_TEXT_START, PULL_DOWN_TEXT_ING, PULL_DOWN_TEXT_END, PULL_UP_HEIGHT, PULL_UP_TEXT_INIT, PULL_UP_TEXT_START, PULL_UP_TEXT_ING, PULL_UP_TEXT_END } from './config'; export default{ name: 'MeScroll', components: { swiper, swiperSlide, MeLoading }, props: { scrollbar: { type: Boolean, default: true }, data: { type: [Array, Object] }, pullDown: { type: Boolean, default: false }, }, data() { return { pulling: false, pullDownText: PULL_DOWN_TEXT_INIT, swiperOption: { direction: 'vertical', slidesPerView: 'auto', freeMode: true, setWrapperSize: true, scrollbar: { el: this.scrollbar ? '.swiper-scrollbar' : null, hide: true }, on: { sliderMove: this.scroll, touchEnd: this.touchEnd } } }; }, watch: { data() { this.update(); } }, methods: { update() { this.$refs.swiper && this.$refs.swiper.swiper.update(); }, scroll() { const swiper = this.$refs.swiper.swiper; if (this.pulling) { return; } if (swiper.translate > 0) { // 下拉 if (!this.pullDown) { return; } if (swiper.translate > PULL_DOWN_HEIGHT) { this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_START); } else { this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_INIT); } } }, touchEnd() { if (this.pulling) { return; } const swiper = this.$refs.swiper.swiper; if (swiper.translate > PULL_DOWN_HEIGHT) { // 下拉 if (!this.pullDown) { return; } this.pulling = true; swiper.allowTouchMove = false;// 禁止触摸 swiper.setTransition(swiper.params.speed); swiper.setTranslate(PULL_DOWN_HEIGHT); swiper.params.virtualTranslate = true;// 定住不给回弹 this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_ING); this.$emit('pull-down', this.pullDownEnd);// 触发一个事件 } }, pullDownEnd() { const swiper = this.$refs.swiper.swiper; this.pulling = false; this.$refs.pullDownLoading.setText(PULL_DOWN_TEXT_END); swiper.params.virtualTranslate = false; swiper.allowTouchMove = true; swiper.setTransition(swiper.params.speed); swiper.setTranslate(0); } } } </script> <style scoped> .swiper-container { overflow: hidden; width: 100%; height: 100%; }; .swiper-slide { height: auto; } .mine-scroll-pull-down { position: absolute; left: 0; bottom: 100%; width: 100%; height: 80px; } </style> src/pages/home/index.vue <template> <div class="home"> <header class="g-header-container"> <home-header/> </header> <me-scroll :data="recommends" pullDown @pull-down="pullToRefresh"> <home-slider ref="slider"/> <home-nav/> <home-recommend @loaded="getRecommends"/> </me-scroll> <div class="g-backtop-container"></div> <router-view></router-view> </div> </template> <script> import HomeHeader from './header'; import HomeSlider from './slider'; import MeScroll from 'base/scroll'; import HomeNav from './nav'; import HomeRecommend from './recommend'; export default{ name: 'Home', components: { MeScroll, HomeHeader, HomeSlider, HomeNav, HomeRecommend, }, data() { return { recommends: [], //isBacktopVisible: false, //isHeaderTransition: false }; }, methods: { updateScroll() { }, getRecommends(recommends) { this.recommends = recommends; }, pullToRefresh(end) { this.$refs.slider.update().then(end); // setTimeout(() =>{ // console.log('下拉刷新'); // end(); // },1000); }, } }; </script> <style scoped> @import "~assets/scss/mixins"; .home { overflow:hidden;width:100%;height:100%;background-color: $bgc-theme; } </style>
1
收起
正在回答
3回答
同学你好,home/slider.vue文件中,如下,缺少return;
希望能帮助到你,欢迎采纳。
祝学习愉快!
慕虎7525453
2019-01-10 11:46:32
api/home.js import axios from 'axios'; import jsonp from 'assets/js/jsonp'; import {SUCC_CODE, TIMEOUT, HOME_RECOMMEND_PAGE_SIZE, jsonpOptions} from './config'; // 打乱数组顺序 const shuffle = (arr) => { const arrLength = arr.length; let i = arrLength; let rndNum; while (i--) { if (i !== (rndNum = Math.floor(Math.random() * arrLength))) { [arr[i], arr[rndNum]] = [arr[rndNum], arr[i]]; } } return arr; }; // 获取幻灯片数据--ajax export const getHomeSlider = () => { return axios.get('http://www.imooc.com/api/home/slider',{ timeout: TIMEOUT }).then(res => { if(res.data.code === SUCC_CODE) { let sliders = res.data.slider; const slider = [sliders[Math.floor(Math.random() * sliders.length)]]; sliders = shuffle(sliders.filter(() => Math.random() >= 0.5)); if (sliders.length === 0) { sliders = slider; } return sliders; } throw new Error('没有成功获取到数据!'); }).catch(err => { if (err) { console.log(err); } return [ { linkUrl: 'https://www.imooc.com', picUrl: require('assets/img/404.png') } ]; }).then(res => { return new Promise(resolve => { setTimeout(() => { resolve(res); }, 1000); }); }); }; // 获取热门推荐数据--jsonp export const getHomeRecommend = (page = 1, psize = HOME_RECOMMEND_PAGE_SIZE) => { const url = 'https://ju.taobao.com/json/tg/ajaxGetItemsV2.json'; const params = { page, psize, type: 0, frontCatId: '' }; return jsonp(url, params, jsonpOptions).then(res => { if (res.code === '200') { return res; } throw new Error('没有成功获取到数据!'); }).catch(err => { if (err) { console.log(err); } }).then(res => { return new Promise(resolve => { setTimeout(() => { resolve(res); }, 1000); }); }); }; home/slider.vue <template> <div class="slider-wapper"> <me-loading v-if="!sliders.length" /> <me-slider :data="sliders" :direction="direction" :loop="loop" :interval="interval" :pagination="pagination" v-else > <swiper-slide v-for="(item, index) in sliders" :key="index" > <a :href="item.linkUrl" alt="" class='slider-link'> <img :src="item.picUrl" alt="" class='slider-img'> </a> </swiper-slide> </me-slider> </div> </template> <script> import MeSlider from 'base/slider'; import {swiperSlide} from 'vue-awesome-swiper'; import {sliderOptions} from './config'; import {getHomeSlider} from 'api/home'; import MeLoading from 'base/loading'; export default { name: 'HomeSlider', components: { MeSlider, MeLoading, swiperSlide }, data() { return { direction: sliderOptions.direction, loop: sliderOptions.loop, interval: sliderOptions.interval, pagination: sliderOptions.pagination, sliders: [] }; }, created() { this.getSliders(); }, methods: { update() { return this.getSliders(); }, getSliders() { getHomeSlider().then(data => { this.sliders = data; }); } } }; </script> <style scoped> .slider-wapper { width: 100%; height: 183px; } .slider-link { display: block; } .slider-link, .slider-img { overflow: hidden; width: 100%; height: 100%; } </style> base/slider/index.vue <template> <swiper :options="swiperOption" :key="keyId"> <slot></slot> <div class="swiper-pagination" v-if="pagination" slot="pagination"> </div> </swiper> </template> <script> import {swiper} from 'vue-awesome-swiper'; export default { name: 'MeSlider', components: { swiper }, props: { direction: { type: String, default: 'horizontal', validator(value) { return [ 'horizontal', 'vertical' ].indexOf(value) > -1; } }, interval: { type: Number, default: 3000, validator(value) { return value >= 0; } }, loop: { type: Boolean, default: true }, pagination: { type: Boolean, default: true }, data: { type: Array, default() { return []; } } }, data() { return { keyId: Math.random() }; }, watch: { data(newData) { if (newData.length === 0) { return; } this.swiperOption.loop = newData.length === 1 ? false : this.loop; this.keyId = Math.random(); } }, created() { this.init(); }, methods: { init() { this.swiperOption = { watchOverflow: true, direction: this.direction, autoplay: this.interval ? { delay: this.interval, disableOnInteraction: false } : false, slidesPerView: 1, loop: this.data.length <= 1 ? false : this.loop, pagination: { el: this.pagination ? '.swiper-pagination' : null } }; } } } </script> <style scoped> .swiper-container { width: 100%; height: 100%; } </style>
热门框架Vue开发WebApp 18版
- 参与学习 人
- 提交作业 209 份
- 解答问题 3299 个
本路径是通过ES6基础知识、运用Zepto、Swiper、fullPag等移动端常用工具包、以及当下流行框架Vue,结合多个实战案例,还原真实开发场景,最终实现手机端购物商城网页开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星