Get city components
city: {
template: '#city_template',
data: function () {
return {
cityData: cityData,//城市数据
selectedOptions:[24,81],//被选中的城市
list:[],
}
},
mounted: function () {
this.getCity()
},
methods: {
getCity: function () {
var self = this
$.ajax({
url:'test.json',
success:function (res) {
self.$set(self.$data, 'cityData', res)
}
})
}
}
},
A page may have multiple such components. In this case, multiple ajax requests will be requested. In fact, the data is the same. Is there a way to only request asynchronous data once, and then use multiple such components? The same data? I tried defining ajax externally and assigning it to cityData, so the data cannot be updated directly
PHP中文网2017-05-19 10:18:17
test.json is a file of yours. You can change it to the format of a .js file, and then directly change it to a variable. Obj={}; won’t it be obtained? Modular ones are also available.
ringa_lee2017-05-19 10:18:17
Look at your city component, city.data().cityData is the result set you want
曾经蜡笔没有小新2017-05-19 10:18:17
Manage with Vuex
Maintain this value on the common parent component, pass it to the child component through props, and the child component notifies the parent component of ajax through $emit
某草草2017-05-19 10:18:17
Finally, we used asynchronous components
Vue.component(ele,function (resolve, reject) {
getCity().then(function (data) {
resolve(comInherit.extend(obj[ele]));
}).catch(function (error) {
alert(error)
})
});
淡淡烟草味2017-05-19 10:18:17
To share data between multiple components, you can use vuex or define a global one yourself