【发布时间】:2021-06-29 03:59:30
【问题描述】:
我想在 props 传递的处理程序中缓存状态。
问题是,模板渲染和文本内容变化很好,但控制台总是抛出错误:
[Vue 警告]:v-on 处理程序中的错误:“TypeError:无法读取未定义的属性 'apply'”
代码如下:
<template>
<picker-box :options="item.options" @change="handlePickerChange($event, item)">
<text>{{ item.options[getPickerValue(item)].text }}</text>
</picker-box>
</template>
<script>
export default {
props: {
item: {
type: Object,
default() {
return {
options: [{ text: 'op1' }, { text: 'op2' }],
handler: {
current: 0,
get() {
return this.current
},
set(value) {
this.current = value
},
},
/* I also tried this: */
// new (function () {
// this.current = 0
// this.get = () => {
// return this.current
// }
// this.set = (value) => {
// this.current = value
// }
// })(),
}
},
},
},
methods: {
handlePickerChange(value, item) {
item.handler.set(value)
},
getPickerValue(item) {
return item.handler.get()
},
},
}
</script>
我知道使用data() 或model prop 很容易,但我希望像handler.current 那样使用,换句话说,我只想知道为什么这个处理程序对象不正确(语法层),怎么能我修好了。
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component