我想将三个下拉框变成一个组件 初始是没问题的 下拉福州市可以读取接口获取福州市的几个区 然后选区 查到街道 但是直接传值的时候一次性把三个都传进去导致第一个读取了 后面两个被重置了 一个一个传又破坏了组件整体性该怎么办js代码如下

`

export default {

name: 'AreaSelect',

model: {

prop: 'value',

event: 'change',

},

props: {

value: Object,

},

data () {

return {

citycodeDict: [{

name: '全部',

code: '',

}],

countrycodeDict: [{

name: '全部',

code: '',

}],

towncodeDict: [{

name: '全部',

code: '',

}],

}

},

watch: {

'value.citycode': {

handler: function (nVal) {

if (!nVal) {

this.reset('countrycodeDict', 'countrycode')

this.reset('towncodeDict', 'towncode')

} else {

const id = this.citycodeDict.find(e => e.code === nVal).id

this.init(id, 'countrycodeDict')

this.value.countrycode = ''

this.value.towncode = ''

}

},

},

'value.countrycode': {

handler: function (nVal) {

if (!nVal) {

this.reset('towncodeDict', 'towncode')

} else {

const id = this.countrycodeDict.find(e => e.code === nVal).id

this.init(id, 'towncodeDict')

this.value.towncode = ''

}

},

},

},

created () {

this.init(1, 'citycodeDict')

},

methods: {

init (id, target) {

const data = {

parentId: id || 1,

}

// 获取列表

const _this = this

this.$http({

url: _this.$http.adornUrl('aa/bb/cc'),

method: 'get',

params: data,

}).then(({ data }) => {

if (data && data.code === 200) {

this[target] = [{ name: '全部', code: '' }]

this[target] = this[target].concat(data.data)

}

})

},

reset (valueDict, value) {

this[valueDict] = [

{

name: '全部',

code: '',

},

]

this.value[value] = ''

},

},

}

`

回答

试试这个方案

// data加上一个listen,初始值为true

data:{

cityListen:true;

countryListen:true;

}

// 在if当中加判断,当listen为false时就不清空

"value.citycode": {

handler: function (nVal) {

if (!nVal) {

this.reset("countrycodeDict", "countrycode");

this.reset("towncodeDict", "towncode");

} else {

const id = this.citycodeDict.find((e) => e.code === nVal).id;

this.init(id, "countrycodeDict");

if (this.cityListen) {

this.value.countrycode = "";

this.value.towncode = "";

}else{

this.cityListen = true;

}

}

},

},

"value.countrycode": {

handler: function (nVal) {

if (!nVal) {

this.reset("towncodeDict", "towncode");

} else {

const id = this.countrycodeDict.find((e) => e.code === nVal).id;

this.init(id, "towncodeDict");

if (this.countryListen) {

this.value.towncode = "";

}else{

this.countryListen = true;

}

}

},

}

// 添加赋值的方法,在外部使用ref进行调用

setVal(citycode, countrycode, towncode) {

this.cityListen = false;

this.countryListen = false;

this.value.citycode = citycode;

this.value.countrycode = countrycode;

this.value.towncode = towncode;

},

多谢大佬提供了思路,稍微调整下就行 需要修改下citycodewatch改变时候 的有调用数据库所以会慢点 查询countryDict,这时候 countrycode的watch事件已经完成了 所以需要在调用下 countrycode的watch事件,真的好麻烦 看看后面有没办法优化了

`export default {

name: 'AreaSelect',

model: {

prop: 'value',

event: 'change',

},

props: {

value: Object,

},

data () {

return {

cityFlag: true,

countryFlag: true,

citycodeDict: [{

name: '全部',

code: '',

}],

countrycodeDict: [{

name: '全部',

code: '',

}],

towncodeDict: [{

name: '全部',

code: '',

}],

}

},

watch: {

'value.citycode': {

handler: function (nVal) {

const _this = this

if (!nVal) {

this.reset('countrycodeDict', 'countrycode')

} else {

const id = this.citycodeDict.find(e => e.code === nVal).id

this.init(id, 'countrycodeDict', function () {

if (!_this.cityFlag) {

_this.value.countrycode = ''

} else {

_this.cityFlag = false

_this.countryChange(_this.value.countrycode)

}

})

}

},

},

'value.countrycode': {

handler: function (nVal) {

this.countryChange(nVal)

},

},

},

created () {

this.init(1, 'citycodeDict')

},

methods: {

countryChange (nVal) {

if (!nVal) {

this.reset('towncodeDict', 'towncode')

} else if (!this.cityFlag) {

const id = this.countrycodeDict.find(e => e.code === nVal).id

this.init(id, 'towncodeDict')

if (!this.countryFlag) {

this.value.towncode = ''

} else {

this.countryFlag = false

}

}

},

// 数据库读取数据

init (id, target, callback) {

const data = {

parentId: id || 1,

}

// 获取列表

const _this = this

this.$http({

url: _this.$http.adornUrl('major/common/area'),

method: 'get',

params: data,

}).then(({ data }) => {

if (data && data.code === 200) {

this[target] = [{ name: '全部', code: '' }]

this[target] = this[target].concat(data.data)

if (callback) {

callback()

}

}

})

},

// 重置

reset (valueDict, value) {

this[valueDict] = [

{

name: '全部',

code: '',

},

]

this.value[value] = ''

},

setValue (a, b, c) {

this.cityFlag = true

this.countryFlag = true

this.value.citycode = a

this.value.countrycode = b

this.value.towncode = c

},

},

}`

html5日期不联动下拉框,下拉框联动问题 赋值时候失效相关推荐

  1. antd vue 多个下拉 联动_antd中select下拉框值为对象选中的问题

    使用antd中的select下拉框,遇到个小问题,后台管理系统中,使用下拉框,一般不会是简单的就直接使用select中的value值,而是会使用id或者value中文对应的keyword,并且这个在o ...

  2. dropdownlist三级联动怎么实现_Excel下拉菜单不会做?15秒教会你制作一二三级联动下拉菜单,从此做表不求人!...

    Hi,各位叨友们好呀!我是叨叨君~ 我们都知道,在使用Excel表格统计数据的时候,为了方便录入,通常会在Excel中设置下拉菜单,以便我们输入相同的内容. 那么,Excel中一级.二级.三级联动下拉 ...

  3. 纯html+css实现点击显示再次点击隐藏并且不联动的多个下拉菜单

    纯html+css实现点击显示再次点击隐藏不联动的多个下拉菜单 实现不联动的下拉菜单核心思想: input<type="checkbox">(复选框) label和in ...

  4. Asp net实现下拉框和列表框的连动

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 走过了牛 ...

  5. js 实现多选框(复选框) 和单选框,下拉框功能完整示例代码附效果图

    <!DOCTYPE html> <html><head><meta charset="utf-8" /><script src ...

  6. python+selenium七:下拉框、选项框、select用法

    # from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains im ...

  7. 如何用javascript获取文本框,下拉框,单选框的对应值或者将值赋给它们?雪原虎 发布于:2007-10-22 00:32

    如何用javascript获取文本框,下拉框,单选框的对应值或者将值赋给它们? 雪原虎 发布于:2007-10-22 00:32 1.文本框 1.1 <input type="text ...

  8. Java swing 实现下拉框和文本框同步显示

    想要MyEclipse中的swing中实现下拉框和文本框实现,对下拉框创建MouseEvent.ItemEvent.ActionEvent private void xingbieMouseClick ...

  9. layui table勾选框的修改_layui表格(Table)下添加可更新拉选择框select

    添加CSS 防止下拉框被遮挡 /* 防止下拉框下拉值被遮盖*/ .layui-table-cell { overflow: visible; } .layui-table-box { overflow ...

  10. 什么是今日头条下拉词下拉框?

    什么是今日头条下拉词下拉框? 什么是今日头条下拉词下拉框?简单的来说,今日头条下拉词下拉框跟百度.搜狗下拉词下拉框这些是同样的原理跟道理. 下拉框就是在网页或者app搜索某个关键词的时候,会出现一个下 ...

最新文章

  1. JavaScript检测之basevalidate.js
  2. Linux初级运维(七)——bash脚本编程(常见测试)
  3. Python vaptcha手势人机验证码识别
  4. before css 旋转_七夕,当然少不了纯CSS的点缀啦
  5. eclipse全局搜索_Eclipse如何进行全局搜索和单页面搜索?
  6. 高效大数据开发之 bitmap 思想的应用
  7. 2.图像作为函数 | 初探数字图像_3
  8. Wet Shark and Flowers(思维)
  9. 数据中心服务器巡检方案,数据中心巡检机器人方案.pdf
  10. MVC和MVVM框架模式
  11. jQuery简单倒计时插件
  12. 锋利的jQuery读书笔记-第1章 认识jQuery
  13. xp计算机限制打开u盘,winxp电脑提示U盘拒绝访问怎么修复
  14. 习题6-5 使用函数验证哥德巴赫猜想 (20 point(s))
  15. 电大专科《计算机网络》机考题库,电大专科计算机网络机考网考纸考题库及答案.doc...
  16. ubuntu企业微信和微信无法发送和显示图片和截图问题
  17. Android心跳包(一)——心跳机制
  18. 智能网联车路协同智慧交通沙盘+无人驾驶汽车+多车协同调度系统
  19. 【笔记】三张图读懂机器学习:基本概念、五大流派与九种常见算法
  20. Typescript从入门到精通(上)

热门文章

  1. TensorFlow2简单入门-图像加载及预处理
  2. 谁是卧底的Android应用 (原型源码)
  3. Kafka设计解析(八)- Kafka事务机制与Exactly Once语义实现原理
  4. Java元组Tuple使用实例--转载
  5. Redis: under the hood---转载
  6. 【Python】Python常用的Series 和 Dataframe处理方法
  7. PANDAS 数据合并与重塑(concat篇) 原创 2016年09月13日 19:26:30 47784 pandas作者Wes McKinney 在【PYTHON FOR DATA ANALYS
  8. 蚂蚁金服发布「定损宝」,推动图像定损技术在车险领域的应用
  9. jvm性能调优实战 - 40 百万级数据误处理导致的频繁Full GC问题优化
  10. RocketMQ-初体验RocketMQ(08)-IDEA拉取调测RocketMQ源码