城市选择插件 V-Distpicker 组件详解以及全套用法

首先第一步,这个插件是一定要在vue项目中使用的
一,安装
使用 npm 安装:

npm install v-distpicker --save

使用 yarn 安装

yarn add v-distpicker --save

二使用
1.在main.js中引入

import VDistpicker from 'v-distpicker'

Vue.component('v-distpicker', VDistpicker);

2.在你想要用这个插件的页面引入

	import VDistpicker from 'v-distpicker'

export default {

 components: { VDistpicker }

}

三.简单使用
基础

<v-distpicker></v-distpicker>

默认值

<v-distpicker province="广东省" city="广州市" area="海珠区"></v-distpicker>

移动端

<v-distpicker type="mobile"></v-distpicker>

四.直接使用到项目中(直接复制我下面的就可以啦)
1.获取选择的值

<template>
	      <div>
	      <button @click="choose">点我选择区域</button>
	      <p>您选择的城市为:<span>{{txt1}}</span><span>{{txt2}}</span><span>{{txt3}}</span></p>
	      <p class="pwrap" v-if="show">
	        <v-distpicker type="mobile" @province="onChangeProvince" @city="onChangeCity" @area="onChangeArea"></v-distpicker>
	      </p>
	    </div>
</template>

2.在你引用 v-distpicker 的父组件里面定义几个方法来获取选择的值。

<script>
	import VDistpicker from 'v-distpicker'
  export default {
    name: 'getAddress',
    components: { VDistpicker },
    data() {
      return {
        show:false,
        txt1:'',
        txt2:'',
        txt3:'',   
      }
    },
    methods: {
      choose(){
        this.show=!this.show
      },
      onChangeProvince(a){
        console.log(a) 
        this.txt1 = a.value + '-'
      },    
      onChangeCity(a){
        console.log(a)    
        this.txt2 = a.value + '-'    
      },
      onChangeArea(a){
        console.log(a)  
        this.txt3 = a.value
        this.show=false
      }      
    },
}
</script>

3.样式(官网给出的样式使用scss写的 如果你要用还得配scss,比较麻烦)

<style scoped>

  .pwrap{

    height: 400px;

    overflow-y: auto;

    position: fixed;

    left: 0;

    bottom: 0;

    width: 100%;

  }

  .pwrap>>>.distpicker-address-wrapper{

    color: #999;

  }

  .pwrap>>>.address-header{

    position: fixed;

    bottom: 400px;

    width: 100%;

    background: #000;

    color:#fff;

  }

  .pwrap>>>.address-header ul li{

    flex-grow: 1;

    text-align: center;

  }

  .pwrap>>>.address-header .active{

    color: #fff;

    border-bottom:#666 solid 8px

  }

  .pwrap>>>.address-container .active{

    color: #000;

  }

</style>

用我这个写出来的取值有点问题,就是第一遍选完之后接着选第二遍的时候第一遍的没有清空,欢迎大神有更好的修改方案
最后附上我参考的原链接:
希望可以帮到大家!!!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值