vue元件jsx語法
阿新 • • 發佈:2018-11-26
原文地址(https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage)
如果使用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"]
}
基礎示例
轉義前
<div id="foo">{this.text}</div>
轉譯後
h('div', {
attrs: {
id: 'foo'
}
}, [this.text])
Note:h
函式為vue例項的$createElement
render (h) { // <-- h 函式必須在作用域內
return <div id="foo">bar</div>
}
自動注入h
函式
從3.4.0開始,在用ES2015語法宣告的方法和getter
訪問器中(使用function
關鍵字或箭頭函式除外),babel會自動注入h
(const h = this.$createElement
)函式,所以可以省略(h)
引數。
Vue.component('jsx-example', {
render () { // h 會自動注入
return <div id="foo">bar</div>
},
myMethod: function () { // h 不會注入
return <div id="foo">bar</div>
},
someOtherMethod: () => { // h 不會注入
return <div id="foo">bar</div>
}
})
@Component
class App extends Vue {
get computed () { // h 會自動注入
return <div id="foo">bar</div>
}
}
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 (
<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>
)
}
JSX展開運算子
支援JSX展開,外掛會智慧的合併資料屬性,如:
const data = {
class: ['b', 'c']
}
const vnode = <div 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 <div {...{ directives }}/>