【问题标题】:Can i add conditional events on svelte components?我可以在纤细的组件上添加条件事件吗?
【发布时间】:2019-01-31 16:06:52
【问题描述】:

是否可以在父组件中定义的条件下触发事件?

我有一个输入组件,我想在其中捕获输入.. 有时,但大多数时候我不希望事件触发

   //App.html
   <Input on:inputData="doStuff(event)" fireEvent=true />    

   //Input.html
   <input bind:value=value (fireEvent ? on:keyup='fire("inputData", { value })' : false )/>

目前发生的情况是fireEvent 被忽略并且on:keyup 总是触发

更新
我将keyup更改为一个函数调用,在触发事件之前我检查了参数,它可以工作,但有点古怪

//App.html
<Input on:inputData='doStuff(event.value)' fireEvent={true} />
 ...
  methods: {
  doStuff(text) {
    console.log('here is the stuff', text);
  }

//Input.html
<input bind:value=value on:keyup='setData({ value })'/>
...
methods: {
  setData(text) {
    if(this.get().fireEvent) {
      this.fire("inputData", text)
    }
  }
}

谁有这个问题的更漂亮的解决方案?

【问题讨论】:

    标签: events svelte


    【解决方案1】:

    我不知道为什么我没有想到这一点,但这正是onupdate 的用途

    //App.html
    <Input on:inputData='doStuff(event.value)' fireEvent={true} />
         ...
    methods: {
     doStuff(text) {
       console.log('here is the stuff', text);
     }
    
    //Input.html
    <input bind:value=value />
    ...
    onupdate({ changed, current, previous }) {
      if(this.get().fireEvent) {
        this.fire("inputData", current)
      }
    

    【讨论】:

      【解决方案2】:

      聚会很晚,但我遇到了类似的问题,最终通过行动解决了它。对于我来说,只有在某个条件为真时,我才需要将点击侦听器附加到按钮上。一个更通用的例子可能是:

      <button use:conditionalEvent({condition: foo, event: 'click', callback: bar})>...</button>
      
      ...
      
      function conditionalEvent(node, {condition, event, callback}) {
          if (condition) {
              node.addEventListener(event, callback);
          }
      
          return {
              destroy() {
                  node.removeEventListener(event, callback)
              } 
          }
      }
      
      

      如果仅针对单个回调或事件类型,标记可能不那么冗长,但您明白了。 如果条件可以随着用户输入而改变,你也可以返回一个更新函数:

      function changingCondition(node, {condition, event, callback}) {
          function update(condition) {
              if (condition) {
                  node.addEventListener(event, callback);
              } else {
                  node.removeEventListener(event, callback);
              }
          }
      
          update(condition);
      
          return {
              update,
              destroy() {
                  node.removeEventListener(event, callback)
              } 
          }
      }
      
      

      只要condition 发生变化,更新就会运行。

      【讨论】:

        【解决方案3】:

        我尝试了以下方法,它奏效了。注意小写的“fireevent”。大写字母把它扔掉了。当然,您必须将其更改为 on:inputData 等。

        	methods: {
        	    test(evt) {
        		console.log('evt.fireEvent', evt.target.getAttributeNode("fireEvent").value);
        		if (evt.target.getAttributeNode("fireevent").value && evt.target.getAttributeNode("fireevent").value=="true")
        		    console.log('do the happy dance');
        		else
        		    console.log('do NOTHING');
        
        	    }
        	}
        &lt;button on:click="test(event)" fireevent="false"&gt;Test&lt;/button&gt;

        【讨论】:

        • 是的,我已经用这样的方法尝试过这个方法并且它有效,但我不喜欢它的外观..我也意识到我的问题已通过 onupdate 解决
        猜你喜欢
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        • 2017-05-07
        • 2021-04-19
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多