用jsx写vue组件,怎样监听.sync修饰符的事件
<el-dialog title="详细" :visible.sync="open" width="700px" append-to-body ></el-dialog >jsx写法<el-dialog title="详细" on={{ ['update:visible']: this.view}} visible={this.open} width="700px"
·
<el-dialog title="详细" :visible.sync="open" width="700px" append-to-body >
</el-dialog >
jsx 写法
<el-dialog title="详细" on={{ ['update:visible']: this.view}} visible={this.open} width="700px" append-to-body >
</el-dialog >
view(){
this.open=true
}
//此写法不影响其他 on 事件
对于 .passive
、.capture
和 .once
这些事件修饰符, Vue 提供了相应的前缀可以用于 on
修饰符 | 前缀 |
.passive | & |
.capture | ! |
.once | ~ |
.capture.once 或.once.capture | ~! |
on: {
'!click': this.doThisInCapturingMode,
'~keyup': this.doThisOnce,
'~!mouseover': this.doThisOnceInCapturingMode
}
其他修饰符
修饰符 | 处理函数中的等价操作 |
.stop | event.stopPropagation() |
.prevent | event.preventDefault() |
.self | if (event.target !== event.currentTarget) return |
按键:.enter , .13 | if (event.keyCode !== 13) return (对于别的按键修饰符来说,可将 13 改为另一个按键码) |
修饰键:.ctrl , .alt , .shift , .meta | if (!event.ctrlKey) return (将 ctrlKey 分别修改为 altKey 、shiftKey 或者 metaKey ) |
on: {
keyup: function (event) {
// 如果触发事件的元素不是事件绑定的元素
// 则返回
if (event.target !== event.currentTarget) return
// 如果按下去的不是 enter 键或者
// 没有同时按下 shift 键
// 则返回
if (!event.shiftKey || event.keyCode !== 13) return
// 阻止 事件冒泡
event.stopPropagation()
// 阻止该元素默认的 keyup 事件
event.preventDefault()
// ...
}
}
插槽
//你可以通过 this.$slots 访问静态插槽的内容,每个插槽都是一个 VNode 数组:
render: function (createElement) {
// `<div><slot></slot></div>`
return createElement('div', this.$slots.default)
}
//也可以通过 this.$scopedSlots 访问作用域插槽,每个作用域插槽都是一个返回若干 VNode 的函数:
props: ['message'],
render: function (createElement) {
// `<div><slot :text="message"></slot></div>`
return createElement('div', [
this.$scopedSlots.default({
text: this.message
})
])
}
//如果要用渲染函数向子组件中传递作用域插槽,可以利用 VNode 数据对象中的 scopedSlots 字段:
render: function (createElement) {
return createElement('div', [
createElement('child', {
// 在数据对象中传递 `scopedSlots`
// 格式为 { name: props => VNode | Array<VNode> }
scopedSlots: {
default: function (props) {
return createElement('span', props.text)
}
}
})
])
}
扩展
createElement(
'anchored-heading', {
props: {
level: 1
}
}, [
createElement('span', 'Hello'),
' world!'
]
)
<anchored-heading :level="1">
<span>Hello</span> world!
</anchored-heading>
//更接近于模板的语法上的jsx
import AnchoredHeading from './AnchoredHeading.vue'
new Vue({
el: '#demo',
render: function (h) {
return (
<AnchoredHeading level={1}>
<span>Hello</span> world!
</AnchoredHeading>
)
}
})
更多推荐
已为社区贡献1条内容
所有评论(0)