首頁 > web前端 > js教程 > 主體

vue組件jsx語法使用步奏詳解

php中世界最好的语言
發布: 2018-05-28 14:32:38
原創
2540 人瀏覽過

這次帶給大家vue組件jsx語法使用步奏詳解,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"]
}
登入後複製

#基礎範例

轉義前

<p id="foo">{this.text}</p>
登入後複製

#轉譯後

h('p', {
 attrs: {
  id: 'foo'
 }
}, [this.text])
登入後複製

Note:h函數為vue實例的$createElement方法,必須存在於jsx的作用域中,在渲染函數中必須以第一個參數傳入,如:

render (h) { // <-- h 函数必须在作用域内
 return <p id="foo">bar</p>
}
登入後複製

自動注入h函數

從3.4.0開始,在用ES2015語法宣告的方法和getter存取器中(使用function關鍵字或箭頭函數除外),babel會自動注入hconst h = this.$createElement)函數,所以可以省略(h)參數。

Vue.component('jsx-example', {
 render () { // h 会自动注入
  return <p id="foo">bar</p>
 },
 myMethod: function () { // h 不会注入
  return <p id="foo">bar</p>
 },
 someOtherMethod: () => { // h 不会注入
  return <p id="foo">bar</p>
 }
})
@Component
class App extends Vue {
 get computed () { // h 会自动注入
  return <p id="foo">bar</p>
 }
}
登入後複製

 Vue JSX 與React JSX比較

首先, Vue2.0 的vnode 格式與react不同,createElement#函數的第二個參數是一個資料對象,接受一個嵌套的對象,每個嵌套對像都會有對應的模組處理。

Vue2.0 render語法

render (h) {
 return h('p', {
  // 组件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 (
  <p
   // 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: &#39;red&#39;, fontSize: &#39;14px&#39; }}
   key="key"
   ref="ref"
   // assign the `ref` is used on elements/components with v-for
   refInFor
   slot="slot">
  </p>
 )
}
登入後複製

JSX展開運算子

支援JSX展開,外掛程式會智慧的合併資料屬性,如:

const data = {
 class: ['b', 'c']
}
const vnode = <p 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 <p {...{ directives }}/>
登入後複製
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

JS實作最簡單的查找、排序、去重演算法

如何使用jQuery實作來獲取隨機顏色

以上是vue組件jsx語法使用步奏詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!