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