詳解利用jsx寫vue組件的方法示例
前言
本文主要給大家介紹的是關(guān)于利用jsx寫vue組件,下面話不多說(shuō),來(lái)一起看看詳細(xì)的介紹吧。
我們平常寫vue的組件時(shí),一般都是用的是模版,這種方式看起來(lái)比較簡(jiǎn)潔,而且vue作者也推薦使用這個(gè)方式,但是這種方式也有一些它的弊端,例如模版調(diào)試麻煩,或者在一些場(chǎng)景下模版描述可能沒那么簡(jiǎn)單和方便。
下面我們要講的是如何在vue里面寫jsx,知道react的人應(yīng)該都知道jsx,jsx的一個(gè)特性就是非常靈活,雖然有的人覺得jsx很丑陋,把邏輯都寫到模版的感覺,但蘿卜青菜各有所愛,適合自己適合團(tuán)隊(duì)的就是最好的。
在使用jsx之前我們需要安裝一個(gè)babel插件(babel-plugin-transform-vue-jsx )
安裝方式:
npm install\ babel-plugin-syntax-jsx\ babel-plugin-transform-vue-jsx\ babel-helper-vue-jsx-merge-props\ babel-preset-es2015\ --save-dev
然后再.babelrc里面添加:
{ "presets": ["es2015"], "plugins": ["transform-vue-jsx"] }
接著我們就可以愉快地在vue里面編寫jsx了。
Test.vue
<script> export default { props: ['onClick', 'isShow'], data() { return { test: 123 }; }, render() { return ( <div class="test" onClick={ this.onClick }> { this.test } { this.isShow + '' } </div> ); } } </script>
可以看到我們把jsx寫在了render方法里面,render方法是vue2.0才支持的,用來(lái)提供對(duì)虛擬DOM的支持,也就是說(shuō)只有vue2.0才支持jsx語(yǔ)法轉(zhuǎn)換。
這里要注意的一點(diǎn)是vue里面編寫jsx和在react里面的jsx語(yǔ)法還是有一點(diǎn)不一樣的。
以下是一段覆蓋大部分語(yǔ)法的vue jsx代碼:
render (h) { return ( <div // 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"> </div> ) }
可以看到DOM屬性要加domProps前綴,但這里lass和style卻不需要,因?yàn)檫@兩個(gè)是特殊的模塊,而且react的class用的是className,vue卻用的class。事件監(jiān)聽是以“on”或者“nativeOn”為開始。
實(shí)際上vue2.0的模版最后都會(huì)被編譯為render方法,所以模版聲明的組件和jsx聲明的組件最后都是一樣的。
上面的jsx最后會(huì)被編譯成下面這樣:
render (h) { return h('div', { // Component props props: { msg: 'hi' }, // normal HTML attributes attrs: { id: 'foo' }, // DOM props domProps: { innerHTML: 'bar' }, // Event handlers are nested under "on", though // modifiers such as in v-on:keyup.enter are not // supported. You'll have to manually check the // keyCode in the handler instead. 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' }) }
這也意味著兩種形式的組件是可以相互引用的。
有時(shí)候我們難免會(huì)在模版里引入jsx編寫的vue組件或者在jsx編寫的vue組件里引入模版組件,這里還是有些需要注意的事項(xiàng):
1.在模版里面引入jsx的組件,可以通過(guò)components引用,另外props的編寫從駝峰式改為連接符:
<template> <div class="wrapper"> <Test :on-click="clickHandler" :is-show="show"></Test> </div> </template> <script> import Test from './Test.vue'; export default { name: 'hello', components: { Test }, data() { return { msg: 'Welcome to Your Vue.js App', show: true }; }, methods: { clickHandler(){ this.show = !this.show; } } }; </script>
2.在jsx里面引入vue模版組件,這里沒有什么要注意的,除了連接符式的屬性要轉(zhuǎn)換成駝峰式,還有一個(gè)需要注意的是指令,如果用了jsx,那么內(nèi)置的指令都不會(huì)生效(除了v-show),好在內(nèi)置指令大部分都可以用jsx描述。那么自定義指令要怎么用呢?
自定義指令可以使用“v-name={value}
”語(yǔ)法,如果要支持指令參數(shù)和modifier可以用“v-name={{ value, modifier: true }}
”語(yǔ)法:
<script> import Vue from 'vue'; Vue.directive('my-bold', { inserted: function (el) { el.style.fontWeight = 900; } }) export default { props: ['onClick', 'isShow'], data() { return { test: 123 }; }, methods: { afterLeave() { console.log('afterLeave') } }, render() { const directives = [ { name: 'my-bold', value: 666, modifiers: { abc: true } } ]; return ( <transition onAfterLeave={this.afterLeave} name="fade"> <div class="test" onClick={this.onClick} v-show={ this.isShow } v-my-bold> {this.test} {this.isShow + ''} </div> </transition> ); } } </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity .5s } .fade-enter, .fade-leave-to { opacity: 0 } </style>
我們還可以用原生vnode的數(shù)據(jù)格式使用自定義指令:
const directives = [ { name: 'my-dir', value: 123, modifiers: { abc: true } } ] return <div {...{ directives }}/>
擴(kuò)展
如果有人覺得在vue組件里面要寫data,props,computed和methods不夠優(yōu)雅,可以參考下這個(gè)插件vue-class-component,它能讓你使用ES6的class和ES7的裝飾器編寫vue組件。
相關(guān)鏈接:
babel-plugin-transform-vue-jsx
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
在使用vue-cli創(chuàng)建vue項(xiàng)目,如何添加和刪除預(yù)置配置
這篇文章主要介紹了在使用vue-cli創(chuàng)建vue項(xiàng)目,如何添加和刪除預(yù)置配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue2.6.10+vite2開啟template模板動(dòng)態(tài)編譯的過(guò)程
這篇文章主要介紹了vue2.6.10+vite2開啟template模板動(dòng)態(tài)編譯,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02vue2中使用echarts實(shí)現(xiàn)中國(guó)地圖、在中國(guó)地圖上標(biāo)注坐標(biāo)散點(diǎn)圖的操作代碼
這篇文章主要介紹了vue2中使用echarts實(shí)現(xiàn)中國(guó)地圖、在中國(guó)地圖上標(biāo)注坐標(biāo)散點(diǎn)圖,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05在Vue當(dāng)中同時(shí)配置多個(gè)路由文件的方法案例代碼
這篇文章主要介紹了在Vue當(dāng)中同時(shí)配置多個(gè)路由文件的方法,包含具體代碼,本文分步驟結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12基于Vue 服務(wù)端Cookies刪除的問(wèn)題
今天小編就為大家分享一篇基于Vue 服務(wù)端Cookies刪除的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09使用canvas實(shí)現(xiàn)一個(gè)vue彈幕組件功能
這篇文章主要介紹了使用canvas實(shí)現(xiàn)一個(gè)vue彈幕組件功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11vue3+vite2中使用svg的方法詳解(親測(cè)可用)
vue2的時(shí)候使用的是字體圖標(biāo),缺點(diǎn)就是比較單一,到了vue3,相信瀏覽器的性能起來(lái),所以這里記錄一下,下面這篇文章主要給大家介紹了關(guān)于vue3+vite2中使用svg的相關(guān)資料,需要的朋友可以參考下2022-08-08