vue 多选监听值变化
时间: 2023-07-17 09:06:40 浏览: 52
在Vue中,可以使用`$watch`来监听localStorage值的变化。
首先在data中定义一个变量来存储localStorage中的值,然后在mounted钩子函数中使用`$watch`监听该变量。
```
data() {
return {
localStorageValue: localStorage.getItem('key')
}
},
mounted() {
this.$watch(
() => localStorage.getItem('key'),
(newValue) => {
this.localStorageValue = newValue
}
)
}
```
在上面的代码中,`$watch`监听的是`localStorage.getItem('key')`的值,当该值发生变化时,就会触发回调函数。回调函数中更新了`localStorageValue`变量的值。
相关问题
vue 多选列表组件
以下是一个使用 Vue.js 实现多选列表组件的示例代码:
```vue
<template>
<ul>
<li v-for="option in options" :key="option.value">
<input type="checkbox" :id="option.value" :value="option.value" v-model="selectedOptions">
<label :for="option.value">{{ option.label }}</label>
</li>
</ul>
</template>
<script>
export default {
props: {
options: {
type: Array,
required: true
},
value: {
type: Array,
default: () => []
}
},
data() {
return {
selectedOptions: this.value
};
},
watch: {
selectedOptions: function (newValue) {
this.$emit('input', newValue);
}
}
};
</script>
```
这个组件接受一个 `options` 数组作为输入,每个选项包含一个 `value` 和 `label` 属性。它还接受一个 `value` 数组作为默认选中的选项。
在组件中使用 `v-for` 循环遍历 `options` 数组来渲染每个选项。通过使用 `v-model` 指令将 `selectedOptions` 数组绑定到每个复选框的状态上,以便在选项被选中或取消选中时更新它。
当 `selectedOptions` 数组发生变化时,组件会触发一个名为 `input` 的事件,并传递更新后的数组作为参数。父组件可以通过监听这个事件来获取选中的选项。
vue自定义多选可搜索下拉框
要实现一个自定义多选可搜索下拉框,可以使用Vue框架结合element-ui组件库来实现。下面是一个简单的实现方式:
首先,安装element-ui组件库:
```
npm install element-ui
```
然后,在需要使用的组件中引入element-ui:
```javascript
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
```
接下来,创建一个多选可搜索下拉框组件,代码如下:
```html
<template>
<el-select
v-model="selectedItems"
multiple
filterable
remote
:remote-method="remoteMethod"
:loading="loading"
:disabled="disabled"
:placeholder="placeholder"
clearable
@change="handleChange">
<el-option
v-for="(item, index) in options"
:key="index"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</template>
<script>
export default {
data() {
return {
selectedItems: [],
options: [],
loading: false,
disabled: false,
placeholder: '请选择',
remoteUrl: '',
remoteParams: {},
}
},
methods: {
remoteMethod(query) {
if (query !== '') {
this.loading = true
// 发送请求获取数据
axios.get(this.remoteUrl, { params: { query, ...this.remoteParams } }).then(res => {
this.options = res.data
this.loading = false
}).catch(() => {
this.loading = false
})
} else {
this.options = []
}
},
handleChange(val) {
this.$emit('change', val)
}
},
props: {
value: {
type: Array,
default: () => []
}
},
watch: {
value: {
handler(newVal) {
this.selectedItems = newVal
},
immediate: true
}
}
}
</script>
```
这个组件使用了element-ui中的`el-select`和`el-option`组件,通过设置`multiple`属性来实现多选。设置`filterable`属性来让下拉框可搜索,通过设置`remote`属性来实现远程搜索。
在组件中,通过`remoteMethod`方法来发送请求获取需要显示的选项,将获取到的选项保存在`options`数据中,同时设置`loading`状态来显示加载中状态。`handleChange`方法用于监听选项的变化,通过`$emit`方法触发`change`事件来向父组件传递选中的值。组件中还定义了一些props和data,用于接收外部传入的数据。
最后,在使用该组件的父组件中,可以通过以下方式来使用:
```html
<template>
<div>
<custom-select v-model="selectedItems" :remote-url="remoteUrl"></custom-select>
</div>
</template>
<script>
import CustomSelect from './CustomSelect.vue'
export default {
components: {
CustomSelect
},
data() {
return {
selectedItems: [],
remoteUrl: 'http://example.com/api/search'
}
},
methods: {
handleChange(val) {
console.log(val)
}
}
}
</script>
```
在父组件中,可以通过v-model指令来绑定选中的值,通过设置`remote-url`属性来指定远程搜索的API地址。同时,可以监听`change`事件来获取选中的值。