【问题标题】:VueJS - Conditional component mouse eventsVueJS - 条件组件鼠标事件
【发布时间】:2019-07-01 01:46:57
【问题描述】:

Vue 和一般框架的新手,我的想法可能不太“像 Vue”。

尝试制作一个带有道具的“超级”按钮组件,该道具决定按钮的行为,因此我只需要维护一个按钮组件。实现时的理想形式是这样的......

<super-button isType="string"></super-button>

模板是...

    <button class="buttonLow" v-bind:class="{buttonHigh: isSelected}">
            <slot></slot>
    </button>

其中isType 属性可以是momentary momentary-multi toggle 或toggle-multi。

我有一组基本的事件发射器/方法和侦听器,无论isType 如何都可以工作,并且只需使用另一个道具isSelected 使按钮状态为高或低/打开或关闭。

问题是尝试根据isType 有条件地设置鼠标事件。在弄清楚逻辑的同时,我使用@ 语法来设置鼠标事件@click@mousedown@mouseup 等,一切都很好。例如,测试期间momentary 按钮的事件如下所示...

<button @mousedown="emitButtonHigh()" @mouseup="emitButtonLow" @mouseleave="emitButonLow"></button>

然而,一个简单的切换按钮看起来更像这样......

<button @click="emitButtonToggle()"></button>

显然这里有一点冲突。

我尝试的解决方法是在created() 中使用一个switch 语句,它将isType 作为表达式并有条件地注册适当的鼠标事件...

created(){
    switch(this.isType){
        case ("momentary"):{
            //attach on events
            break;
        }
        case ("toggle"):{
            //attach on events
            break;
        }
        case ("toggle-multi"):{
            //attach on events
            break;
        }
        default:{
            break;
        }
    }
}

虽然 switch 本身正在工作,但我不知道如何在此上下文中附加鼠标事件。我可以使用...附加自定义事件没问题。

    this.$root.$on('my-custom-event', ()=>{
        //do stuff
    });

但尝试做类似...

    this.$root.$on('click', ()=>{
        //do stuff
    });

或者...

    this.$on('click', ()=>{
        //do stuff
    });

不起作用,我也想不出任何方法可以创建与 @click 相同的功能,更不用说 @mousedown @mouseup 或其他内置事件。

TL;DR

如何使用$on 语法为内置事件(单击、鼠标按下、鼠标向上等)写出@ 语法或v-on 语法,以便实际触发事件?

【问题讨论】:

  • 使用计算属性,该属性根据 prop 的值返回包含事件侦听器/处理程序的对象。然后您可以使用v-on="yourComputedProp" 一次性附加它们。

标签: javascript vue.js


【解决方案1】:

您可以将所有这些组件事件附加到单个处理程序上,在它们被触发时确定event.types,并从这里发出您的自定义事件,同时可以选择传递其他参数。

const SuperButton = Vue.extend({
  template: `
    <button 
      @mousedown="emitCustomEvent" 
      @mouseup="emitCustomEvent" 
      @mouseleave="emitCustomEvent">
      <slot></slot>
    </button>
  `, 

  props: {
    isType: {
      type: String,
      default:
        String
      },
      
    // ...
  },

  methods: {
    emitCustomEvent(e) {
      let type = e.type.substr('mouse'.length);
      let args = {
        type,
        isSelected: type === 'down',
        args: {
          // Your additional args here
        }
      };
      
      switch(this.isType) {
        case ("momentary"):{
          //attach on events
          break;
        }
          // ...
      }
      
      // Or emit events regardless of the "isType"
      
      this.$emit(`mouse:${type}`, args);
      this.$emit('mouse', args);
    }
  }
});

new Vue({
  el: '#app',

  methods: {
    mousing(args) {
      console.log(`mouse:${args.type} from component.`);
    },
    
    mouseLeaving() {
      console.log('Mouse leaving.');
    }
  },

  components: {
    SuperButton
  }
});

Vue.config.devtools = false;
Vue.config.productionTip = false;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <super-button @mouse="mousing">Super button</super-button>
  
  <super-button @mouse:leave="mouseLeaving">Super button 2, detecting leaving only</super-button>
</div>

【讨论】:

  • 太棒了,我对其进行了一些修改以适应我的需要,但现在它工作得很好。
猜你喜欢
  • 2018-08-13
  • 2019-01-04
  • 2018-06-11
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 2020-05-10
相关资源
最近更新 更多