如何在vue中使用jsx語法
為什么需要在vue中使用jsx
幾年前面試的時候,被問過這個問題,當(dāng)時我一臉懵,這兩個東西為啥要混用呢?
直到后來,我遇到了這種場景。
tab切換展示組件,大部分組件展示表格,除了2個tab需要展示不同,這個不同,怎么處理呢?
當(dāng)然可以直接改封裝的tab組件,v-if條件渲染嘛
那如果后面再有其他需求,tab組件繼續(xù)寫if么
這個時候,組件就過于冗余啦
那怎么讓組件統(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é)點 _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語法是不會有對應(yīng)的指令的,所以我們就要實現(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
其實就是判斷語句,用&&或三元表達式
// 在vue中 <el-button v-if="show"></el-button> // 對應(yīng)jsx語法 item.render = (h, params) => { return ( this.show && <el-button></el-button> ) }
v-for
其實就是循環(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后大寫即可,見上個例子
到此這篇關(guān)于如何在vue中使用jsx語法的文章就介紹到這了,更多相關(guān)vue使用jsx語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用 vue 實現(xiàn)滅霸打響指英雄消失的效果附demo
這篇文章主要介紹了使用 vue 實現(xiàn)滅霸打響指英雄消失的效果 demo,需要的朋友可以參考下2019-05-05Vue?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-08vue axios基于常見業(yè)務(wù)場景的二次封裝的實現(xiàn)
這篇文章主要介紹了vue axios基于常見業(yè)務(wù)場景的二次封裝的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09