首页 > 其他分享 >vue组件封装 - 省市县下拉选联动

vue组件封装 - 省市县下拉选联动

时间:2023-07-24 14:23:08浏览次数:46  
标签:province city vue 封装 area value addressParams 下拉选 const

改封装组件依赖element-china-area-data插件,引入组件可参照:https://www.npmjs.com/package/element-china-area-data

<!-- 
  * component:省市县下拉选联动
  * time:2023/7/19
  * author:zx

  * 使用方式:
  * import DialogAddress from "@/components/Dialog/dialogAddress.vue"; // 引入
  * components: { DialogAddress };  // 注册
  * this.$refs.refAddress.open = true;  // 打开弹框调用的方法
  * <dialog-address ref="refAddress"/>

  title:弹框显示标题, pageId:页面id,根据id查询历史操作记录
-->
<template>
  <div>
    <!-- 地址联系人 地址录入 -->
    <el-dialog title="选择地址" :visible.sync="open" width="30%" append-to-body>
      <el-form id="form" :model="addressParams" class="search-table" ref="queryForm" label-width="70px">
        <el-form-item label="省" prop="province">
          <el-select v-model="addressParams[conditionSet.province]" placeholder="请选择" @change="provinceChange"
            style="width:100%">
            <el-option v-for="item in provinceList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-select v-model="addressParams[conditionSet.city]" placeholder="请选择" @change="cityChange" style="width:100%">
            <el-option v-for="item in cityList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区" prop="area">
          <el-select v-model="addressParams[conditionSet.area]" placeholder="请选择" style="width:100%">
            <el-option v-for="item in areaList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="addressParams[conditionSet.address]"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="newAddress">生成新地址</el-button>
        <el-button @click="closeAddressDialog">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { regionData, CodeToText } from 'element-china-area-data'
export default {
  data() {
    return {
      // 地址录入弹窗
      open: false,
      addressParams: {
        province: '',
        city: '',
        area: '',
        address: '',
      },
      provinceList: regionData,
      cityList: [],
      areaList: []
    }
  },
  created() {
  },
  props: {
    // 查询条件字段设置
    conditionSet: {
      type: Object,
      default: () => {
        return {
          province: 'province',
          city: 'city',
          area: 'area',
          address: 'address',
        }
      }
    },
  },
  computed: {
  },
  methods: {
    // 地址录入
    addressEntry() {
      this.open = true
    },
    // 生成市级列表
    provinceChange(value) {
      this.cityList = []
      this.areaList = []
      this.addressParams.city = ''
      this.addressParams.area = ''
      console.log(this.provinceList)
      const province = CodeToText[value]
      if (province === '北京市' || province === '天津市' || province === '上海市' || province === '重庆市' ||
        province === '香港特别行政区' || province === '澳门特别行政区') {
        const city = { value: value, label: province }
        this.cityList.push(city)
      } else {
        const childList = this.provinceList.find(i => i.value === value).children
        this.cityList.push(...childList)
      }
    },
    // 生成区列表
    cityChange(value) {
      this.areaList = []
      this.addressParams.area = ''
      const city = CodeToText[value]
      if (city === '北京市' || city === '天津市' || city === '上海市' || city === '重庆市' ||
        city === '香港特别行政区' || city === '澳门特别行政区') {
        const childList = this.provinceList.find(i => i.value === value).children[0].children
        this.areaList.push(...childList)
      } else {
        const childList = this.cityList.find(i => i.value === value).children
        this.areaList.push(...childList)
      }
    },
    // 生成新地址
    newAddress() {
      if (this.addressParams.province === '北京市' || this.addressParams.province === '天津市' || this.addressParams.province === '上海市' || this.addressParams.province === '重庆市' ||
        this.addressParams.province === '香港特别行政区' || this.addressParams.province === '澳门特别行政区') {
        let addressText = this.addressParams.province + this.addressParams.area + this.addressParams.address
        this.$emit("newAddress", this.addressParams, addressText)
      } else {
        let addressText = this.addressParams.province + this.addressParams.city + this.addressParams.area + this.addressParams.address
        this.$emit("newAddress", this.addressParams, addressText)
      }
      this.closeAddressDialog()
    },
    // 关闭选择地址弹窗
    closeAddressDialog() {
      this.open = false
    },
  },
}
</script>
<style lang="less" scoped>
</style>

标签:province,city,vue,封装,area,value,addressParams,下拉选,const
From: https://www.cnblogs.com/axingya/p/17577098.html

相关文章

  • vue项目使用vue-virtual-scroll-list虚拟滚动超多千万条数据 cv可用案例
    当我们有大量数据需要显示在列表中时,传统的滚动列表会将所有数据渲染到DOM中,导致性能下降和内存占用增加。虚拟滚动列表通过仅渲染当前视窗内可见的一小部分数据,动态地根据滚动位置更新列表内容,从而实现更高效的列表渲染。vue-virtual-scroll-list是一个用于Vue.js的虚拟滚动......
  • 怎么看python有没有封装
    Python封装的概念和作用在面向对象编程中,封装是一种将数据和代码包含在一个单独的单元中的机制。通过封装,我们可以隐藏对象的内部实现细节,只暴露必要的接口供其他对象使用。Python是一种面向对象的编程语言,提供了良好的封装机制,使得我们可以更好地组织和管理代码。Python的封装主......
  • vue3.0 外部配置文件一 (导入json文件方式)
    vue3.0外部配置文件,重点是打包后也可以修改配置参数 注:js文件中必须是标准的json格式一、在public中创建static文件夹,在static文件夹中创建config.json  文件 config.json (必须是标准的json格式){"webSocketUrl":"ws://192.168.1.120:5011/chat/","......
  • android meidaplayer + surfaceview封装视频播放
    AndroidMediaPlayer+SurfaceView封装视频播放作为一名经验丰富的开发者,我将教会你如何使用AndroidMediaPlayer和SurfaceView来封装视频播放功能。下面是整个实现过程的步骤:步骤操作1添加权限和组件2初始化MediaPlayer3设置SurfaceView4设置资源......
  • vue 父向子通过props 传递一个function报未定义
    解决方法:参考资料:https://cloud.tencent.com/developer/ask/sof/523570来自为知笔记(Wiz)......
  • vue中ref的用法
    1,可以引用元素,通过this.$refs.domxx直接操作元素。<divref="domxx"></div>methods:{getxx(){console.log(this.$refs.domxx)//访问DOM元素}}2,可以绑定组件实例,访问组件的属性和方法;参考地址:(22条消息)Vue中ref的用法_我心向阳.的博客-CSDN博客Templ......
  • 一个nginx + vue下二级路径版本化方案
    过程说明:1、arg_appver表示读取url上appver参数2、对appver参数做变量映射得到alias_party1_test路径。具体条件:没有指定参数的话(即"")指向2.8.0版本化文件夹;默认的话(即default)则指向动态拼接的路径3、第2点里面动态拼接如果不需要版本化则先固定一个路径,如果需要则使用$arg_ap......
  • 图解 Vue 响应式原理
    Vue初始化模板渲染组件渲染为了便于理解,本文将从以下两个方面进行探索:从Vue初始化,到首次渲染生成DOM的流程。从Vue数据修改,到页面更新DOM的流程。Vue初始化先从最简单的一段Vue代码开始:"""<template><div> {{message}}</div></template><......
  • vue--dat41--scoped作用域
    1.scoped样式作用:让样式在局部生效防止冲突写法 <stylescoped>    </style> npmviewwebpackversions. 查看webpack的版本npmviewless-loaderversions查看less-loader版本npmiless-loader  安装less-loader......
  • vue--day40--plugins插件
    1.main.js/***该文件是整个项目的入口文件*///引入VueimportVuefrom'vue'//引入App组件他是所有组件的父组件importAppfrom'./App.vue'//引入插件importpluginsfrom'./plugins'//关闭vue的生产提示Vue.config.productionTip=false//应用插件Vue.us......