省市区选择的三级联动插件的使用

本文档详细介绍了如何在Vue项目中安装和使用v-distpicker组件。通过npm或yarn进行安装,并在main.js全局或局部注册。组件支持基础选择、预设值及移动端显示。同时提供了一个示例,展示如何监听和响应省市区选择变化,以及相应的CSS样式调整,以实现一个可展开收起的移动端选择器效果。
摘要由CSDN通过智能技术生成

注意:该方法只能用在vue的脚手架项目中,如果该项目是有npm安装,建议使用npm方式安装

安装

npm安装

npm install v-distpicker --save

yarn安装

yarn add v-distpicker --save

注册

main.js注册全局组件

import VDistpicker from 'v-distpicker'

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

在使用的vue组件里注册

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>

具体用法

html代码

<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>

vue代码

<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>

对应的css样式

<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>

最终的效果图

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值