如何在vue中使用jsx語法
為什么需要在vue中使用jsx
幾年前面試的時(shí)候,被問過這個(gè)問題,當(dāng)時(shí)我一臉懵,這兩個(gè)東西為啥要混用呢?
直到后來,我遇到了這種場景。
tab切換展示組件,大部分組件展示表格,除了2個(gè)tab需要展示不同,這個(gè)不同,怎么處理呢?
當(dāng)然可以直接改封裝的tab組件,v-if條件渲染嘛
那如果后面再有其他需求,tab組件繼續(xù)寫if么
這個(gè)時(shí)候,組件就過于冗余啦
那怎么讓組件統(tǒng)一處理呢?當(dāng)然可以用render函數(shù)來抽象組件啦
render函數(shù)寫法有多惡心,想必大家都知道 => 不知道的看段簡單的ul,li布局

with(this){ // this 就是 vm return _c( 'div', { attrs:{"id":"app"} }, [ _c( 'div', [ _c( 'input', { directives:[ { name:"model", rawName:"v-model", value:(title), expression:"title" } ], domProps:{ "value":(title) }, on:{ "input":function($event){ if($event.target.composing)return; title=$event.target.value } } } ), _v(" "), (!title) ? _c( 'button', { on:{ "click":add } }, [_v("submit")] ): _e() ] ), _v(" "), // 空字符串節(jié)點(diǎn) _c('div', [ _c( 'ul', _l((list),function(item){return _c('li',[_v(_s(item))])}) ) ] ) ] ) }這...日子沒法過了
于是我們就發(fā)現(xiàn)了jsx的好用,同樣上述代碼,可讀性更高,更加精簡
在vue中如何使用jsx
主要是依賴babel插件
- 安裝babel依賴,
npm install babel-plugin-transform-vue-jsx babel-helper-vue-jsx-merge-props - 配置.babelrc文件
"plugins": [ [ "transform-vue-jsx" ] ] - 子組件是函數(shù)式組件
// content.js
export default {
name: 'content',
functional: true,
props: {
render: Function,
column: {
type: Object,
default: null
},
params: {
type: Object,
default: () => {}
}
},
render: (h, ctx) => {
const params = ctx.props.params
return ctx.props.render && ctx.props.render(h, params)
}
}- 父組件引入
<el-tabs
v-model="activeName">
<el-tab-pane
v-for="item in tabList"
:key="item.code"
:label="item.label">
<span v-if="item.render">
<content
:params="item.params"
:render="item.render"
></content>
</span>
</el-tab-pane>
</el-tabs>
<script>
import Content from './content'
components: {
Content
},
</script>template轉(zhuǎn)jsx的語法轉(zhuǎn)換
v-model,v-if,v-for,v-html,v-text,vue中的指令
- jsx語法是不會(huì)有對應(yīng)的指令的,所以我們就要實(shí)現(xiàn)這些指令的功能,對于
v-model
// 在vue中
<el-input
v-model="searchParams.searchVal">
</el-input>
// 對應(yīng)jsx語法
function _input (value) {
this.searchParams.searchVal = value
},
item.render = (h, params) => {
// 這里也可以從params傳入?yún)?shù)
return (
<el-input
value={this.searchParams.searchVal}
onInput={_input}>
</el-input>
)
}v-if其實(shí)就是判斷語句,用&&或三元表達(dá)式
// 在vue中
<el-button v-if="show"></el-button>
// 對應(yīng)jsx語法
item.render = (h, params) => {
return (
this.show && <el-button></el-button>
)
}v-for其實(shí)就是循環(huán)語句,用map
// 在vue中
<ul>
<li v-for="item in list">{{item.label}}</li>
</ul>
// jsx語法
item.render = (h, params) => {
return (
<ul>
{
list.map((item, index) => (
<li>{item.label}</li>
)
}
</ul>
)
}
v-html
item.render = (h, params) {
return (
<div> <div domPropsInnerHTML={htmlStr}></div> </div>
)
}vue中el-input組件上觸發(fā)原生enter事件,@keyup.enter.native對應(yīng)nativeOnKeyup
// 在vue中
<el-input
@keyup.enter.native="onSearch"
></el-input>
// 在jsx中
item.render = (h, params) => {
function _keyup (e) {
if (e.keyCode === 13) {
// 13為enter鍵的鍵盤碼
this.onSearch()
}
}
return (
<el-input
nativeOnKeyup={e => _keyup(e)}>
</el-input>
)
}vue中的插槽,在jsx中用scopedSlots
// 在vue中
<el-table
:data="tableData">
<el-table-column
v-for="column in columnData"
:key="column.value"
:prop="column.value"
:label="column.value"
sortable
:sort-change="change">
<template slot-scope="scope">
<span>{{scope.row[column.value]}}</span>
</template>
</el-table-column>
</el-table>
// 在jsx中
item.render = (h, params) => {
return (
<el-table
data={tableData}>
{
columnData.map((column, index) => (
<el-table-column
prop={column.value}
label={column.label}
sortable
onSort-change={(...args) => sortChange(...args)}
scopedSlots={{
default: (scope) => <span>{scope.row[column.value]}</span>
}}>
</el-table-column>
))
}
</el-table>
)
}組件用-分隔的事件,在jsx中在第一段on后大寫即可觸發(fā)
比如el-table的sort-change,在jsx中是onSort-change,第一段在on后大寫即可,見上個(gè)例子
到此這篇關(guān)于如何在vue中使用jsx語法的文章就介紹到這了,更多相關(guān)vue使用jsx語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用 vue 實(shí)現(xiàn)滅霸打響指英雄消失的效果附demo
這篇文章主要介紹了使用 vue 實(shí)現(xiàn)滅霸打響指英雄消失的效果 demo,需要的朋友可以參考下2019-05-05
基于Vue實(shí)現(xiàn)卡片無限滾動(dòng)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了如何利用Vue制作出卡片無限滾動(dòng)動(dòng)畫,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-05-05
Vue?axios和vue-axios的關(guān)系及使用區(qū)別
axios是基于promise的HTTP庫,可以使用在瀏覽器和node.js中,它不是vue的第三方插件,vue-axios是axios集成到Vue.js的小包裝器,可以像插件一樣安裝使用:Vue.use(VueAxios, axios),本文給大家介紹Vue?axios和vue-axios關(guān)系,感興趣的朋友一起看看吧2022-08-08
Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的方法
這篇文章主要介紹了Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
vue axios基于常見業(yè)務(wù)場景的二次封裝的實(shí)現(xiàn)
這篇文章主要介紹了vue axios基于常見業(yè)務(wù)場景的二次封裝的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

