国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

javascript - vuex報錯 this.$store.dispatch is not a function,怎么解決?

瀏覽:95日期:2023-04-02 11:16:37

問題描述

之前對著vuex官方的例子抄了一遍以后,打算自己做個帶完整html,css的demo,帶更多完整的功能,然后基本都完成了,開始測試的時候就遇到了后臺報錯“this.$store.dispatch is not a function”javascript - vuex報錯 this.$store.dispatch is not a function,怎么解決?我搞了一下午都沒弄好,對著例子比對半天都沒看出問題,使用vue官方的chrome調試器知道state和getters是有引入所以state,就是actions沒獲得我模擬出來的數據javascript - vuex報錯 this.$store.dispatch is not a function,怎么解決?

相關代碼如下

//Product.vueimport { mapGetters, mapActions } from ’vuex’export default {...created () {this.$store.dispatch(’getAllDetails’) }}

//store/modules/product.jsimport shop from ’../../api/shop’import * as types from ’../mutation-types’const state = { all:[] }const actions = { getAllDetails({ commit }) {shop.getDetails( details => { commit(types.PRODUCT_DETAILS, { details })}) }}const mutations = { [types.PRODUCT_DETAILS] (state, { details }) {state.all = details }}export default { state, getters, actions, mutations}

//store/mutations-typesexport const PRODUCT_DETAILS = ’PRODUCT_DETAILS’

//shop.jsconst _details = [{ iPhone6S: { name: ’Apple/蘋果 iPhone 6S’, desc: ’3D Touch、1200萬像素照片、4k視頻,強大功能于一身。’, price: ’5288 - 6888’, style: {’銀色’: ’http://o8yu724qs.bkt.clouddn.com/iphone6s-silver-select-2015.png’,’深空灰色’: ’http://o8yu724qs.bkt.clouddn.com/iphone6s-gray-select-2015.png’,’金色’: ’http://o8yu724qs.bkt.clouddn.com/iphone6s-gold-select-2015.png’,’玫瑰金色’: ’http://o8yu724qs.bkt.clouddn.com/iphone6s-rosegold-select-2015.png’ }, activeStyleUrl: ’http://o8yu724qs.bkt.clouddn.com/iphone6s-silver-select-2015.png’, size: {’16GB’: 5288,’64GB’: 6088,’128GB’: 6888 } }}]export default { getDetails (cb) {console.log(cb)return cb(_details) }}

如果大佬們有時間,或者覺得我截取的片段不能說明問題,可以到github中下載完整版本調試,先謝謝大佬們了。

問題解答

回答1:

修改main.js中

import * as store from ’./store’

為import store from ’./store’

回答2:

//Product.vueimport { mapGetters, mapActions } from ’vuex’export default {...created () {this.$store.dispatch(’getAllDetails’) }}

要先在你這個代碼里面引入store,import store from ’./store’

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 长岛县| 嘉祥县| 皮山县| 平南县| 桃江县| 通榆县| 莎车县| 集安市| 兴国县| 舒兰市| 灯塔市| 长岭县| 铁力市| 澜沧| 泸州市| 荥阳市| 云和县| 碌曲县| 青川县| 乳山市| 澄江县| 秦皇岛市| 永泰县| 莱西市| 米脂县| 教育| 潢川县| 黄梅县| 肥东县| 沽源县| 潢川县| 宿迁市| 新昌县| 本溪市| 西和县| 沅江市| 连山| 郁南县| 星座| 铁岭市| 肇源县|