vue.js 1.0中用v-for遍历出的li中的@click事件在移动端无效

本文介绍了一种在Vue.js中使用Better Scroll时遇到的问题:通过v-for生成元素上的@click事件在移动端不生效的情况。文章提供了源代码示例,并指出问题在于Better Scroll默认阻止了touch事件,解决方案是在配置中加入click: true选项。

在vue.js使用v-for遍历出的li中的@click事件在移动端无效,在网页端可以执行,代码如下

<template>
  <div class="rating-section" ref="ratingSection">
    <div>
      <div class="comprehensive">
        <div class="score">
          <div class="mark">{{seller.score}}</div>
          <div class="text">综合评分</div>
          <div class="compare">高于周边商家{{seller.rankRate}}%</div>
        </div>
        <div class="service">
          <div class="service-item">
            <span class="lable-text">服务态度</span>
            <div class="star-container">
              <star :size="12" :score="seller.serviceScore"></star>
            </div>
            <span class="inline-score">{{seller.serviceScore}}</span>
          </div>
          <div class="service-item">
            <span class="lable-text">菜品评价</span>
            <div class="star-container">
              <star :size="12" :score="seller.foodScore"></star>
            </div>
            <span class="inline-score">{{seller.foodScore}}</span>
          </div>
          <div class="service-item">
            <span class="lable-text">送达时间</span>
            <span class="delivery">{{seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <div class="rating-container">
        <div class="setter">
          <rating-select :options="options"></rating-select>
        </div>
        <div class="rating-setting" @click="onlyContentClick">
          <span class="icon-check_circle" :class="{'highlight':onlyContent}"></span>
          <span class="text">只看有内容的评价</span>
        </div>
        <div class="rating-list">
          <ul>
            <li class="item-rating" v-for="item in ratings">
              <div class="avart">
                <img :src="item.avatar">
              </div>
              <div class="content">
                <div class="username">{{item.username}}</div>
                <div class="user-score">
                  <div class="score">
                    <star :size="12" :score="item.score"></star>
                  </div>
                  <span v-show="item.deliveryTime>0">{{item.deliveryTime}}分钟送达</span>
                </div>
                <div class="text">{{item.text}}</div>
                <div class="thumbs-up">
                  <span class="split" :class="[{'icon-thumb_down':item.rateType===0},{'icon-thumb_up':item.rateType===1}]"></span>
                  <span class="thumb-item split" v-for="r in item.recommend">{{r}}</span>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll';
  import RatingSelect from '../ratingSelect/ratingSelect.vue';
  import star from '../star/star.vue';
  const ERROR_OK = 0;
  export default{
    data () {
      return {
        ratings: [],
        onlyContent: false,
        options: [
          {
            text: '全部',
            count: 57,
            type: 'positive'
          }, {
            text: '满意',
            count: 47,
            type: 'positive'
          }, {
            text: '不满意',
            count: 10,
            type: 'negative'
          }]
      };
    },
    created () {
      this.loadData();
    },
    props: {
      seller: {
        type: Object,
        default () {
          return {};
        }
      }
    },
    components: {star, RatingSelect},
    methods: {
      onlyContentClick () {
        this.onlyContent = !this.onlyContent;
      },
      loadData () {
        this.$http.get('/getRatings').then(res => {
          if (res.body.code === ERROR_OK) {
            this.ratings = res.body.result;
            this.$nextTick(() => {
              if (!this.ratingScroll) {
                this.initScroll();
              } else {
                this.ratingScroll.refresh();
              }
            });
          }
        });
      },
      initScroll () {
        this.ratingScroll = new BScroll(this.$refs.ratingSection, {
          click: true  //better-scroll 默认会阻止浏览器的原生 click 事件。当设置为 true,better-scroll 会派发一个 click 事件
        });
      }
    }
  };
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .rating-section
    position absolute;
    top 176px;
    bottom 0px;
    overflow hidden;
    width 100%;
    font-size 0;
    background-color rgb(243, 245, 247);
    .comprehensive
      display flex;
      padding 18px 0;
      background-color #FFF;
      border-bottom 1px solid rgba(7, 17, 27, .1);
      .score
        padding 0px 16px;
        border-right 1px solid rgba(7, 17, 27, .1);
        text-align center;
        .mark
          margin-bottom 6px;
          line-height 28px;
          font-size 24px;
          color rgb(255, 153, 0);
        .text
          margin-bottom 8px;
          line-height 12px;
          font-size 12px;
          color rgb(7, 17, 27);
        .compare
          margin-bottom 6px;
          line-height 10px;
          font-size 10px;
          color rgba(7, 17, 27, .6);
      .service
        flex 1;
        padding-left 16px;
        .service-item
          margin-bottom 8px;
          line-height 18px;
          font-size 12px;
          .lable-text
            color rgb(7, 17, 27);
          .inline-score
            color rgb(255, 153, 0);
          .delivery
            color rgb(147, 153, 159);
          .star-container
            display inline-block;
          &:last-child
            margin-bottom 0;
    .rating-container
      margin-top 18px;
      background-color #fff;
      border-top 1px solid rgba(7, 17, 27, .1);
      .setter
        padding 0 18px;
        border-bottom 1px solid rgba(7, 17, 27, .1);
      .rating-setting
        padding 12px 18px;
        font-size 0;
        color rgb(147, 153, 159);
        .icon-check_circle
          margin-right 4px;
          font-size 24px;
          &.highlight
            color #00c850;
        .text
          font-size 12px;
          line-height 24px;
          vertical-align top;
      .rating-list
        padding 18px 18px 0px 18px;
        border-top 1px solid rgba(7, 17, 27, .1);
        .item-rating
          display flex;
          padding 18px 0;
          font-size 12px;
          border-bottom 1px solid rgba(7, 17, 27, .1);
          &:first-child
            padding-top 0;
          .avart
            margin-right 12px;
            img
              width 28px;
              height 28px;
              border-radius 50%;
          .content
            font-size 10px;
            line-height 12px;
            .username
              color rgb(7, 17, 27);
            .user-score
              color rgb(147, 153, 159);
              .score
                display inline-block;
            .text
              margin-bottom 7px;
              font-size 12px;
              line-height 18px;
              color rgb(7, 17, 27);
            .thumbs-up
              .split
                margin-right 8px;
                &:last-child
                  margin-right 0;
              .icon-thumb_up
                color #00a0dc;
              .icon-thumb_down
                color rgb(183, 187, 191);
              .thumb-item
                display inline-block;
                padding 0px 6px;
                margin-bottom 4px;
                line-height 16px;
                font-size 9px;
                color rgb(147, 153, 159);
                border-radius 2px;
                background-color rgb(255, 255, 240, .1);
                border 1px solid rgba(7, 17, 27, .1);
</style>

应该是你使用了better-scroll的原因,默认它会阻止touch事件。所以在配置中需要加上click: true

具体的配置还可以查看 https://github.com/ustbhuangyi/better-scroll

主题任务二:智能仪表盘 项目概述: 创建一个模拟的数据可视化仪表盘,展示统计数据、用户信息和任务列表,支持数据筛选和状态更新。 知识点覆盖与实现要求: 1.VUE3 项目创建流程 要求:使用 Vite 创建名为smart-dashboard的新项目,完成基础配置并运行。 2.Vue.js 开发基础 数据绑定:在Dashboard.vue中定义userInfo(用户信息)、stats(统计数据)、tasks(任务列表)等响应式数据。 内容渲染:用v-text显示用户名、统计数值;用{{ }}展示任务标题。 条件渲染:用v-if/v-else-if根据任务status(未开始 / 进行中 / 已完成)显示不同状态标签;用v-show控制 “数据筛选面板” 的显示。 列表渲染:用v-for遍历stats数组渲染统计卡片;遍历tasks数组渲染任务列表,结合:key确保唯一性。 3.Vue 组件的使用1 父向子传参: Dashboard.vue向StatCard.vue传递单个统计项(:data="stat",包含title、value、icon)。 Dashboard.vue向TaskList.vue传递任务列表(:tasks="filteredTasks")和筛选条件(:filter="statusFilter")。 子向父传参: TaskItem.vue的状态切换按钮点击时,通过$emit('update-status', task.id, newStatus)通知父组件更新任务状态。 FilterPanel.vue的筛选条件变化时,通过$emit('change-filter', value)传递新的筛选值。 跨级组件通信: 在Dashboard.vue中用provide('userInfo', userInfo)提供用户信息。 在Header.vue(嵌套在Layout.vue中)用inject('userInfo')接收并显示用户名。 引用组件:在Dashboard.vue中组合StatCard.vue、TaskList.vue、FilterPanel.vue等组件。 4.Vue 组件的使用2 动态组件: 创建ChartBar.vue(柱状图)、ChartPie.vue(饼图)组件。 在Dashboard.vue中用下拉菜单切换activeChart值,通过<component :is="activeChart" />动态渲染图表。 生命周期函数: Dashboard.vue中用onMounted模拟加载初始数据;ChartBar.vue中用onUpdated在数据变化时重新绘制图表。 插槽的使用: 创建Widget.vue组件,含#header(标题)和默认插槽(内容),统一仪表盘组件布局。 StatCard.vue和TaskList.vue均使用Widget.vue包裹,通过插槽传入内容。 自定义指令: 注册v-tooltip指令,实现鼠标悬停时显示提示文本(如统计数据的说明)。 在StatCard.vue的数值上应用v-tooltip="'近7天数据'"。
最新发布
09-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值