成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

用Jsx寫Vue組件

開發 前端
下面我們要講的是如何在vue里面寫jsx,知道react的人應該都知道jsx,jsx的一個特性就是非常靈活,雖然有的人覺得jsx很丑陋,把邏輯都寫到模版的感覺,但蘿卜青菜各有所愛,適合自己適合團隊的就是最好的。

 

前言

我們平常寫vue的組件時,一般都是用的是模版,這種方式看起來比較簡潔,而且vue作者也推薦使用這個方式,但是這種方式也有一些它的弊端,例如模版調試麻煩,或者在一些場景下模版描述可能沒那么簡單和方便。

下面我們要講的是如何在vue里面寫jsx,知道react的人應該都知道jsx,jsx的一個特性就是非常靈活,雖然有的人覺得jsx很丑陋,把邏輯都寫到模版的感覺,但蘿卜青菜各有所愛,適合自己適合團隊的就是***的。

在使用jsx之前我們需要安裝一個babel插件(babel-plugin-transform-vue-jsx )

安裝方式:

  1. npm install\ 
  2.  
  3.   babel-plugin-syntax-jsx\ 
  4.  
  5.   babel-plugin-transform-vue-jsx\ 
  6.  
  7.   babel-helper-vue-jsx-merge-props\ 
  8.  
  9.   babel-preset-es2015\ 
  10.  
  11.   --save-dev  

然后再.babelrc里面添加:

  1.  
  2. "presets": ["es2015"], 
  3.  
  4. "plugins": ["transform-vue-jsx"
  5.  
  6.  

接著我們就可以愉快地在vue里面編寫jsx了。

Test.vue

  1. <script> 
  2.  
  3. export default { 
  4.  
  5.     props: ['onClick''isShow'], 
  6.  
  7.     data() { 
  8.  
  9.         return { 
  10.  
  11.             test: 123 
  12.  
  13.         }; 
  14.  
  15.     }, 
  16.  
  17.     render() { 
  18.  
  19.         return ( 
  20.  
  21.             <div class="test" onClick={ this.onClick }> 
  22.  
  23.                 { this.test } 
  24.  
  25.                 { this.isShow + '' } 
  26.  
  27.             </div> 
  28.  
  29.         ); 
  30.  
  31.     } 
  32.  
  33.  
  34. </script>  

可以看到我們把jsx寫在了render方法里面,render方法是vue2.0才支持的,用來提供對虛擬DOM的支持,也就是說只有vue2.0才支持jsx語法轉換。

這里要注意的一點是vue里面編寫jsx和在react里面的jsx語法還是有一點不一樣的。

一下是一段覆蓋大部分語法的vue jsx代碼:

  1. render (h) { 
  2.  
  3.   return ( 
  4.  
  5.     <div 
  6.  
  7.       // normal attributes or component props. 
  8.  
  9.       id="foo" 
  10.  
  11.       // DOM properties are prefixed with `domProps` 
  12.  
  13.       domPropsInnerHTML="bar" 
  14.  
  15.       // event listeners are prefixed with `onor `nativeOn` 
  16.  
  17.       onClick={this.clickHandler} 
  18.  
  19.       nativeOnClick={this.nativeClickHandler} 
  20.  
  21.       // other special top-level properties 
  22.  
  23.       class={{ foo: true, bar: false }} 
  24.  
  25.       style={{ color: 'red', fontSize: '14px' }} 
  26.  
  27.       key="key" 
  28.  
  29.       ref="ref" 
  30.  
  31.       // assign the `ref` is used on elements/components with v-for 
  32.  
  33.       refInFor 
  34.  
  35.       slot="slot"
  36.  
  37.     </div> 
  38.  
  39.   ) 
  40.  
  41.  

可以看到DOM屬性要加domProps前綴,但這里lass和style卻不需要,因為這兩個是特殊的模塊,而且react的class用的是className,vue卻用的class。事件監聽是以“on”或者“nativeOn”為開始。

實際上vue2.0的模版***都會被編譯為render方法,所以模版聲明的組件和jsx聲明的組件***都是一樣的。

上面的jsx***會被編譯成下面這樣:

  1. render (h) { 
  2.  
  3.   return h('div', { 
  4.  
  5.     // Component props 
  6.  
  7.     props: { 
  8.  
  9.       msg: 'hi' 
  10.  
  11.     }, 
  12.  
  13.     // normal HTML attributes 
  14.  
  15.     attrs: { 
  16.  
  17.       id: 'foo' 
  18.  
  19.     }, 
  20.  
  21.     // DOM props 
  22.  
  23.     domProps: { 
  24.  
  25.       innerHTML: 'bar' 
  26.  
  27.     }, 
  28.  
  29.     // Event handlers are nested under "on", though 
  30.  
  31.     // modifiers such as in v-on:keyup.enter are not 
  32.  
  33.     // supported. You'll have to manually check the 
  34.  
  35.     // keyCode in the handler instead
  36.  
  37.     on: { 
  38.  
  39.       click: this.clickHandler 
  40.  
  41.     }, 
  42.  
  43.     // For components only. Allows you to listen to 
  44.  
  45.     // native events, rather than events emitted from 
  46.  
  47.     // the component using vm.$emit. 
  48.  
  49.     nativeOn: { 
  50.  
  51.       click: this.nativeClickHandler 
  52.  
  53.     }, 
  54.  
  55.     // class is a special module, same API as `v-bind:class` 
  56.  
  57.     class: { 
  58.  
  59.       foo: true
  60.  
  61.       bar: false 
  62.  
  63.     }, 
  64.  
  65.     // style is also same as `v-bind:style` 
  66.  
  67.     style: { 
  68.  
  69.       color: 'red'
  70.  
  71.       fontSize: '14px' 
  72.  
  73.     }, 
  74.  
  75.     // other special top-level properties 
  76.  
  77.     key'key'
  78.  
  79.     ref: 'ref'
  80.  
  81.     // assign the `ref` is used on elements/components with v-for 
  82.  
  83.     refInFor: true
  84.  
  85.     slot: 'slot' 
  86.  
  87.   }) 
  88.  
  89.  

這也意味著兩種形式的組件是可以相互引用的。

有時候我們難免會在模版里引入jsx編寫的vue組件或者在jsx編寫的vue組件里引入模版組件,這里還是有些需要注意的事項:

1.在模版里面引入jsx的組件,可以通過components引用,另外props的編寫從駝峰式改為連接符:

  1. <template> 
  2.  
  3.   <div class="wrapper"
  4.  
  5.     <Test :on-click="clickHandler" :is-show="show"></Test> 
  6.  
  7.   </div> 
  8.  
  9. </template> 
  10.  
  11. <script> 
  12.  
  13. import Test from './Test.vue'
  14.  
  15. export default { 
  16.  
  17.   name'hello'
  18.  
  19.   components: { 
  20.  
  21.     Test 
  22.  
  23.   }, 
  24.  
  25.   data() { 
  26.  
  27.     return { 
  28.  
  29.       msg: 'Welcome to Your Vue.js App'
  30.  
  31.       show: true 
  32.  
  33.     }; 
  34.  
  35.   }, 
  36.  
  37.   methods: { 
  38.  
  39.     clickHandler(){ 
  40.  
  41.       this.show = !this.show; 
  42.  
  43.     } 
  44.  
  45.   } 
  46.  
  47. }; 
  48.  
  49. </script>  

2.在jsx里面引入vue模版組件,這里沒有什么要注意的,除了連接符式的屬性要轉換成駝峰式,還有一個需要注意的是指令,如果用了jsx,那么內置的指令都不會生效(除了v-show),好在內置指令大部分都可以用jsx描述。那么自定義指令要怎么用呢?

自定義指令可以使用“v-name={value}”語法,如果要支持指令參數和modifier可以用“v-name={{ value, modifier: true }}”語法:

  1. <script> 
  2.  
  3. import Vue from 'vue'
  4.  
  5. Vue.directive('my-bold', { 
  6.  
  7.   inserted: function (el) { 
  8.  
  9.     el.style.fontWeight = 900; 
  10.  
  11.   } 
  12.  
  13. }) 
  14.  
  15. export default { 
  16.  
  17.     props: ['onClick''isShow'], 
  18.  
  19.     data() { 
  20.  
  21.         return { 
  22.  
  23.             test: 123 
  24.  
  25.         }; 
  26.  
  27.     }, 
  28.  
  29.     methods: { 
  30.  
  31.         afterLeave() { 
  32.  
  33.             console.log('afterLeave'
  34.  
  35.         } 
  36.  
  37.     }, 
  38.  
  39.     render() { 
  40.  
  41.         const directives = [ 
  42.  
  43.             { name'my-bold', value: 666, modifiers: { abc: true } } 
  44.  
  45.         ]; 
  46.  
  47.         return ( 
  48.  
  49.             <transition onAfterLeave={this.afterLeave} name="fade"
  50.  
  51.                 <div class="test" onClick={this.onClick} v-show={ this.isShow } v-my-bold> 
  52.  
  53.                     {this.test} 
  54.  
  55.                     {this.isShow + ''
  56.  
  57.                 </div> 
  58.  
  59.             </transition> 
  60.  
  61.         ); 
  62.  
  63.     } 
  64.  
  65.  
  66. </script> 
  67.  
  68. <style> 
  69.  
  70. .fade-enter-active, .fade-leave-active { 
  71.  
  72.   transition: opacity .5s 
  73.  
  74.  
  75. .fade-enter, .fade-leave-to { 
  76.  
  77.   opacity: 0 
  78.  
  79.  
  80. </style>  

我們還可以用原生vnode的數據格式使用自定義指令:

  1. const directives = [ 
  2.  
  3. name'my-dir', value: 123, modifiers: { abc: true } } 
  4.  
  5.  
  6. return <div {...{ directives }}/>  

擴展

如果有人覺得在vue組件里面要寫data,props,computed和methods不夠優雅,可以參考下這個插件vue-class-component,它能讓你使用ES6的class和ES7的裝飾器編寫vue組件。

相關鏈接

babel-plugin-transform-vue-jsx(https://github.com/vuejs/babel-plugin-transform-vue-jsxhttps://github.com/vuejs/babel-plugin-transform-vue-jsx) 

責任編輯:龐桂玉 來源: 前端大全
相關推薦

2024-02-02 08:33:00

Vue模板性能

2022-11-01 11:55:27

ReactVue3

2020-02-10 10:23:03

VueJSX前端

2024-01-11 10:22:20

AI代碼生成工具前端

2023-03-30 11:50:34

2021-07-19 10:32:17

Vue倒計時組件前端

2023-04-18 09:17:40

父子組件Vue

2024-09-05 08:50:11

2022-12-19 08:17:36

ReactReconciler

2022-02-08 15:55:00

Vue組件庫Vue Demi

2023-07-25 14:24:33

元素JSX解析器

2017-08-07 16:39:03

JSX動態數據

2024-04-16 12:05:27

Vue3前端

2019-05-05 11:02:07

vscodevue前端

2023-08-07 08:52:53

Vue組件Props 命名

2024-01-09 08:34:56

Vue3.js組件通信

2024-07-10 10:38:58

Vue組件函數

2020-04-03 10:57:09

文檔分支項目

2024-03-29 08:32:01

Node.jsNext.js組件

2023-12-08 13:16:00

CSSJSXStyleX
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 男女在线免费观看 | 成人精品在线视频 | 在线免费观看a级片 | 精品久久久一区二区 | 久久香焦| 色欧美片视频在线观看 | 亚洲激情第一页 | 久久综合狠狠综合久久 | 久久午夜剧场 | 国内精品视频在线观看 | 伊人狠狠干 | 日韩在线资源 | 中文字幕加勒比 | 亚洲欧美激情网 | 影视一区 | 国产成人精品免费视频大全最热 | 毛片a级| 日本精品久久久一区二区三区 | av在线影院 | 欧美激情精品久久久久久 | 国产精品成人一区二区三区 | 亚洲区在线 | 一级做a爰片性色毛片16美国 | 99热这里都是精品 | 久久精品一区 | 99爱视频| 香蕉视频在线播放 | 色天堂影院 | 黄色大片免费看 | 99久久久久 | 91网站在线观看视频 | 国产精品久久视频 | 福利视频亚洲 | www.嫩草 | 日日夜夜精品 | 污视频在线免费观看 | 粉色午夜视频 | 国产九九精品视频 | 一区二区中文 | 亚洲一区二区三区视频 | 日本在线看片 |