前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue实战—菜单栏商品展示数据交互(8)

Vue实战—菜单栏商品展示数据交互(8)

原创
作者头像
前端大彬哥
修改2019-07-23 09:46:39
9440
修改2019-07-23 09:46:39
举报
文章被收录于专栏:大前端666大前端666

上篇我们将菜单栏,商品展示结构完成,本次我们为这两个部分接入数据。

菜单栏接入数据

导入组件,定义需要的数据格式
代码语言:javascript
复制
<script>
// 导入BScroll 滚动组件
import BScroll from "better-scroll";
// 导入Food 商品页面
import Food from "components/Food/Food";
​
export default {
  data() { //准备需要的数据,初始化组件。
    return {
      container: {},
      goods: [],
      poiInfo: {},
      listHeight: [],
      scrollY: 0,
      menuScroll: {},
      foodScroll: {},
      selectedFood: {}
    };
  },
    //引用组件
  components: {
    BScroll,
    Food
  }
};
</script>
通过created钩子发起请求获取数据

之前我们说过在created钩子,mounted钩子内可以发起请求,请求需要的数据。本次我们在created钩子内发起get请求,获取数据:

代码语言:javascript
复制
  created() {
    //通过that改变.then中的this指向
    var that = this;
​
    // 通过axios发起get请求
    this.$axios
      .get("/api/goods")
      .then(function(response) {
        // 获取到数据
        var dataSource = response.data;
        if (dataSource.code == 0) {
          that.container = dataSource.data.container_operation_source;
          that.goods = dataSource.data.food_spu_tags;
          that.poiInfo = dataSource.data.poi_info;
​
          // 调用滚动的初始化方法
          // that.initScroll();
          // 开始时,DOM元素没有渲染,即高度是问题
          // 在获取到数据后,并DOM已经被渲染,表示列表高度是没问题的
          // nextTick
          that.$nextTick(() => {
            // DOM已经更新
            that.initScroll();
​
            // 计算分类区间高度
            that.calculateHeight();
          });
        }
      })
      .catch(function(error) {
        // 出错处理
        console.log(error);
      });
  },

注意$nextTick()用法,在dom更新后。我们执行初始化滚动函数。

https://cn.vuejs.org/v2/api/#vm-nextTick

通过methods定义我们需要的方法
  • 通过head_bg(imgName)方法获取到商品的背景图片;
  • 方法initScroll()用来初始化滚动,https://cn.vuejs.org/v2/api/#ref;
  • calculateHeight()方法获取左侧每一个菜单栏目的元素;
  • 使用selectMenu()方法,在我们点击菜单后,右侧显示对应的商品信息;
代码语言:javascript
复制
​
  methods: {
    head_bg(imgName) {
      return "background-image: url(" + imgName + ");";
    },
    // 滚动的初始化
    initScroll() {
      // ref属性就是用来绑定某个dom元素或某个组件,然后在this.$refs里面
      this.menuScroll = new BScroll(this.$refs.menuScroll, {
        click: true
      });
      this.foodScroll = new BScroll(this.$refs.foodScroll, {
        probeType: 3,
        click: true
      });
​
      // 添加滚动监听事件
      this.foodScroll.on("scroll", pos => {
        this.scrollY = Math.abs(Math.round(pos.y));
      });
    },
    calculateHeight() {
      // 通过$refs获取到对应的元素
      let foodlist = this.$refs.foodScroll.getElementsByClassName(
        "food-list-hook"
      );
​
      // 添加到数组区间
      // 0~216 第一个区间(专场)
      // 217~1342 第二个区间(热销)
      let height = 0;
      this.listHeight.push(height);
      for (let i = 0; i < foodlist.length; i++) {
        let item = foodlist[i];
​
        // 累加
        height += item.clientHeight;
​
        this.listHeight.push(height);
      }
    },
    selectMenu(index) {
​
      let foodlist = this.$refs.foodScroll.getElementsByClassName(
        "food-list-hook"
      );
​
      // 根据下标,滚动到相对应的元素
      let el = foodlist[index];
      // 滚动到对应元素的位置
      this.foodScroll.scrollToElement(el, 250);
    }
  },
computed定义属性
  • currentIndex属性绑定在左侧菜单栏,使菜单元素与右侧内容作为对应,展示给用户。
代码语言:javascript
复制
​
  computed: {
    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;
    }
  },

以上我们完成了商品页面数据的交互,下一章节我们将加入商品控件,与购物车。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 菜单栏接入数据
    • 导入组件,定义需要的数据格式
      • 通过created钩子发起请求获取数据
        • 通过methods定义我们需要的方法
          • computed定义属性
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档