视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
关于vue组件jsx语法的使用介绍
2020-11-27 19:34:20 责编:小采
文档

本篇文章主要介绍了vue组件jsx语法的具体使用,内容挺不错的,现在分享给大家,也给大家做个参考。

如果使用render函数来写比较复杂的vue组件,对于可读性和可维护性都很不友好,而使用jsx就会让我们回到更接近于模板的语法。babel转译器会将jsx转译为render函数渲染。

配置

需要用到babel插件

安装

npm install\
 babel-plugin-syntax-jsx\
 babel-plugin-transform-vue-jsx\
 babel-helper-vue-jsx-merge-props\
 babel-preset-env\
 --save-dev

.babelrc配置

在plugins中添加transform-vue-jsx

{
 "presets": ["env"],
 "plugins": ["transform-vue-jsx"]
}

基础示例

转义前

<p id="foo">{this.text}</p>

转译后

h('p', {
 attrs: {
 id: 'foo'
 }
}, [this.text])

Note:h函数为vue实例的$createElement方法,必须存在于jsx的作用域中,在渲染函数中必须以第一个参数传入,如:

render (h) { // <-- h 函数必须在作用域内
 return <p id="foo">bar</p>
}

自动注入h函数

从3.4.0开始,在用ES2015语法声明的方法和getter访问器中(使用function关键字或箭头函数除外),babel会自动注入hconst h = this.$createElement)函数,所以可以省略(h)参数。

Vue.component('jsx-example', {
 render () { // h 会自动注入
 return <p id="foo">bar</p>
 },
 myMethod: function () { // h 不会注入
 return <p id="foo">bar</p>
 },
 someOtherMethod: () => { // h 不会注入
 return <p id="foo">bar</p>
 }
})

@Component
class App extends Vue {
 get computed () { // h 会自动注入
 return <p id="foo">bar</p>
 }
}

Vue JSX 和 React JSX对比

首先, Vue2.0 的vnode 格式与react不同,createElement函数的第二个参数是一个数据对象,接受一个嵌套的对象,每一个嵌套对象都会有对应的模块处理。

Vue2.0 render语法

render (h) {
 return h('p', {
 // 组件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 (
 <p
 // normal attributes or component props.
 id="foo"
 // DOM properties are prefixed with `domProps`
 domPropsInnerHTML="bar"
 // event listeners are prefixed with `on` or `nativeOn`
 onClick={this.clickHandler}
 nativeOnClick={this.nativeClickHandler}
 // other special top-level properties
 class={{ foo: true, bar: false }}
 style={{ color: 'red', fontSize: '14px' }}
 key="key"
 ref="ref"
 // assign the `ref` is used on elements/components with v-for
 refInFor
 slot="slot">
 </p>
 )
}

JSX展开运算符

支持JSX展开,插件会智能的合并数据属性,如:

const data = {
 class: ['b', 'c']
}
const vnode = <p class="a" {...data}/>

合并后的数据为:

{ 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 <p {...{ directives }}/>

下载本文
显示全文
专题