首先建立mixin文件夹
命名mode-ways.js ,重点代码(直接复制)
export default function twoWaysMixin(
option = {},
attrName = 'value',
valueKey = 'bindVal',
eventName = 'bind-change'
) {
const obj = {
props: {},
model: {},
computed: {}
}
obj.props = {
[attrName]: option
}
obj.model = {
prop: attrName,
event: eventName
}
obj.computed = {
[valueKey]: {
get() {
return this[attrName]
},
set(new_val) {
this.$emit(eventName, new_val)
}
}
}
return obj
}
使用,在需要的页面中引入
import ModeWaysMixin from '@/mixin/mode-ways'
mixins: [twoWaysMixin({ type: [Array, Object], required: true })],