前端Vue自定义简单实用中国省市区三级联动选择器

随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随意的进行组合。大大提升开发效率低,降低维护成本。

组件化对于任何一个业务场景复杂的前端应用以及经过多次迭代之后的产品来说都是必经之路。组件化要做的不仅仅是表面上看到的模块拆分解耦,其背后还有很多工作来支撑组件化的进行,例如结合业务特性的模块拆分策略、模块间的交互方式和构建系统等等 。

本文给大家介绍的组件是:前端Vue自定义简单实用中国省市区三级联动选择器,

效果图如下:

84f87f22d8393a74bb6ffd743dd58be9.png

a1c1d34431d9f573429bfb9a11aea4cf.png

34b20b7e6ae02a555fba8f907b94a842.png

#### 使用方法

```使用方法

<!-- themeColor:主题颜色 ref:设置唯一ref pickerValueDefault:默认选择 @onCancel:取消事件 @onConfirm:确认事件 -->

<cc-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault"

@onCancel="onCancel" @onConfirm="onConfirm"></cc-city-picker>

```

#### HTML代码实现部分

```html

<template>

<view class="content">

<form @submit="formSubmit" @reset="formReset">

<!-- leftTitle:左边标题 name:输入框名字 value:输入框选择值  placeholder:占位符 @click:点击事件-->

<ccInputSelView leftTitle="收获地址" name="location" :value="pickerText" placeholder="请选择位置"

@click="showMulLinkageThreePicker"></ccInputSelView>

<ccInputView leftTitle="详细地址" name="address" :value="address" placeholder="请输入详细地址">

</ccInputView>

<!-- themeColor:主题颜色 ref:设置唯一ref pickerValueDefault:默认选择 @onCancel:取消事件 @onConfirm:确认事件 -->

<cc-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValueDefault"

@onCancel="onCancel" @onConfirm="onConfirm"></cc-city-picker>

<view class="uni-btn-v">

<button class="botBtn" type="primary" form-type="submit">下一步</button>

<view class="tipText"> 注意事项: 请确保您填写的房屋信息真实无误 </view>

</view>

</form>

</view>

</template>

<script>

import ccInputSelView from '../../components/cc-inputSelView/ccInputSelView.vue'

import ccInputView from '../../components/cc-inputSelView/ccInputView.vue'

export default {

components: {

ccInputSelView,

ccInputView

},

data() {

return {

cityPickerValueDefault: [0, 0, 1],

themeColor: '#0BBBEF',

pickerText: '',

address: ''

}

},

methods: {

// 三级联动选择

showMulLinkageThreePicker() {

this.$refs.mpvueCityPicker.show()

},

onConfirm(e) {

this.pickerText = e.label

},

onCancel(e) {

console.log(e)

},

formSubmit: function(e) {

console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e));

var formdata = e.detail.value;

uni.showModal({

title: '温馨提示',

content: 'formsubmit事件携带数据为:' + JSON.stringify(e.detail.value)

})

},

}

}

</script>

<style>

.content {

display: flex;

flex-direction: column;

}

.uni-btn-v {

width: 100%;

height: auto;

}

.botBtn {

width: 90%;

margin-top: 36px;

}

.tipText {

width: 100%;

margin-left: 0px;

text-align: center;

color: #666666;

margin-top: 36px;

margin-bottom: 36px;

font-size: 28rpx;

}

</style>

```

 阅读全文下载完整组件代码请关注微信公众号: 前端组件开发

d848d5658a07453c843277846948c608.png

  欢迎加入我们的前端组件学习交流群,可添加群主微信,审核通过后入群。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue.js是一个流行的JavaScript框架,用于构建用户界面。要实现省市县三级联动选择器,你可以使用Vue.js结合一些插件或组件来实现。 下面是一种常见的实现方法: 1. 安装所需的插件或组件: - vue-select:用于选择省、市和县的下拉菜单。 - axios:用于发送HTTP请求,获取省市县数据。 2. 创建一个Vue组件: ```html <template> <div> <select v-model="selectedProvince" @change="getCities"> <option value="">请选择省份</option> <option v-for="province in provinces" :key="province.id" :value="province.id">{{ province.name }}</option> </select> <select v-model="selectedCity" @change="getCounties"> <option value="">请选择城市</option> <option v-for="city in cities" :key="city.id" :value="city.id">{{ city.name }}</option> </select> <select v-model="selectedCounty"> <option value="">请选择县/区</option> <option v-for="county in counties" :key="county.id" :value="county.id">{{ county.name }}</option> </select> </div> </template> <script> import axios from 'axios'; export default { data() { return { selectedProvince: '', selectedCity: '', selectedCounty: '', provinces: [], cities: [], counties: [], }; }, mounted() { this.getProvinces(); }, methods: { getProvinces() { axios.get('/api/provinces') .then(response => { this.provinces = response.data; }) .catch(error => { console.error(error); }); }, getCities() { axios.get(`/api/cities?provinceId=${this.selectedProvince}`) .then(response => { this.cities = response.data; this.selectedCity = ''; this.counties = []; }) .catch(error => { console.error(error); }); }, getCounties() { axios.get(`/api/counties?cityId=${this.selectedCity}`) .then(response => { this.counties = response.data; this.selectedCounty = ''; }) .catch(error => { console.error(error); }); }, }, }; </script> ``` 3. 根据实际情况修改接口地址和数据结构。 这是一个简单的示例,你可以根据自己的需求进行修改和扩展。同时,你还需要在后端实现相应的接口来获取省市县数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端组件开发

你的钟意将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值