ratingselect组件复用后button按钮点击事件失效

来源:

若强

2016-12-15

http://szimg.mukewang.com/5851700e0001f17303320266.jpg

上图红框内的按钮点击没有效果,该部分是ratingselect组件在ratings.vue里面引用的时候出现的问题,但是在food.vue里面引用却是可以的

ratingselect组件代码

<template>
<div>
  <div>
    <span class="block positive" @click="select(2,$event)" :class="{'active':selectTyped===2}">{{desc.all}}<span>{{ratings.length}}</span></span>
    <span class="block positive" @click="select(0,$event)":class="{'active':selectTyped===0}">{{desc.positive}}<span>{{positives.length}}</span></span>
    <span class="block negative" @click="select(1,$event)":class="{'active':selectTyped===1}">{{desc.negative}}<span>{{negatives.length}}</span></span>
  </div>
  <div @click="toggleContent" :class="{'active':onlyContented}">
    <span ></span>
    <span>只看有内容的评价</span>
  </div>
</div>
</template>
<script>
import Vue from 'vue';
//定义常量选择类型
const POSITIVE = 0;
const NEGATIVE = 1;
const All = 2;
export default {
  props: {
    ratings: {
      type: Array,
      default () {
        return [];
      }
    },
    selectType: {
      type: Number,
      default: All
    },
    onlyContent: {
      type: Boolean,
      default: false //如果没有传递的话才默认
    },
    desc: {
      type: Object,
      default () { 
        return {
          all: '全部',
          positive: '满意',
          negative: '不满意'
        };
      }
    }
  },
  data() {
    return {
      selectTyped: this.selectType, //对props接收多了的值存一下,以便后面操做不报错
      onlyContented: this.onlyContent
    };

  },
  computed: {
    positives() {
      return this.ratings.filter((rating) => { //过滤每一条rating,
        return rating.rateType === 0; 
      });
    },
    negatives() {
      return this.ratings.filter((rating) => {
        return rating.rateType === 1;
      });
    }

  },
  methods: {
    select(type, event) {
      if (!event._constructed) {
        return;
      }
      this.selectTyped = type; //修改选择的类型
       
      this.$parent.$emit('ratingType', this.selectTyped);//向父元素派发ratingType事件并且传递过去type值

      console.log(this.$parent);
    },
    toggleContent(event) {
      if (!event._constructed) {
        return;
      }
      
      this.onlyContented = !this.onlyContented;
      this.$parent.$emit('onlyContented', this.onlyContented);
    }
  }
};
</script>

food组件里面引用后可以正常点击,以下是food.vue部分代码截图

food.vue

http://szimg.mukewang.com/58516e800001d79f08880385.jpg

这里的按钮是可以点击切换高亮样式的

http://szimg.mukewang.com/58516f9c00016ca603220520.jpg


写回答

4回答

ustbhuangyi

2016-12-15

请提供一个完整的外网访问地址~

0
2
ustbhuangyi
因为你整体没有套 better-scroll?
2016-12-15
共2条回复

还好了

2016-12-24

//szimg.mukewang.com/585e8af5000144ce05760141.jpg

然后,就报错了,

//szimg.mukewang.com/585e8bb90001124c09260062.jpg

还需要用npm安装一些东西吗?

0
0

若强

提问者

2016-12-24

你好像不是对父元素派发的事件

ratingselect组件里面这样写派发事件到父组件

this.$parent.$emit('onlyContented', this.onlyContented);

然后再去父组件的created里面监听这个事件

this.$on('ratingType', function(type) { 
        this.selectType = type;
        this.$nextTick(() => {
          this.scroll.refresh();
        });
      });

你试一下打印看看能不能拿到值

0
0

还好了

2016-12-23

您好,我的问题是一开始报这个错误//szimg.mukewang.com/585d449f00011f7c06720114.jpg,然后,我就按着你的代码改了一下,//szimg.mukewang.com/585d44f00001d46106220627.jpg

修改以后,单击不管用了,没有任何效果了,将下面的红色红框注释以后,也不起作用//szimg.mukewang.com/585d455c0001b83504580367.jpg

 

 

0
1
若强
你对他父组件去派发事件试一下 this.$parent.$emit('onlyContented', this.onlyContented);
2016-12-24
共1条回复

Vue.js2.5+cube-ui重构饿了么App(经典再升级)

掌握Vue1.0到2.0再到2.5最全版本应用与迭代,打造极致流畅的WebApp

9868 学习 · 4162 问题

查看课程