如何从 Vue 实例读取数据



给定以下代码片段:

var title,description,lng,lat,identifier;
const details = new Vue({
el: '#detailsLOC',
data: {
resultsONE: [],
resultsImages: [],
GeoJsonONE: []
},
mounted() {
axios.all([
axios.get('/getJsonDetails'),
axios.get('/getJsonDetailsImages'),
]).then(axios.spread((response1,response2) => {
this.resultsONE = response1.data;
this.resultsImages = response2.data;
title = this.resultsONE.title;
description = this.resultsONE.preview_description;
lng = this.resultsONE.location_lng;
lat = this.resultsONE.location_lat;
identifier = this.resultsONE.identifier;
})).catch( e => {
console.log(e);
document.getElementById("app1").style.display = "none";
document.getElementById("app2").style.display = "none";
document.getElementById("app3").style.display = "";
});
}
});

我想从 resultsONE 传输/获取/读取数据并将其保存在 Vue 实例之外(并将其存储在我的例子中这些变量 lng,lat,title,... 中(。 两个axios.get都是有效的,并且完美地工作。 你能帮我吗?

你可以通过参考instance.$data来访问 vue 实例的data对象。
像这样的 vue instace:

var foo = new Vue({
el: "#app",
data() {
return {
bar: "inside data"
};
},

可以通过foo.$data.bar访问。

完整示例:https://codesandbox.io/s/4wwoxzow97

最新更新