欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)

 更新時(shí)間:2019年05月07日 08:33:04   作者:棋鬼王  
這篇文章主要介紹了Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

現(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)站的支持!

相關(guān)文章

最新評(píng)論