(编辑:jimmy 日期: 2024/11/18 浏览:2)
Coordinates组件实现
模板
一个span显示东经西经,三个输入框输入度分秒
<template> <div class="coordinates"> <!-- 经度 --> <div class="item"> <span class="itude" @click="itudeChange(true)">{{ longFlag | longitudeName }}</span> <el-input v-model.number="longitude[0]" @change="change(true,0)" size="mini"> <i slot="suffix">°</i> </el-input> <el-input v-model.number="longitude[1]" @change="change(true,1)" size="mini"> <i slot="suffix">′</i> </el-input> <el-input v-model.number="longitude[2]" @change="change(true,2)" size="mini"> <i slot="suffix">″</i> </el-input> </div> <!-- 纬度 --> <div class="item"> <span class="itude" @click="itudeChange(false)">{{ latFlag | latitudeName }}</span> <el-input v-model.number="latitude[0]" @change="change(false,0)" size="mini"> <i slot="suffix">°</i> </el-input> <el-input v-model.number="latitude[1]" @change="change(false,1)" size="mini"> <i slot="suffix">′</i> </el-input> <el-input v-model.number="latitude[2]" @change="change(false,2)" size="mini"> <i slot="suffix">″</i> </el-input> </div> </div> </template>
实现
props: 父组件传入的参数 value ,验证合法性 经度绝对值小于180,纬度绝对值小于90,数组长度为2
value: { //绑定的 value type: Array, require: true, validator: function (value) { let len = value.length > 0 && value.length === 2 let isvalid = Math.abs(value[0]) < 180 && Math.abs(value[1]) < 90 return len && isvalid }, default: function () { return [] } }
model: prop为 value 时不用实现 model 但是this.$emit(event,arg) 传入的event需要为 'input',这里要注意
model: { prop: 'value', event: 'input' },
v-model实现: 使用this.$emit(event,arg)修改父组件的数据
/** * v-model 绑定事件 双向绑定实现 */ returnBackFn () { let longitude = parseFloat(this.longFlag + this.Dms2D(this.longitude)); let latitude = parseFloat(this.latFlag + this.Dms2D(this.latitude)); let array = [longitude, latitude] this.$emit('input', array); },
Coordinates组件完整代码
<template> <div class="coordinates"> <!-- 经度 --> <div class="item"> <span class="itude" @click="itudeChange(true)">{{ longFlag | longitudeName }}</span> <el-input v-model.number="longitude[0]" @change="change(true,0)" size="mini"> <i slot="suffix">°</i> </el-input> <el-input v-model.number="longitude[1]" @change="change(true,1)" size="mini"> <i slot="suffix">′</i> </el-input> <el-input v-model.number="longitude[2]" @change="change(true,2)" size="mini"> <i slot="suffix">″</i> </el-input> </div> <!-- 纬度 --> <div class="item"> <span class="itude" @click="itudeChange(false)">{{ latFlag | latitudeName }}</span> <el-input v-model.number="latitude[0]" @change="change(false,0)" size="mini"> <i slot="suffix">°</i> </el-input> <el-input v-model.number="latitude[1]" @change="change(false,1)" size="mini"> <i slot="suffix">′</i> </el-input> <el-input v-model.number="latitude[2]" @change="change(false,2)" size="mini"> <i slot="suffix">″</i> </el-input> </div> </div> </template> <script> require('math') export default { name: 'Coordinates', props: { value: { //绑定的 value type: Array, require: true, validator: function (value) { let len = value.length > 0 && value.length === 2 let isvalid = Math.abs(value[0]) < 180 && Math.abs(value[1]) < 90 return len && isvalid }, default: function () { return [] } } }, // model: { // prop为 value 时不用实现 model 但是this.$emit(event,arg) 传入的event需要为 'input' // prop: 'value', // event: 'returnBack' // }, data () { return { longitude: [], // 经度 latitude: [], // 纬度 longFlag: '+', //表示东经西经 latFlag: '+', //表示南纬北纬 } }, created: function () { this.initData(); }, filters: { longitudeName (value) { return value === '+' "东经" : "西经" }, latitudeName (value) { return value === '+' "南纬" : "北纬" } }, watch: { /** * 监测父组件绑定的value */ value () { this.initData(); } }, computed: { // 转换为 东经 XXX°XX′XX″ 格式 // 返回一个经纬度的数组 formatString () { let longitude = (this.longFlag === '+' "东经 " : "西经 ") + this.longitude[0] + '°' + this.longitude[1] + '′' + this.longitude[2] + '″'; let latitude = (this.latFlag === '+' "南纬 " : "北纬 ") + this.latitude[0] + '°' + this.latitude[1] + '′' + this.latitude[2] + '″'; return [longitude, latitude] } }, methods: { /** * 东经西经,南纬北纬 change事件 */ itudeChange (flag) { flag "less" scoped> @color-border: #9e9e9e; @height: 28px; .coordinates { border: 1px solid @color-border; width: fit-content; display: inline-flex; } .item:nth-of-type(1) { border-right: 1px solid @color-border; } .el-input { width: 40px; } .itude { height: @height; line-height: @height; display: inline-block; padding-left: 5px; cursor: pointer; user-select: none; } i { font-size: 18px; color: gray; } </style> <style lang="less"> .el-input__inner { text-align: center; border: none; border-radius: unset; } .el-input--suffix .el-input__inner { padding: 0; } </style>
测试代码 index.vue
<template> <div id="example"> <Coordinates ref="coordinates" v-model="value"></Coordinates> <el-button @click="changeValue" type="primary"> change value </el-button> <br> <span>value:{{value.toString()}}</span> <br> <span>度分秒格式:{{formatString.toString()}}</span> <el-button @click="refresh" type="primary"> refresh </el-button> </div> </template> <script> import Coordinates from '@/components/Coordinates' export default { name: 'index', components: { Coordinates }, data () { return { value: [12.34, -45.67], formatString: [] } }, mounted () { this.refresh () }, methods: { changeValue () { this.$set(this.value, 0, (this.value[0] + 2) >= 180 "less" scoped> #example { padding: 20px; } .el-button { margin: 20px; } span { font-size: 17px; } </style>
效果
修改子组件值 父组件的value会改变,修改父组件的value,子组件会自动修改, [change value] 按钮 可以修改value [refresh] 按钮 通过ref获取度分秒格式的经纬度
总结
以上所述是小编给大家介绍的基于elementUI使用v-model实现经纬度输入的vue组件,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!