bar
}
})
@Component
class App extends Vue {
get computed () { // h 会自动注入
return
bar
}
}
Vue JSX 和 React JSX对比
首先, Vue2.0 的vnode 格式与react不同,createElement
函数的第二个参数是一个数据对象,接受一个嵌套的对象,每一个嵌套对象都会有对应的模块处理。
Vue2.0 render语法
render (h) {
return h('div', {
// 组件props
props: {
msg: 'hi'
},
// 原生HTML属性
attrs: {
id: 'foo'
},
// DOM props
domProps: {
innerHTML: 'bar'
},
// 事件是嵌套在`on`下面的,所以将不支持修饰符,如:`v-on:keyup.enter`,只能在代码中手动判断keyCode
on: {
click: this.clickHandler
},
// For components only. Allows you to listen to
// native events, rather than events emitted from
// the component using vm.$emit.
nativeOn: {
click: this.nativeClickHandler
},
// class is a special module, same API as `v-bind:class`
class: {
foo: true,
bar: false
},
// style is also same as `v-bind:style`
style: {
color: 'red',
fontSize: '14px'
},
// other special top-level properties
key: 'key',
ref: 'ref',
// assign the `ref` is used on elements/components with v-for
refInFor: true,
slot: 'slot'
})
}
对应的Vue2.0 JSX语法
render (h) {
return (
)
}
JSX展开运算符
支持JSX展开,插件会智能的合并数据属性,如:
const data = {
class: ['b', 'c']
}
const vnode =
合并后的数据为:
{ class: ['a', 'b', 'c'] }
Vue 指令
JSX对大多数的Vue内建指令都不支持,唯一的例外是v-show
,该指令可以使用v-show={value}
的语法。大多数指令都可以用编程方式实现,比如v-if
就是一个三元表达式,v-for
就是一个array.map()
等。
如果是自定义指令,可以使用v-name={value}
语法,但是改语法不支持指令的参数arguments
和修饰器modifier
。有以下两个解决方法:
将所有内容以一个对象传入,如:v-name={{ value, modifier: true }}
使用原生的vnode指令数据格式,如:
const directives = [
{ name: 'my-dir', value: 123, modifiers: { abc: true } }
]
return
Vue的优点
Vue具体轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、虚拟DOM、运行速度快等优势,Vue中页面使用的是局部刷新,不用每次跳转页面都要请求所有数据和dom,可以大大提升访问速度和用户体验。
上述内容就是怎么在vue组件中使用jsx语法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网站栏目:怎么在vue组件中使用jsx语法-创新互联
本文链接:
http://pwwzsj.com/article/cdsjih.html