项目原始demo,不改动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

README.md 5.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # babel-plugin-transform-vue-jsx [![CircleCI](https://img.shields.io/circleci/project/vuejs/babel-plugin-transform-vue-jsx.svg?maxAge=2592000)](https://circleci.com/gh/vuejs/babel-plugin-transform-vue-jsx)
  2. > Babel plugin for Vue 2.0 JSX
  3. ### Requirements
  4. - Assumes you are using Babel with a module bundler e.g. Webpack, because the spread merge helper is imported as a module to avoid duplication.
  5. - This is mutually exclusive with `babel-plugin-transform-react-jsx`.
  6. ### Usage
  7. ``` bash
  8. npm install\
  9. babel-plugin-syntax-jsx\
  10. babel-plugin-transform-vue-jsx\
  11. babel-helper-vue-jsx-merge-props\
  12. babel-preset-env\
  13. --save-dev
  14. ```
  15. In your `.babelrc`:
  16. ``` json
  17. {
  18. "presets": ["env"],
  19. "plugins": ["transform-vue-jsx"]
  20. }
  21. ```
  22. The plugin transpiles the following JSX:
  23. ``` jsx
  24. <div id="foo">{this.text}</div>
  25. ```
  26. To the following JavaScript:
  27. ``` js
  28. h('div', {
  29. attrs: {
  30. id: 'foo'
  31. }
  32. }, [this.text])
  33. ```
  34. Note the `h` function, which is a shorthand for a Vue instance's `$createElement` method, must be in the scope where the JSX is. Since this method is passed to component render functions as the first argument, in most cases you'd do this:
  35. ``` js
  36. Vue.component('jsx-example', {
  37. render (h) { // <-- h must be in scope
  38. return <div id="foo">bar</div>
  39. }
  40. })
  41. ```
  42. ### `h` auto-injection
  43. Starting with version 3.4.0 we automatically inject `const h = this.$createElement` in any method and getter (not functions or arrow functions) declared in ES2015 syntax that has JSX so you can drop the `(h)` parameter.
  44. ``` js
  45. Vue.component('jsx-example', {
  46. render () { // h will be injected
  47. return <div id="foo">bar</div>
  48. },
  49. myMethod: function () { // h will not be injected
  50. return <div id="foo">bar</div>
  51. },
  52. someOtherMethod: () => { // h will not be injected
  53. return <div id="foo">bar</div>
  54. }
  55. })
  56. @Component
  57. class App extends Vue {
  58. get computed () { // h will be injected
  59. return <div id="foo">bar</div>
  60. }
  61. }
  62. ```
  63. ### Difference from React JSX
  64. First, Vue 2.0's vnode format is different from React's. The second argument to the `createElement` call is a "data object" that accepts nested objects. Each nested object will be then processed by corresponding modules:
  65. ``` js
  66. render (h) {
  67. return h('div', {
  68. // Component props
  69. props: {
  70. msg: 'hi'
  71. },
  72. // normal HTML attributes
  73. attrs: {
  74. id: 'foo'
  75. },
  76. // DOM props
  77. domProps: {
  78. innerHTML: 'bar'
  79. },
  80. // Event handlers are nested under "on", though
  81. // modifiers such as in v-on:keyup.enter are not
  82. // supported. You'll have to manually check the
  83. // keyCode in the handler instead.
  84. on: {
  85. click: this.clickHandler
  86. },
  87. // For components only. Allows you to listen to
  88. // native events, rather than events emitted from
  89. // the component using vm.$emit.
  90. nativeOn: {
  91. click: this.nativeClickHandler
  92. },
  93. // class is a special module, same API as `v-bind:class`
  94. class: {
  95. foo: true,
  96. bar: false
  97. },
  98. // style is also same as `v-bind:style`
  99. style: {
  100. color: 'red',
  101. fontSize: '14px'
  102. },
  103. // other special top-level properties
  104. key: 'key',
  105. ref: 'ref',
  106. // assign the `ref` is used on elements/components with v-for
  107. refInFor: true,
  108. slot: 'slot'
  109. })
  110. }
  111. ```
  112. The equivalent of the above in Vue 2.0 JSX is:
  113. ``` jsx
  114. render (h) {
  115. return (
  116. <div
  117. // normal attributes or component props.
  118. id="foo"
  119. // DOM properties are prefixed with `domProps`
  120. domPropsInnerHTML="bar"
  121. // event listeners are prefixed with `on` or `nativeOn`
  122. onClick={this.clickHandler}
  123. nativeOnClick={this.nativeClickHandler}
  124. // other special top-level properties
  125. class={{ foo: true, bar: false }}
  126. style={{ color: 'red', fontSize: '14px' }}
  127. key="key"
  128. ref="ref"
  129. // assign the `ref` is used on elements/components with v-for
  130. refInFor
  131. slot="slot">
  132. </div>
  133. )
  134. }
  135. ```
  136. ### Component Tip
  137. If a custom element starts with lowercase, it will be treated as a string id and used to lookup a registered component. If it starts with uppercase, it will be treated as an identifier, which allows you to do:
  138. ``` js
  139. import Todo from './Todo.js'
  140. export default {
  141. render (h) {
  142. return <Todo/> // no need to register Todo via components option
  143. }
  144. }
  145. ```
  146. ### JSX Spread
  147. JSX spread is supported, and this plugin will intelligently merge nested data properties. For example:
  148. ``` jsx
  149. const data = {
  150. class: ['b', 'c']
  151. }
  152. const vnode = <div class="a" {...data}/>
  153. ```
  154. The merged data will be:
  155. ``` js
  156. { class: ['a', 'b', 'c'] }
  157. ```
  158. ### Vue directives
  159. Note that almost all built-in Vue directives are not supported when using JSX, the sole exception being `v-show`, which can be used with the `v-show={value}` syntax. In most cases there are obvious programmatic equivalents, for example `v-if` is just a ternary expression, and `v-for` is just an `array.map()` expression, etc.
  160. For custom directives, you can use the `v-name={value}` syntax. However, note that directive arguments and modifiers are not supported using this syntax. There are two workarounds:
  161. 1. Pass everything as an object via `value`, e.g. `v-name={{ value, modifier: true }}`
  162. 2. Use the raw vnode directive data format:
  163. ``` js
  164. const directives = [
  165. { name: 'my-dir', value: 123, modifiers: { abc: true } }
  166. ]
  167. return <div {...{ directives }}/>
  168. ```