vue-city-picker
vue-cli
初始化webpack工程若是没有安装vue-cli
的同窗请走 传送门javascript
在vue-city-picker
同级目录下执行css
vue init webpack vue-city-picker
复制代码
接下来出现的提示能够参考下图 html
cd vue-city-picker
npm npm install amaze-vue --save
npm install
复制代码
npm run dev
复制代码
此时在浏览器中访问http://127.0.0.1:8080/
就能够访问了vue
src/assets
src/components
src/App.vue
将提示准备好的location.js
文件拷贝到src
目录下。 同窗能够根据本身的状况本身拷入城市数据,代码里提供的数据仅供参考。java
src/main.js
引入amaze-vue组件库import Vue from 'vue'
import App from './App'
import AmazeVue from 'amaze-vue';
import 'amaze-vue/dist/amaze-vue.css';
Vue.config.productionTip = false
Vue.use(AmazeVue);
/* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})
复制代码
src/App.vue
<template>
<div class="container">
<section class="city-picker">
<am-select @select="proviceHandle" :options="province" search placeholder="请选择省、直辖市"></am-select>
<am-select v-if="city.length > 0" @select="cityHandle" search :options="city" placeholder="请选择市、区"></am-select>
<am-select v-if="county.length > 0" @select="countyHandle" search :options="county" placeholder="请选择区、县"></am-select>
</section>
<p v-if="address">您选择的是:<span class="am-text-success">{{ address }}</span></p>
</div>
</template>
<script>
import locationData from './location';
export default {
name: 'city-picker',
data() {
const province = [];
for (let code in locationData) {
let item = locationData[code];
province.push(Object.assign(item, {
label: item.name
}));
}
return {
province,
city: [],
county: [],
selectProvince: null,
selectCity: null,
selectCounty: null
};
},
methods: {
proviceHandle(value) {
const city = [];
for (let code in value.cities) {
let item = value.cities[code];
city.push(Object.assign(item, {
label: item.name
}));
}
this.city = city;
this.county = [];
this.selectProvince = value.name;
this.selectCity = null;
this.selectCounty = null;
},
cityHandle(value) {
const county = [];
for (let code in value.districts) {
let item = value.districts[code];
county.push({
code,
label: item,
name: item
});
}
this.county = county;
this.selectCity = value.name;
this.selectCounty = null;
},
countyHandle(value) {
this.selectCounty = value.name;
}
},
computed: {
address() {
const { selectProvince, selectCity, selectCounty } = this;
return (selectProvince ? selectProvince : '') +
(selectCity ? ',' + selectCity : '') +
(selectCounty ? ',' + selectCounty : '');
}
}
}
</script>
<style>
.container {
width: 800px;
height: 800px;
padding-top: 80px;
margin: auto;
}
.city-picker {
margin-bottom: 20px;
}
</style>
复制代码
https://github.com/sunshineJi/vue-city-picker
webpack
git clone https://github.com/sunshineJi/vue-city-picker.git
cd vue-city-picker
npm i
npm run dev
复制代码
此demo只是提供一个思路去解决联动选择的问题,线上需求还请使用的同窗根据具体状况优化代码后使用。git
amaze-vue是一只基于amazeui 和 vue.js的响应式组件库,项目刚刚起步,但愿你们多多支持。github
amaze-vueweb
documentvue-cli