詳解Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
現(xiàn)在大部分電商的網(wǎng)站、app都需要用戶或者管理者去選擇設(shè)置地區(qū)等位置信息。下面我就介紹一下前端開(kāi)發(fā)者用vue,axios,element-ui開(kāi)發(fā)一個(gè)省市區(qū)三級(jí)聯(lián)動(dòng)的組件。
1.準(zhǔn)備工作,首先我們需要全中國(guó)的省市區(qū)資源的json數(shù)據(jù)(科普一下:前六位數(shù)字是身份證前六位)
2.搭建vue-cli,安裝axios,element-ui,創(chuàng)建vue,webpack項(xiàng)目
1).
在控制臺(tái)或者終端執(zhí)行以下代碼,其中只需要路由(y),其他e2e,eslint這些不需要(y)
vue init webpack threelink
cd threelink
npm run dev
把沒(méi)用的組件刪除,重新創(chuàng)建組件
npm install axios --save ,安裝axios
npm i element-ui -S(這是縮寫) , 安裝element-ui
2).
在項(xiàng)目threelink->src->main.js里面添加,調(diào)用element-ui插件得代碼
// 加載element_ui import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI)
3).在static下創(chuàng)建json文件夾,文件夾里面放map.json,就是全國(guó)地址的數(shù)據(jù)信息,目錄結(jié)構(gòu)如下
3.基本步驟都已經(jīng)ok,下面我們開(kāi)始寫前端界面代碼.
上element-ui官網(wǎng),找到選擇器select,這里我們就不多說(shuō)了,瘋狂地復(fù)制粘貼,寫css樣式就行了。粘貼完修改完之后的樣子就是這個(gè)樣子了,
別著急,一會(huì)開(kāi)始上代碼!
4.首先我們要知道,當(dāng)我們選擇之后數(shù)據(jù)才會(huì)變化的,所以要給select綁定change事件。
我們仔細(xì)閱讀了element-ui的select文檔之后發(fā)現(xiàn),v-model的值為當(dāng)前被選中的el-option的 value 屬性值。
5.template組件里面的代碼?。?!
<div class="linkage"> <el-select v-model="sheng" @change="choseProvince"http://這個(gè)change事件是select的,不是option的,別寫在option里面 placeholder="省級(jí)地區(qū)"> <el-option v-for="item in province" :key="item.id" :label="item.value" :value="item.id"> </el-option> </el-select> <el-select v-model="shi" @change="choseCity" placeholder="市級(jí)地區(qū)"> <el-option v-for="item in shi1" :key="item.id" :label="item.value" :value="item.id"> </el-option> </el-select> <el-select v-model="qu" @change="choseBlock" placeholder="區(qū)級(jí)地區(qū)"> <el-option v-for="item in qu1" :key="item.id" :label="item.value" :value="item.id"> </el-option> </el-select> </div>
script標(biāo)簽里面的代碼!!
這里主要的難點(diǎn)就是,找到j(luò)son數(shù)據(jù)的特點(diǎn)!
像直轄市這些沒(méi)有市區(qū)的,默認(rèn)省份=市區(qū),例如:省份:天津。城市:天津。區(qū)縣:。。/
我們可以找到數(shù)據(jù)之間的規(guī)律自己手動(dòng)增加數(shù)據(jù)信息,如果要返回給后端,那么就拿出來(lái)他的前面的位置參數(shù),或者是后面的未知參數(shù)!!
例如:
按正常來(lái)講,是沒(méi)有紅色圈出來(lái)的那個(gè)參數(shù)的,這是我按照正確的規(guī)律手動(dòng)后添加的!!!為了滿足直轄市而添加
一定要記得手動(dòng)添加的數(shù)據(jù)是無(wú)效的?。∵@個(gè)只是為了自己編碼方便才添加的!手動(dòng)添加的那條數(shù)據(jù),不要返給后端,要找到它前面的數(shù)據(jù),找到真實(shí)數(shù)據(jù)!
import axios from 'axios' export default { data () { return { mapJson:'../static/json/map.json', province:'', sheng: '', shi: '', shi1: [], qu: '', qu1: [], city:'', block:'', } }, methods:{ // 加載china地點(diǎn)數(shù)據(jù),三級(jí) getCityData:function(){ var that = this axios.get(this.mapJson).then(function(response){ if (response.status==200) { var data = response.data that.province = [] that.city = [] that.block = [] // 省市區(qū)數(shù)據(jù)分類 for (var item in data) { if (item.match(/0000$/)) {//省 that.province.push({id: item, value: data[item], children: []}) } else if (item.match(/00$/)) {//市 that.city.push({id: item, value: data[item], children: []}) } else {//區(qū) that.block.push({id: item, value: data[item]}) } } // 分類市級(jí) for (var index in that.province) { for (var index1 in that.city) { if (that.province[index].id.slice(0, 2) === that.city[index1].id.slice(0, 2)) { that.province[index].children.push(that.city[index1]) } } } // 分類區(qū)級(jí) for(var item1 in that.city) { for(var item2 in that.block) { if (that.block[item2].id.slice(0, 4) === that.city[item1].id.slice(0, 4)) { that.city[item1].children.push(that.block[item2]) } } } } else{ console.log(response.status) } }).catch(function(error){console.log(typeof+ error)}) }, // 選省 choseProvince:function(e) { for (var index2 in this.province) { if (e === this.province[index2].id) { console.log(this.province[index2].id)//你選擇的省級(jí)編碼 console.log(this.province[index2].value)//省級(jí)編碼 對(duì)應(yīng)的漢字 this.shi1 = this.province[index2].children this.shi = this.province[index2].children[0].value this.qu1 =this.province[index2].children[0].children this.qu = this.province[index2].children[0].children[0].value this.E = this.qu1[0].id } } }, // 選市 choseCity:function(e) { for (var index3 in this.city) { if (e === this.city[index3].id) { this.qu1 = this.city[index3].children this.qu = this.city[index3].children[0].value this.E = this.qu1[0].id // console.log(this.E) } } }, // 選區(qū) choseBlock:function(e) { this.E=e; // console.log(this.E) }, }, created:function(){ this.getCityData() } }
7.效果圖
8.OK我個(gè)人感覺(jué)效果還不錯(cuò),也不卡
github項(xiàng)目地址:https://github.com/LLJJTT/threelink
以上所述是小編給大家介紹的Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Vue自定義省市區(qū)三級(jí)聯(lián)動(dòng)
- Vue實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
- 基于Vue2實(shí)現(xiàn)簡(jiǎn)易的省市區(qū)縣三級(jí)聯(lián)動(dòng)組件效果
- 使用vue2實(shí)現(xiàn)帶地區(qū)編號(hào)和名稱的省市縣三級(jí)聯(lián)動(dòng)效果
- Vue2仿淘寶實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
- vue.js模仿京東省市區(qū)三級(jí)聯(lián)動(dòng)的選擇組件實(shí)例代碼
- vue mint-ui 實(shí)現(xiàn)省市區(qū)街道4級(jí)聯(lián)動(dòng)示例(仿淘寶京東收貨地址4級(jí)聯(lián)動(dòng))
- vue省市區(qū)三聯(lián)動(dòng)下拉選擇組件的實(shí)現(xiàn)
- vue基于element-china-area-data插件實(shí)現(xiàn)省市區(qū)聯(lián)動(dòng)
相關(guān)文章
vue項(xiàng)目npm?run?build打包dist文件及打包后空白解決辦法
npm run build 這個(gè)命令會(huì)執(zhí)行Vue CLI中預(yù)定義的打包配置,并將打包后的文件存放在"dist"文件夾中,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目npm?run?build打包dist文件及打包后空白的解決辦法,需要的朋友可以參考下2023-10-10vue3+vite assets動(dòng)態(tài)引入圖片的三種方法及解決打包后圖片路徑錯(cuò)誤不顯示的問(wèn)題
這篇文章主要介紹了vue3+vite assets動(dòng)態(tài)引入圖片的幾種方式,解決打包后圖片路徑錯(cuò)誤不顯示的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03vue項(xiàng)目不能使用localhost訪問(wèn)的解決
這篇文章主要介紹了vue項(xiàng)目不能使用localhost訪問(wèn)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue踩坑記錄:屬性報(bào)undefined錯(cuò)誤問(wèn)題
這篇文章主要介紹了vue踩坑記錄:屬性報(bào)undefined錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04使用vue/cli出現(xiàn)defineConfig?is?not?function錯(cuò)誤解決辦法
這篇文章主要給大家介紹了關(guān)于使用vue/cli出現(xiàn)defineConfig?is?not?function錯(cuò)誤的解決辦法,當(dāng)我們?cè)谧龃虬渲玫臅r(shí)候,出現(xiàn)了這個(gè)錯(cuò)誤,需要的朋友可以參考下2023-11-11vue 使用自定義指令實(shí)現(xiàn)表單校驗(yàn)的方法
今天小編就為大家分享一篇vue 使用自定義指令實(shí)現(xiàn)表單校驗(yàn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08