简体   繁体   English

如何在 vuejs 的父组件中从子组件向 object 发出值?

[英]How to Emit values from child to an object in parent component in vuejs?

I am trying to use a child component with custom form inputs & emit those value to parent component.我正在尝试使用带有自定义表单输入的子组件并将这些值发送到父组件。 However, while I am trying to store the values in an object of parent component, One input value disapper when another input value is entered.但是,当我尝试将值存储在父组件的 object 中时,当输入另一个输入值时,一个输入值会消失。 Let me show some code:让我展示一些代码:

Child Component子组件

<template>
   <input
      type="text"
      @input="setField($event.target.value, 'title')"
   />
   <wysiwyg-input
      @change="(text) => setField(text, 'content')"
   />
</template>

<script>
export default {
methods: {
    setField(value, field) {
      this.$emit("input", {
        title: field === "title" && value,
        content: field === "content" && value,
      });
    },
  },
}
</script>

Parent Component父组件

<template>
  <FormFields v-model="blogPost" />
</template>

<script>

export default {
  data() {
    return {
      blogPost: {},
    };
  },
  watch: {
    blogPost(val) {
      console.log(val);
    },
  },
};
</script>

Here, when I try to input the 'content', the 'title' field become false.在这里,当我尝试输入“内容”时,“标题”字段变为假。 So how can I set the condition on child component as I can emit both input to parent component?那么如何在子组件上设置条件,因为我可以将两个输入都发送到父组件? Or Any other idea to accomplish the same task?或任何其他想法来完成相同的任务?

CodeSandbox link: https://codesandbox.io/s/mystifying-benz-w8wgu?file=/src/App.vue CodeSandbox 链接: https://codesandbox.io/s/mystifying-benz-w8wgu?file=/src/App.vue

You can use ternary operator:您可以使用三元运算符:

this.$emit("input", {
   title: field === "title" ? value : "",
   content: field === "content" ? value : "",
});

As per MDN docs根据MDN 文档

&& operator actually returns the value of one of the specified operands &&运算符实际上返回指定操作数之一的值

expr1 && expr2

If expr1 can be converted to true, returns expr2; else, returns expr1

 const a = 3; const b = -2; console.log(a > 0 && b > 0); // which returns false as expression `a > 0` true and b > 0 is `false` returned.

In your case在你的情况下

if filed = 'title', field === "title" && value will return value如果 filed = 'title', field === "title" && value将返回value

if filed = 'somethingelse', field === "title" && value will return false .如果 filed = 'somethingelse', field === "title" && value将返回false

As Serg mentioned, you can use ternary operator to fix your issue.正如 Serg 提到的,您可以使用三元运算符来解决您的问题。

this.$emit("input", {
   title: field === "title" ? value : "",
   content: field === "content" ? value : "",
});

Awesome Question真棒问题

Why don't we use this.value in child component?为什么我们不在子组件中使用 this.value? In your code, parent component passed v-model="blogPost" into child component, but in child component that is not used.在您的代码中,父组件将 v-model="blogPost" 传递给子组件,但在未使用的子组件中。

Try this尝试这个

In Child component:在子组件中:

 <template> <input type="text" @input="setField($event.target.value, 'title')" /> <wysiwyg-input @change="(text) => setField(text, 'content')" /> </template> <script> export default { props: { value: { type: Oject } }, methods: { setField(value, field) { this.$emit("input", { title: field === "title"? value: this.value.title, content: field === "content"? value: this.value.content, }); }, }, } </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VueJS如何从子组件向其父组件发出事件 - VueJS How to emit an event from a child component to its parent component 无法从Vue.js的子组件向父组件发出事件 - Not able to emit an event to a parent component from child component in Vuejs VueJS-自动将子组件中的父对象绑定到发射,为什么? - VueJS - Auto bind parent object in child component with emit, why? VueJs:如何使用 $emit(optionalPayload) 和 $on 将“计算函数”值从子组件传递到父组件? - VueJs: How to pass a 'computed function' value from child component to the parent component using $emit(optionalPayload) and $on? 如何在Vuejs中从子级向父级$ emit重置数据列表 - How to $emit from child to parent in Vuejs for reset data list VueJS - 使用参数从发射更新父值? - VueJS - Update parent values from emit with parameters? this.$emit 在 Vuejs 中将值从子组件传递给父组件时不起作用 - this.$emit not working while passing the value from child to parent component in Vuejs VueJs 2.0 将事件从孙子发送到他的祖父组件 - VueJs 2.0 emit event from grand child to his grand parent component 如何从父组件到子组件发射数据[Angular 2] - How to emit data from parent to child component [Angular 2] 如何在 Angular 中从子组件向父组件发出多个事件? - how to emit multiple events from child to parent component in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM