最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - vue.js : error unknown action type? - Stack Overflow

matteradmin2PV0评论

I created my store store/user.js

export const state = () => ({
      user: {},
    });
export const mutations = {

};
export const actions = {
  AUTH ({mit},{email, password}){
console.log('email, password =', email, password)
  }
};

export const getters = {};

ponent:

<template>
<form @submit.prevent="AUTH(model)">
  <input type="text"  required v-model.lazy = "model.email">
    <input type="password" required v-model.lazy = "model.password" >
</template>


<script>
  import { mapActions } from 'vuex'

  export default {

    data() {
      return {
        model:{
          email:" " ,
          password:" "

      }

      }
    },
    methods: {
      ...mapActions(['AUTH']),
}
}

In my ponent , I am trying to execute a vuex action from a module, but I am getting an error, even if this action is defined :

unknown action type: AUTH,

I don't have any idey about problem.

index.js

import Vue from 'vue'
import Vuex from 'vuex'

import user from './modules/user.js'

Vue.use(Vuex);

const store = new Vuex.Store({
  modules: {
    user
  }
})

I created my store store/user.js

export const state = () => ({
      user: {},
    });
export const mutations = {

};
export const actions = {
  AUTH ({mit},{email, password}){
console.log('email, password =', email, password)
  }
};

export const getters = {};

ponent:

<template>
<form @submit.prevent="AUTH(model)">
  <input type="text"  required v-model.lazy = "model.email">
    <input type="password" required v-model.lazy = "model.password" >
</template>


<script>
  import { mapActions } from 'vuex'

  export default {

    data() {
      return {
        model:{
          email:" " ,
          password:" "

      }

      }
    },
    methods: {
      ...mapActions(['AUTH']),
}
}

In my ponent , I am trying to execute a vuex action from a module, but I am getting an error, even if this action is defined :

unknown action type: AUTH,

I don't have any idey about problem.

index.js

import Vue from 'vue'
import Vuex from 'vuex'

import user from './modules/user.js'

Vue.use(Vuex);

const store = new Vuex.Store({
  modules: {
    user
  }
})
Share edited Jul 20, 2018 at 2:07 Лена Фролова asked Jul 20, 2018 at 1:58 Лена ФроловаЛена Фролова 991 gold badge3 silver badges13 bronze badges 6
  • Can you provide your code which creates the store? Are you using vuex modules? – Decade Moon Commented Jul 20, 2018 at 2:01
  • Because you're using modules, it'll be namespaced. See this answer. – Decade Moon Commented Jul 20, 2018 at 2:06
  • [nuxt] store/index.js should export a method which returns a Vuex instance. i get err – Лена Фролова Commented Jul 20, 2018 at 2:09
  • When using modules mode in Nuxt, you don't create the store yourself. Instead, export the state, getters, mutations and actions. – Brian Lee Commented Jul 20, 2018 at 2:12
  • i use nuxt . how to do it nuxt? – Лена Фролова Commented Jul 20, 2018 at 2:12
 |  Show 1 more ment

1 Answer 1

Reset to default 9

You need to use createNamespacedHelpers:

import { createNamespacedHelpers } from 'vuex'

const { mapState, mapActions } = createNamespacedHelpers('users')

Binding helpers with namespace

Otherwise, the mapping helpers need the full module namespace:

...mapActions([
  'users/AUTH'
])

// if you are only using one module in the ponent
...mapActions('users', [
  'AUTH'
])

Nuxt

You're mixing classic and modules mode. When using modules mode, Nuxt creates the store instance from the index.js file. You simply export the state, getters, mutations and actions. State should be exported as a function:

export const state = () => ({
  foo: 0,
  bar: 1
})

Any file within the store directory will be considered a module and Nuxt will automatically register it as a namespaced module.

- store
-- index.js // the store
-- users.js // module 'users'
-- foo.js // module 'foo'

The users module looks otherwise correct.

Make the following changes to your ponent:

// template
<form @submit.prevent="submitForm">

// script
methods: {
    ...mapActions({
         auth: 'users/AUTH'
    }),
    submitForm () {
        this.auth(this.model)
    }
}
Post a comment

comment list (0)

  1. No comments so far