教程部分 getters 对象里面函数简写的疑问

getArticleById: (state) => (id) => {}getArticleById: (state,id) => {}

有什么区别吗?
为什么第二种会报错:
报错信息如下:

vue-router.esm.js?8c4f:2257 TypeError: store.getters.getArticleById is not a function
    at eval (index.js?a18c:27)
    at iterator (vue-router.esm.js?8c4f:2300)
    at step (vue-router.esm.js?8c4f:1947)
    at eval (vue-router.esm.js?8c4f:1948)
    at eval (vue-router.esm.js?8c4f:2322)
    at VueComponent.beforeRouteLeave (Create.vue?56af:52)
    at boundRouteGuard (vue-router.esm.js?8c4f:2450)
    at iterator (vue-router.esm.js?8c4f:2300)
    at step (vue-router.esm.js?8c4f:1947)
    at runQueue (vue-router.esm.js?8c4f:1955)

有点不懂,望赐教!

最佳答案

不简写的情况下,应该是这样的。 getArticleById 方法返回了一个函数。

    getArticleById: function (state)  {
        function getArticle(id) {
           // 方法体
        }
        return getArticle
    }

第一次简写,使用箭头函数

    getArticleById: (state) => {
        function getArticle(id) {
           // 方法体
        }
        return getArticle
    },

再次简写:

    getArticleById: (state) => {
        return (id) => {
            // 方法体
        }
    }

最后一次简写,使用箭头函数省略了 return

getArticleById: (state) => (id) => {
// 方法体
}
3年前 评论
爆炸青山绿水 (楼主) 3年前
讨论数量: 1

不简写的情况下,应该是这样的。 getArticleById 方法返回了一个函数。

    getArticleById: function (state)  {
        function getArticle(id) {
           // 方法体
        }
        return getArticle
    }

第一次简写,使用箭头函数

    getArticleById: (state) => {
        function getArticle(id) {
           // 方法体
        }
        return getArticle
    },

再次简写:

    getArticleById: (state) => {
        return (id) => {
            // 方法体
        }
    }

最后一次简写,使用箭头函数省略了 return

getArticleById: (state) => (id) => {
// 方法体
}
3年前 评论
爆炸青山绿水 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!