溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在vue組件中使用jsx語法

發布時間:2021-05-17 17:48:36 來源:億速云 閱讀:256 作者:Leah 欄目:web開發

本篇文章為大家展示了怎么在vue組件中使用jsx語法,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

配置

需要用到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方法,必須存在于jsx的作用域中,在渲染函數中必須以第一個參數傳入,如:

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

自動注入h函數

從3.4.0開始,在用ES2015語法聲明的方法和getter訪問器中(使用function關鍵字或箭頭函數除外),babel會自動注入hconst 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 }}/>

Vue的優點

Vue具體輕量級框架、簡單易學、雙向數據綁定、組件化、數據和結構的分離、虛擬DOM、運行速度快等優勢,Vue中頁面使用的是局部刷新,不用每次跳轉頁面都要請求所有數據和dom,可以大大提升訪問速度和用戶體驗。

上述內容就是怎么在vue組件中使用jsx語法,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女