elementUI select,option变化,如果option不存在上次的选项,自动清空上次的选择

select,option变化,如果option不存在上次的选项,自动清空上次的选择

javascript 复制代码
<script src="//unpkg.com/vue@2/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.15.14/lib/index.js"></script>
<div id="app">
<template>
  <el-select v-model="value" placeholder="请选择">
    <el-option
      v-for="item in arr"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
  <el-button @click="fun">fun</el-button>
  <el-button @click="fun2">fun2</el-button>
  <el-button @click="fun3">fun3</el-button>
  <el-button @click="fun4">fun4</el-button>
</template>
</div>

var Main = {
    data() {
      return {
        arr:[],
        options: [{value: '黄金糕',label: '黄金糕'}, {
          value: '双皮奶',label: '双皮奶'}, {
          value: '蚵仔煎',label: '蚵仔煎'}],
        options2: [{value: '黄金糕2',label: '黄金糕2'}, {
          value: '双皮奶2',label: '双皮奶2'}, {
          value: '蚵仔煎2',label: '蚵仔煎2'} ,{
          value: '蚵仔煎',label: '蚵仔煎'}],
        value: ''//
      }
    },
    methods:{
      fun(){
        this.arr= this.options
      },
      fun2(){
        this.arr=this.options2
      },
      fun3(){ this.arr=[]},
      fun4(){ alert(this.value)}
    },
    watch:{
      arr(val){
        let data=val.find(i=>i.value==this.value)
        if(!data)this.value=''
      }
    }
  }
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
相关推荐
奕鼎竜瑆15 分钟前
Solid 前端响应式开发从零到精通
前端·人工智能
动恰客流统计1 小时前
景区客流统计怎么做?兼顾管控与运营的实施方案解析
大数据·前端·人工智能
派小心.2 小时前
A/B测试工具免费版够用吗?
前端·数据分析
前端snow2 小时前
ai agent --- redis 缓存
前端
duanshu_3 小时前
官网表单与下载异常怎么排查?五个环节定位故障
前端·自动化·flyweight
Zhou1411363 小时前
SpringBoot_03_Web开发
前端·spring boot·python
辻弋2013 小时前
【无标题】
大数据·服务器·前端·搜索引擎·开源软件
FluxArt4 小时前
GPT Image 2.5 中文提示词教程:Flux Art 可复用模板
前端·gpt·算法
搬砖的小码农_Sky5 小时前
AI Agent:Windows上Node.js安装后 npm -v报错
前端·npm·node.js·人机交互
FYKJ_20105 小时前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark