我正在尝试使用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
},
]
}
}
],
}
之后我制作了一个吸气剂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对象的数组。哪里可能是问题?
我想知道你是否可以通过这种方式将消息保存在Array中?你可以这样写,也许你想要的
let messageArray = state.googleResponse.map(
item => item.body.content.map(
itemCotent => itemCotent.paragraph.elements.map(
itemElements => itemElements.textRun.content)))
您需要在getter中返回message
变量。