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
test.json就是你的一个文件,你可以改成.js文件的格式,然后直接改成变量,obj={};不就获取了?也可以用 模块化的。
看你的city组件,city.data().cityData就是你要的结果集
使用 Vuex 管理
在共同的父组件上维护这个值,通过 props 传到子组件,子组件通过 $emit 告知父组件 ajax
最后使用了异步组件的方式
多个组件共享数据可以用vuex 或者自己定义一个全局的