@ohwoow

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

У меня два компонента. В одном я получаю активные чекбоксы на товарах и их ID и добавлять это все в два массива (в одном выбранный товар, в другом его ID) Потом я это все передаю в другой компонент и делаю запрос к апи, в котором передаю ID выбранных товаров и добавляю в другой массив (devices) данные выбранных товары (цену и прочее). С добавлением все норм, но когда убираю галочку с инпута, то вместо удаления происходит добавление этого же элемента. Как это исправить?
async fetchDevice(id) {
      try {
        const response = await axios.get(`https://${id}`)
        this.devices.push(response.data.data)
        return response.data.data
      } catch (e) {
        console.error(e)
      }
    },

watch: {
    checkedInput: {
      async handler(val, oldVal) {
        if (val.length >= oldVal.length) {
          await this.fetchDevice(this.id)
          console.log(this.checkedInput)
        } else if (val.length == 0) {
          this.devices = []
        } else {
          await this.fetchDevice(this.id)
        }
        if (this.id == '') {
          this.devices = []
          this.devicesItem = []
        }
      },
      deep: true
    },

Здесь я получаю выбранные чекбоксы и Id товара
getActiveInput(event) {
        const arr = []
        const checkedInputs = document.querySelectorAll('.form-calc__input:checked')
        this.hardware.forEach((item) => {
          checkedInputs.forEach(input => {
            if (item.ID == input.id) {
              arr.push(item)
            }
          })

        })
        return this.$emit('checkedInput', arr)
      },
      getId(event) {
        let id = []
        const checkedInputs = document.querySelectorAll('.form-calc__input:checked')
        this.hardware.forEach((item) => {
          checkedInputs.forEach(input => {
            if (item.ID == input.id) {
              id.push(`sectionIds[]=${item.ID}&`)
            }
          })
        })
        return this.$emit('getId', id)
      },
  • Вопрос задан
  • 74 просмотра
Пригласить эксперта
Ваш ответ на вопрос

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

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