@Now159

Как перезаписать значения из дочернего компонента в родитель?

Мне нужно передать 3 переменные из дочернего компонента, то есть из модалки, в родитель и перезаписать их
Проблема в том, что данные не обновляются.
Наверх прокидываю emit, в родителе слушаю событие и перезаписываю дату.

Дочерний
<div @click="choice(item)" v-for="item in items" :key="item.id">

</div>

data: () => ({
    show: false,
    amount: null,
    choiceUserColor: "",
    choiceUserImg: "",
    choiceUserNumder: 0,

  }),
  methods: {
    choice(item) {
    this.choiceUserColor = item.title
    this.choiceUserImg = item.img
    this.choiceUserNumder = item.number
      console.log(this.choiceUserColor, this.choiceUserImg, this.choiceUserNumder)
      this.$emit('choiceUser',  this.choiceUserColor, this.choiceUserImg, this.choiceUserNumder )
    },
    methodClose() {
      this.show = false
    }

  }

Родитель
<modal-palette @choiceUser="adaa" :items="items" :name="name" ref="modal"/>

data: () => ({
    UserColor: "Violet",
    UserImg: "/temp/card-bg-12.svg",
    UserNumder: 522,

  }),
  methods: {
    adaa() {
      this.UserColor = this.choiceUserColor
      this.UserImg = this.choiceUserImg
      this.UserNumder = this.choiceUserNumder
    },
    openModal() {
      this.$refs.modal.show = true
    },
  }
  • Вопрос задан
  • 124 просмотра
Пригласить эксперта
Ответы на вопрос 3
yarkov
@yarkov Куратор тега Vue.js
Помог ответ? Отметь решением.
this.$emit('choiceUser', item)
adaa(selectedUser)
Ответ написан
@Now159 Автор вопроса
может быть не одно из самых лучших решений, но рабочее

дочерний:
<div @click="choice(item)" v-for="item in items" :key="item.id">

</div>

data: () => ({
    show: false,
    amount: null,
    choiceUserColor: "",
    choiceUserImg: "",
    choiceUserNumder: 0,

  }),
  methods: {
     this.choiceUserColor = item.title
      this.choiceUserImg = item.img
      this.choiceUserNumder = item.number
      console.log(this.choiceUserColor, this.choiceUserImg, this.choiceUserNumder)
      this.$emit('choiceUser', [this.choiceUserColor, this.choiceUserImg, this.choiceUserNumder])
    },
    methodClose() {
      this.show = false
    }

  }


Родитель:

<modal-palette @choiceUser="adaa" :items="items" :name="name" ref="modal"/>


data: () => ({
UserColor: "Violet",
UserImg: "/temp/card-bg-12.svg",
UserNumder: 522,

}),
methods: {
adaa(args) {
      this.args = args
      this.UserColor = this.args[0]
      this.UserImg = this.args[1]
      this.UserNumder = this.args[2]
      console.log(args)
},
openModal() {
this.$refs.modal.show = true
},
}
Ответ написан
Комментировать
@roaddd
Emit не принимает несколько параметров. Можно отправить только один параметр т.е в вашем случае объект
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы