0

I just want to update Vue X store when I click on add button called "Ajouter" but my browser crashes and I should open it again and again.

I already use store in my project and I'm not able to reproduce this issue with another state

I got this issue in production and not in development

I'm using this dependencies :

  • "core-js": "^3.8.3",
  • "vue": "^3.2.13",
  • "vue-class-component": "^8.0.0-0",
  • "vue-router": "^4.0.3",
  • "vuex": "^4.0.0"

State

export default {
  selectedFoods: [],
  currentFood: {},
  nutrients: {
    energy: 0,
    protein: 0,
    lipid: 0,
    carbohydrate: 0
  },
  activeFilters: {
    energy: false,
    protein: false,
    lipid: false,
    carbohydrate: false
  }
}

I would like to update selectedFoods

Mutation

SET_FOOD_QUANTITY (state: OrganizeMealState, param: SelectedFood) {
    const selectedFoodsLength = Object.values(state.selectedFoods).length
    if (selectedFoodsLength > 0) {
      for (let index = 0; index < selectedFoodsLength; index++) {
        const food = state.selectedFoods[index]
        if (food.food.name === param.food.name) {
          state.selectedFoods[index].quantity = param.quantity
          return
        }
      }
    }

    // ===> crash here
    state.selectedFoods.push({
      food: param.food,
      quantity: param.quantity
    })
  }

I can't push data into selectedFoods because browser crash

Action

setFoodQuantity ({ commit }: { commit: Commit }, param: SelectedFood) {
  commit('SET_FOOD_QUANTITY', param)
},

Component

<template>
  <div class="modal fade" id="modal-quantity" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="modal-portion-label" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="modal-quantity-label">Quantité (g)</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" @click="cleanCurrentFood()"></button>
        </div>
        <div class="modal-body">
            <form>
                <div class="mb-1">
                  <input type="number" class="form-control" v-model="quantity">
                </div>
            </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @click="cleanCurrentFood()">Fermer</button>
          <button type="button" class="btn btn-danger">Réinitialiser</button>
          <button type="button" class="btn btn-primary" @click="addQuantity()">Ajouter</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component'
import { mapActions, mapGetters } from 'vuex'

import { Food } from '../../domain/food'
import { SelectedFood, Nutrients } from '../../domain/store/OrganizeMealState'

@Options({
  computed: {
    ...mapGetters({
      currentFood: 'OrganizeMeal/currentFood',
      selectedFoods: 'OrganizeMeal/selectedFoods',
      nutrients: 'OrganizeMeal/nutrients'
    })
  },
  methods: {
    ...mapActions({
      setFoodQuantity: 'OrganizeMeal/setFoodQuantity',
      cleanCurrentFood: 'OrganizeMeal/cleanCurrentFood'
    }),

    addQuantity () {
      this.setFoodQuantity({
        food: this.currentFood,
        quantity: this.quantity
      })
    }
  }
})
export default class AddFoodQuantityModal extends Vue {
  currentFood!: Food
  selectedFoods!: SelectedFood[]
  nutrients!: Nutrients

  quantity = 0
}
</script>

Here is my modal

modal

If you need to see more code, I share you github project here : https://github.com/wyllisMonteiro/diet-helper

Use devtools result

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

const app = createApp(App).use(store).use(router).mount('#app')

// eslint-disable-next-line
// @ts-ignore: Unreachable code error
app.config.performance = true

and got this

error

WHelp
  • 69
  • 1
  • 5
  • Does it crash if you build it locally? Tried hosting it somewhere else? – kissu Aug 27 '22 at 16:29
  • Sounds like you've got a never ending loop. Either try using the native debugger, use the VueX tab of the [Vue dev tools](https://devtools.vuejs.org/), or just see which process is using up all the memory under the mem tab of your dev tools. – Alicia Sykes Aug 27 '22 at 18:30
  • @Lissy93 I can't use this tool in production and in development it work's well – WHelp Aug 27 '22 at 18:45
  • @kissu if I use npm run serve, in local it work's and I try to publish to another hosting provider and go the same issue – WHelp Aug 27 '22 at 19:00
  • Isn't serve for a local dev server? I'm speaking about building a production version and then launching that one locally. Also, you can enable devtools on production via a setting. – kissu Aug 27 '22 at 22:36
  • @kissu with your first solution I got the same issue. However trying to use devtools on production I found a solution in this link : https://stackoverflow.com/questions/43781351/vue-js-is-detected-on-this-page-devtools-inspection-is-not-available-because-it. I should replace my build command in package.json `vue-cli-service build` to `vue-cli-service build --mode=development`. Can I have an explanation ? – WHelp Aug 30 '22 at 18:57
  • This is probably not the way to go since it will push a dev build to production, can't recommend that solution. Check the most upvoted one. – kissu Aug 30 '22 at 23:17
  • It's not worked for me and this post is about use vue devtools but it's not my issue. When I try to put Vue.config.devtools = true in main.ts but I got "Property 'config' does not exist on type 'typeof import" – WHelp Aug 31 '22 at 16:19
  • Not sure how to fix that one (you need to import a type there) but at the end, you could just ignore the TS warning/error. Also, using some devtools in production is still the most relevant approach I can think of to debug what you posted. We can't do magic and have to use what you're sharing us. Otherwise, you'll need to go the good old way of commenting out parts of your code and see what breaks it. Tried pushing a brand new project to your host? Can you replicate the issue? – kissu Aug 31 '22 at 16:46
  • I would like to use devtools but for 2 hours I tried to use it in production mode but I'm not able to use it. I tried to follow this : https://stackoverflow.com/questions/63628677/how-to-enable-devtools-in-vue-3-with-typescipt-in-development-mode, but in console I got Uncaught TypeError: Cannot set properties of undefined (setting 'performance'), but in doc it's available : https://vuejs.org/api/application.html#app-config-performance ? Do you have code to share me to enable devtools in production ? – WHelp Aug 31 '22 at 18:31
  • I updated question in part `Use devtools result` – WHelp Aug 31 '22 at 18:36

0 Answers0