No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Problem with mapMutation, error with "It's not a function"

    Help
    3
    5
    1339
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Samulwong
      Samulwong last edited by

      Hello guys,
      I generated a store with Cli named “shopping”, added state and mutation (with mutation-type)

      import { LocalStorage } from 'quasar';
      import {
        INIT_BUYCART,
        ADD_CART,
        GET_USERINFO,
        RECORD_USERINFO,
        SHOW_CART,
        REDUCE_CART,
        EDIT_CART,
      } from './mutation-types';
      
      export default {
        [INIT_BUYCART](state) {
          const initCart = LocalStorage.getItem('buyCart');
          if (initCart) {
            state.cartList = JSON.parse(initCart);
          }
        },
      

      So when I use them in Vue file with mapState and mapMutation, should I do it as follow?

        compouted: {
          ...mapState([
            'shopping/cartList', 'shopping/login', 'shopping/receiveInCart', 'shopping/userInfo',
          ]),
      
        methods: {
          ...mapMutations(['shopping/ADD_CART', 'shopping/INIT_BUYCART', 'shopping/SHOW_CART', 'shopping/REDUCE_CART', 'shopping/RECORD_USERINFO', 'EDIT_CART']),
      }
      

      But when I call it in mounted, it show error “TypeError: this.INIT_BUYCART is not a function”

        mounted() {
          if (this.login) {
            /* eslint no-underscore-dangle: 0 */
            this._getCartList();
          } else {
            // this.$store.commit('shopping/INIT_BUYCART'); this work. 
            this.INIT_BUYCART(); //Report TypeError: this.INIT_BUYCART is not a function in console.
          }
        },
      

      Can anyone help? Great thanks!

      1 Reply Last reply Reply Quote 0
      • metalsadman
        metalsadman last edited by

        @Samulwong ...mapMutation('shopping', ['INIT_BUY_CART']) is one way to do it, please refer to official vuex docs https://vuex.vuejs.org/.

        Samulwong 1 Reply Last reply Reply Quote 0
        • Samulwong
          Samulwong @metalsadman last edited by

          @metalsadman Thank you so much. I will try and read the docs more carefully. I didn’t understand the docs since it’s a little bit too simple (not mention about stores in sub-folder) and the vuex structure in quasar is a little bit different from plain vue.

          metalsadman 1 Reply Last reply Reply Quote 0
          • metalsadman
            metalsadman @Samulwong last edited by

            @Samulwong indeed, quasar cli quasar new store <store_name> just neatly separate states, getters, mutations, actions and config in their own separate js files.

            1 Reply Last reply Reply Quote 0
            • S
              srinivasgangji last edited by

              I am also running in to same issue. Looks like the mapped Vuex properties ( in this case mutation) is not available in Vue lifecycle methods.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post