注:在程序中使用的數據導致我的麻煩來自外部,所以我很抱歉無法提供一個完整的工作示例。請讓我知道,如果缺少了什麼,我加入我beive的所有信息是相關的理解上下文爲什麼計算的屬性返回undefined?
我Vue的實例有一個computed
財產是依賴於一個AJAX調用數據帶來。它所做的就是將這些返回的數據(因爲它)轉換成Vue屬性(下面的allcases
),以便在HTML代碼中重用。
從我的Vue實例的computed
部分:
computed: {
allcases: function() {
console.log('getting all cases')
var request = $.ajax({
(...) <- a working AJAX call here, please see below
})
.done(function(msg) {
console.log('found: ' + JSON.stringify(msg.hits.hits));
return msg.hits.hits;
})
.fail(function(jqXHR, msg) {
console.log('error posting incident: ' + msg);
});
}
}
當運行這個我在控制檯上看到
found: {"_index":"incidents","_type":"incidents","_id":"AVxeaaE5n3UYRVv5wrrJ","_score":1,"_source":{"time":"2017-05-31T14:09:22+02:00","lastname":"ert","firstname":"ertr"}},{"_index":"incidents","_type":"incidents","_id":"AVxjykjeqc2UmzuwNYsy","_score":1,"_source":{"time":"2017-06-01T15:13:40+02:00","lastname":"hello2","firstname":null}},{"_index":"incidents","_type":"incidents","_id":"AVxjx3TKqc2UmzuwNYsw","_score":1,"_source":{"time":"2017-06-01T15:10:34+02:00","lastname":"hello","firstname":"worldddd"}},{"_index":"incidents","_type":"incidents","_id":"AVxfOF-sRInTI31ZgCYt","_score":1,"_source":{"time":"2017-06-01T15:12:20+02:00","lastname":"hello","firstname":"worldddd"}},{"_index":"incidents","_type":"incidents","_id":"new incident","_score":1,"_source":{"time":"2017-06-01T15:12:41+02:00","lastname":"hello1","firstname":"ertert"}},{"_index":"incidents","_type":"incidents","_id":"AVxfN4wIRInTI31ZgCYs","_score":1,"_source":{"time":"2017-05-31T17:54:54+02:00","lastname":null,"firstname":null}},{"_index":"incidents","_type":"incidents","_id":"AVxjol5dRInTI31ZgCmI","_score":1,"_source":{"time":"2017-06-01T14:30:04+02:00","lastname":"hjkj","firstname":"hjkhjk"}},{"_index":"incidents","_type":"incidents","_id":"AVxjyKaFqc2UmzuwNYsx","_score":1,"_source":{"time":"2017-06-01T15:11:53+02:00","lastname":"hello","firstname":"world"}}]
所以return msg.hits.hits
前右的msg
(和msg.hits.hits
)的內容是否正確,內容是我所期望的。
本示例複製the basic one from the documentation。
allcases
然而undefined
當我運行腳本。
我知道這是因爲
-1-
-2-
的HTML文件有以下代碼
<div id="currentcases">
{{allcases}}
</div>
其中,在開發工具元素觀察時,是空的:
<div id="currentcases">
</div>
我真的不知道什麼是錯的這個代碼。
,因爲它不返回任何東西 – thanksd
@thanksd肯定'computed.allcases'仍然會解析爲函數本身? –
@thanksd:對不起,我不明白,請您詳細說明一下嗎? 'msg.hits.hits'是一個數組,這就是返回的結果 – WoJ