[英]How to make vuex getter from data from google api docs?
我正在嘗試使用來自谷歌dosc api的平面數據在vuex存儲中獲取吸氣劑。 我需要做的就是獲取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
和使用的map
從lodash:
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
時, message
說是未定義的……我想擁有包含textRun對象的數組。 問題可能出在哪里?
我想知道您是否可以通過這種方式將消息保存在Array中? 你可以這樣寫
let messageArray = state.googleResponse.map( item => item.body.content.map( itemCotent => itemCotent.paragraph.elements.map( itemElements => itemElements.textRun.content)))
您需要在getter中返回message
變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.