使用一个方法中的多个 $emits

huangapple go评论46阅读模式
英文:

Using multiple $emits in one method

问题

请就这个更为理论性的问题给我一些建议。

我需要在项目中创建一些自定义元素。对于不同的变体,需要添加样式和方法。

关于方法和 $emit,我有一个问题。例如,input 标签。

我可以通过 $emit 从组件中获取字段的值。

<input @change="changeInput" />

export default {
  methods: {
    changeInput() {
      this.$emit('changeInput')
    }
  }
}

在这个阶段,出现了两个问题。

  1. 如果我在同一个方法中运行两个具有不同参数的 $emit,但只使用其中一个,另一个是否仍然会被触发并创建额外的负担?例如:
// Input 组件
<input @change="changeInput" />

export default {
  methods: {
    changeInput(event) {
      this.$emit('changeInput', event)
      this.$emit('changeInputValue', event.target.value)
    }
  }
}

// 父组件
<MyInput @changeInput="changeInput" />
  1. 例如,对于输入框,我只使用一个 $emit,但使用它的所有值,即使大多数地方只需要它的各个值。传递值的负担是否会大于使用单独的部分?例如:
// Input 组件
<input @change="changeInput" />

export default {
  methods: {
    changeInput(event) {
      this.$emit('changeInput', event)
    }
  }
}

// 父组件
<MyInput @changeInput="changeName" />
<MyInput @changeInput="changeEmail" />
<MyInput @changeInput="getInput" />

export default {
  data: () => ({
    name: '',
    email: '',
    input: {}
  }),
  methods: {
    changeName(event) {
      this.name = event.target.value
    },
    changeEmail(event) {
      this.email = event.target.value
    },
    getInput(event) {
      this.input = event
    }
  }
}

一方面,这里总是传递整个对象,如果需要的话,可以随心所欲地使用它。但如果在大多数情况下只使用此输入标记的值,是否有必要始终传递整个对象?还是最好为所有条件制作多个 $emit:

changeInput(event) {
  this.$emit('changeInput', event)
  this.$emit('changeInputTarget', event.target)
  this.$emit('changeInputValue', event.target.value)
}

这里我写了一个单个输入,但如果它是一个具有大数据对象的组件呢?根据情况,它需要通过 $emit 传递部分或全部。在这种情况下,最佳操作方式是什么?

我使用了具有不同值的多个 $emit,以及一个通用的 $emit。从全局来看,这不会影响性能。但突然之间,如果增加组件的数量,可能会出现问题。我希望在任何项目中确保尽可能高的性能。

英文:

Please advise me on this more theoretical question.

I need to create some custom elements in the project. And for different variants to add styles and methods.

And I have a question about methods and $emit.

For example, the tag input.

I can get the value of the field from the component via $emit.

&lt;input @change=&quot;changeInput&quot; /&gt;

export default {
  methods: {
    changeInput() {
      this.$emit(&#39;changeInput&#39;)
    }
  }
}

At this stage, two questions arise.

  1. If I run two $emit with different parameters in the same method, but use only one of them, will the other still be triggered and create an additional load? For example:
// Input component
&lt;input @change=&quot;changeInput&quot; /&gt;

export default {
  methods: {
    changeInput(event) {
      this.$emit(&#39;changeInput&#39;, event)
      this.$emit(&#39;changeInputValue&#39;, event.target.value)
    }
  }
}

//Parent component
&lt;MyInput @changeInput=&quot;changeInput&quot; /&gt;
  1. If, for example, for an input I use only one $emit but with all its value, even though most places only need its individual values. Would the load of passing values be greater than using the individual parts? For example:
// Input component
&lt;input @change=&quot;changeInput&quot; /&gt;

export default {
  methods: {
    changeInput(event) {
      this.$emit(&#39;changeInput&#39;, event)
    }
  }
}

//Parent component
&lt;MyInput @changeInput=&quot;changeName&quot; /&gt;
&lt;MyInput @changeInput=&quot;changeEmail&quot; /&gt;
&lt;MyInput @changeInput=&quot;getInput&quot; /&gt;

export default {
  data: () =&gt; ({
    name: &#39;&#39;,
    email: &#39;&#39;,
    input: {}
  }),
  methods: {
    changeName(event) {
      this.name = event.target.value
    },
    changeEmail(event) {
      this.email = event.target.value
    },
    getInput(event) {
      this.input = event
    }
  }
} 

On the one hand, the whole object is always transferred here, and it can be used as you want if necessary. But if in most cases only the value of this input tag is used, does it make sense to always pass the whole object? Or is it better to make several $emit for all conditions:

changeInput(event) {
  this.$emit(&#39;changeInput&#39;, event)
  this.$emit(&#39;changeInputTarget&#39;, event.target)
  this.$emit(&#39;changeInputValue&#39;, event.target.value)
}

Here I wrote a single input, but what if it's a component with a big data object? And depending on the case, it needs to be passed through $emit either partially or completely. What's the best way to proceed in such cases?

I have used several $emit with different values, as well as a common one. Globally, this has no effect on performance. But suddenly, if you increase the number of components, you may end up with problems. I'd like to ensure as much performance as possible in any of the projects.

答案1

得分: 1

Both emits would execute, and cause additional load

两个 $emit 将都执行,并引起额外的负载。

It is sometimes tempting to think of an $emit as a return. But it isn't. It does not "end" the component, or the function it is in. It is executed, and then the next $emit is executed in turn.
有时候会有一种诱人的想法,把 $emit 当作 return 来考虑。但它不是。它不会"结束"组件或函数,它被执行,然后下一个 $emit 依次执行。

this.$emit('changeInput', event)
this.$emit('changeInputValue', event.target.value)

The load of passing the bigger object will not be significantly larger

传递更大对象的负载不会显著增加。

Something is going to have to unwrap event to event.target.value. Either the child component or the parent component.

If you think you will ever need the "bigger" object, then pass the bigger object consistently.

i.e. do not do two $emits, just do this:
也就是,不要进行两次 $emit,只需这样做:

this.$emit('changeInput', event)
英文:

Both emits would execute, and cause additional load

It is sometimes tempting to think of an $emit as a return. But it isn't. It does not "end" the component, or the function it is in. It is executed, and then the next $emit is executed in turn.

      this.$emit(&#39;changeInput&#39;, event)
      this.$emit(&#39;changeInputValue&#39;, event.target.value)

The load of passing the bigger object will not be significantly larger

Something is going to have to unwrap event to event.target.value. Either the child component or the parent component.

If you think you will ever need the "bigger" object, then pass the bigger object consistently.

i.e. do not do two $emits, just do this:

      this.$emit(&#39;changeInput&#39;, event)

huangapple
  • 本文由 发表于 2023年4月4日 11:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定