首页 > 解决方案 > 如何从谷歌 api 文档中的数据中制作 vuex getter?

问题描述

我正在尝试使用来自 google dosc api 的平面数据在 vuex 商店中制作 getter。我需要做的就是获取 textRun 内容并将其保存在数组中(因为消息很少)。现在我将这个响应硬编码为如下状态:

 state: {
    googleResponse: [
      {
        "body": {
          "content": [
            {
              "endIndex": 75,
              "paragraph": {
                "elements": [
                  {
                    "endIndex": 75,
                    "startIndex": 1,
                    "textRun": {
                      "content": "This is an ordinary paragraph. It is the first paragraph of the document",
                      "textStyle": {}
                    }
                  }
                ],
                "paragraphStyle": {
                  "direction": "LEFT_TO_RIGHT",
                  "namedStyleType": "NORMAL_TEXT"
                }
              },
              "startIndex": 1
            },
            {
              "endIndex": 102,
              "paragraph": {
                "elements": [
                  {
                    "endIndex": 102,
                    "startIndex": 75,
                    "textRun": {
                      "content": "Here's a level one heading",
                      "textStyle": {}
                    }
                  }
                ],
                "paragraphStyle": {
                  "direction": "LEFT_TO_RIGHT",
                  "headingId": "h.o1fkftgl5zwf",
                  "namedStyleType": "HEADING_1"
                }
              },
              "startIndex": 75
            },
          ]
        }
      }
    ],
} 

然后我做了一个吸气剂messagemap从lodash使用:

message: (state) => {
  let message = '';
  map(state.googleResponse, (element) => ({
    content: map(element.body.content, (content) => {
      map(content.paragraph.elements, (obj) => {
        message += get(obj, 'textRun', '')
      })
    })
  }))
}

但是当我签message入 vuex 时,它说未定义......我想拥有带有 textRun 对象的数组。问题可能出在哪里?

标签: javascriptvue.jsvuexgetter

解决方案


我想知道您是否可以通过这种方式将消息保存在数组中?你可以这样写

   let messageArray = state.googleResponse.map(
        item => item.body.content.map(
            itemCotent => itemCotent.paragraph.elements.map(
                itemElements => itemElements.textRun.content)))


推荐阅读