注意:该方法只能用在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>