如何从 google api 文档中的数据制作 vuex getter?

How to make vuex getter from data from google api docs?

我正在尝试使用来自 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
            },
          ]
        }
      }
    ],
} 

然后我做了一个 getter message 并使用了来自 lodash 的 map:

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

但是当我在 vuex 中检查 message 时,它说未定义...我想要包含 textRun 对象的数组。问题可能在哪里?

您需要 return 您的 getter 中的 message 变量。

请问这样能不能把message keep in Array? 你可以这样写你想要的

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