最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue組件jsx語(yǔ)法的具體使用

 更新時(shí)間:2018年05月21日 10:15:09   作者:joenlee  
本篇文章主要介紹了vue組件jsx語(yǔ)法的具體使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

如果使用render函數(shù)來(lái)寫(xiě)比較復(fù)雜的vue組件,對(duì)于可讀性和可維護(hù)性都很不友好,而使用jsx就會(huì)讓我們回到更接近于模板的語(yǔ)法。babel轉(zhuǎn)譯器會(huì)將jsx轉(zhuǎn)譯為render函數(shù)渲染。

配置

需要用到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"]
}

基礎(chǔ)示例

轉(zhuǎn)義前

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

轉(zhuǎn)譯后

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

Note:h函數(shù)為vue實(shí)例的$createElement方法,必須存在于jsx的作用域中,在渲染函數(shù)中必須以第一個(gè)參數(shù)傳入,如:

render (h) { // <-- h 函數(shù)必須在作用域內(nèi)
 return <div id="foo">bar</div>
}

自動(dòng)注入h函數(shù)

從3.4.0開(kāi)始,在用ES2015語(yǔ)法聲明的方法和getter訪問(wèn)器中(使用function關(guān)鍵字或箭頭函數(shù)除外),babel會(huì)自動(dòng)注入hconst h = this.$createElement)函數(shù),所以可以省略(h)參數(shù)。

Vue.component('jsx-example', {
 render () { // h 會(huì)自動(dòng)注入
  return <div id="foo">bar</div>
 },
 myMethod: function () { // h 不會(huì)注入
  return <div id="foo">bar</div>
 },
 someOtherMethod: () => { // h 不會(huì)注入
  return <div id="foo">bar</div>
 }
})

@Component
class App extends Vue {
 get computed () { // h 會(huì)自動(dòng)注入
  return <div id="foo">bar</div>
 }
}

 Vue JSX 和 React JSX對(duì)比

首先, Vue2.0 的vnode 格式與react不同,createElement函數(shù)的第二個(gè)參數(shù)是一個(gè)數(shù)據(jù)對(duì)象,接受一個(gè)嵌套的對(duì)象,每一個(gè)嵌套對(duì)象都會(huì)有對(duì)應(yīng)的模塊處理。

Vue2.0 render語(yǔ)法

render (h) {
 return h('div', {
  // 組件props
  props: {
   msg: 'hi'
  },
  // 原生HTML屬性
  attrs: {
   id: 'foo'
  },
  // DOM props
  domProps: {
   innerHTML: 'bar'
  },
  // 事件是嵌套在`on`下面的,所以將不支持修飾符,如:`v-on:keyup.enter`,只能在代碼中手動(dòng)判斷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'
 })
}

對(duì)應(yīng)的Vue2.0 JSX語(yǔ)法

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展開(kāi)運(yùn)算符

支持JSX展開(kāi),插件會(huì)智能的合并數(shù)據(jù)屬性,如:

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

合并后的數(shù)據(jù)為:

{ class: ['a', 'b', 'c'] }

Vue 指令

JSX對(duì)大多數(shù)的Vue內(nèi)建指令都不支持,唯一的例外是v-show,該指令可以使用v-show={value}的語(yǔ)法。大多數(shù)指令都可以用編程方式實(shí)現(xiàn),比如v-if就是一個(gè)三元表達(dá)式,v-for就是一個(gè)array.map()等。

如果是自定義指令,可以使用v-name={value}語(yǔ)法,但是改語(yǔ)法不支持指令的參數(shù)arguments和修飾器modifier。有以下兩個(gè)解決方法:

將所有內(nèi)容以一個(gè)對(duì)象傳入,如:v-name={{ value, modifier: true }}

使用原生的vnode指令數(shù)據(jù)格式,如:

const directives = [
 { name: 'my-dir', value: 123, modifiers: { abc: true } }
]

return <div {...{ directives }}/>

原文地址(https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

亚东县| 临沭县| 留坝县| 尼玛县| 遵义市| 襄城县| 本溪市| 澄城县| 揭西县| 西乌珠穆沁旗| 屏东市| 崇礼县| 格尔木市| 富宁县| 海丰县| 行唐县| 迁安市| 清原| 延庆县| 巴彦县| 庆云县| 巫山县| 张家口市| 全南县| 山东省| 美姑县| 伊川县| 册亨县| 抚州市| 天津市| 长岭县| 芷江| 绥江县| 青川县| 高阳县| 彭山县| 阜南县| 中山市| 静海县| 南丰县| 饶平县|