版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 6.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. # Babel Plugin JSX for Vue 3.0
  2. [![CircleCI](https://circleci.com/gh/vuejs/jsx-next.svg?style=svg)](https://circleci.com/gh/vuejs/vue-next) [![npm package](https://img.shields.io/npm/v/@vue/babel-plugin-jsx.svg?style=flat-square)](https://www.npmjs.com/package/@vue/babel-plugin-jsx)
  3. [![issues-helper](https://img.shields.io/badge/Issues%20Manage%20By-issues--helper-blueviolet?style=flat-square)](https://github.com/actions-cool/issues-helper)
  4. To add Vue JSX support.
  5. English | [简体中文](/packages/babel-plugin-jsx/README-zh_CN.md)
  6. ## Installation
  7. Install the plugin with:
  8. ```bash
  9. npm install @vue/babel-plugin-jsx -D
  10. ```
  11. Then add the plugin to your babel config:
  12. ```json
  13. {
  14. "plugins": ["@vue/babel-plugin-jsx"]
  15. }
  16. ```
  17. ## Usage
  18. ### options
  19. #### transformOn
  20. Type: `boolean`
  21. Default: `false`
  22. transform `on: { click: xx }` to `onClick: xxx`
  23. #### optimize
  24. Type: `boolean`
  25. Default: `false`
  26. enable optimization or not. It's not recommended to enable it If you are not familiar with Vue 3.
  27. #### isCustomElement
  28. Type: `(tag: string) => boolean`
  29. Default: `undefined`
  30. configuring custom elements
  31. #### mergeProps
  32. Type: `boolean`
  33. Default: `true`
  34. merge static and dynamic class / style attributes / onXXX handlers
  35. #### enableObjectSlots
  36. Type: `boolean`
  37. Default: `true`
  38. Whether to enable `object slots` (mentioned below the document) syntax". It might be useful in JSX, but it will add a lot of `_isSlot` condition expressions which increase your bundle size. And `v-slots` is still available even if `enableObjectSlots` is turned off.
  39. #### pragma
  40. Type: `string`
  41. Default: `createVNode`
  42. Replace the function used when compiling JSX expressions.
  43. ## Syntax
  44. ### Content
  45. functional component
  46. ```jsx
  47. const App = () => <div>Vue 3.0</div>;
  48. ```
  49. with render
  50. ```jsx
  51. const App = {
  52. render() {
  53. return <div>Vue 3.0</div>;
  54. },
  55. };
  56. ```
  57. ```jsx
  58. import { withModifiers, defineComponent } from "vue";
  59. const App = defineComponent({
  60. setup() {
  61. const count = ref(0);
  62. const inc = () => {
  63. count.value++;
  64. };
  65. return () => (
  66. <div onClick={withModifiers(inc, ["self"])}>{count.value}</div>
  67. );
  68. },
  69. });
  70. ```
  71. Fragment
  72. ```jsx
  73. const App = () => (
  74. <>
  75. <span>I'm</span>
  76. <span>Fragment</span>
  77. </>
  78. );
  79. ```
  80. ### Attributes / Props
  81. ```jsx
  82. const App = () => <input type="email" />;
  83. ```
  84. with a dynamic binding:
  85. ```jsx
  86. const placeholderText = "email";
  87. const App = () => <input type="email" placeholder={placeholderText} />;
  88. ```
  89. ### Directives
  90. #### v-show
  91. ```jsx
  92. const App = {
  93. data() {
  94. return { visible: true };
  95. },
  96. render() {
  97. return <input v-show={this.visible} />;
  98. },
  99. };
  100. ```
  101. #### v-model
  102. > Note: You should pass the second param as string for using `arg`.
  103. ```jsx
  104. <input v-model={val} />
  105. ```
  106. ```jsx
  107. <input v-model:argument={val} />
  108. ```
  109. ```jsx
  110. <input v-model={[val, ["modifier"]]} />
  111. ```
  112. ```jsx
  113. <A v-model={[val, "argument", ["modifier"]]} />
  114. ```
  115. Will compile to:
  116. ```js
  117. h(A, {
  118. argument: val,
  119. argumentModifiers: {
  120. modifier: true,
  121. },
  122. "onUpdate:argument": ($event) => (val = $event),
  123. });
  124. ```
  125. #### v-models (Not recommended since v1.1.0)
  126. > Note: You should pass a Two-dimensional Arrays to v-models.
  127. ```jsx
  128. <A v-models={[[foo], [bar, "bar"]]} />
  129. ```
  130. ```jsx
  131. <A
  132. v-models={[
  133. [foo, "foo"],
  134. [bar, "bar"],
  135. ]}
  136. />
  137. ```
  138. ```jsx
  139. <A
  140. v-models={[
  141. [foo, ["modifier"]],
  142. [bar, "bar", ["modifier"]],
  143. ]}
  144. />
  145. ```
  146. Will compile to:
  147. ```js
  148. h(A, {
  149. modelValue: foo,
  150. modelModifiers: {
  151. modifier: true,
  152. },
  153. "onUpdate:modelValue": ($event) => (foo = $event),
  154. bar: bar,
  155. barModifiers: {
  156. modifier: true,
  157. },
  158. "onUpdate:bar": ($event) => (bar = $event),
  159. });
  160. ```
  161. #### custom directive
  162. Recommended when using string arguments
  163. ```jsx
  164. const App = {
  165. directives: { custom: customDirective },
  166. setup() {
  167. return () => <a v-custom:arg={val} />;
  168. },
  169. };
  170. ```
  171. ```jsx
  172. const App = {
  173. directives: { custom: customDirective },
  174. setup() {
  175. return () => <a v-custom={[val, "arg", ["a", "b"]]} />;
  176. },
  177. };
  178. ```
  179. ### Slot
  180. > Note: In `jsx`, _`v-slot`_ should be replace with **`v-slots`**
  181. ```jsx
  182. const A = (props, { slots }) => (
  183. <>
  184. <h1>{ slots.default ? slots.default() : 'foo' }</h1>
  185. <h2>{ slots.bar?.() }</h2>
  186. </>
  187. );
  188. const App = {
  189. setup() {
  190. const slots = {
  191. bar: () => <span>B</span>,
  192. };
  193. return () => (
  194. <A v-slots={slots}>
  195. <div>A</div>
  196. </A>
  197. );
  198. },
  199. };
  200. // or
  201. const App = {
  202. setup() {
  203. const slots = {
  204. default: () => <div>A</div>,
  205. bar: () => <span>B</span>,
  206. };
  207. return () => <A v-slots={slots} />;
  208. },
  209. };
  210. // or you can use object slots when `enableObjectSlots` is not false.
  211. const App = {
  212. setup() {
  213. return () => (
  214. <>
  215. <A>
  216. {{
  217. default: () => <div>A</div>,
  218. bar: () => <span>B</span>,
  219. }}
  220. </A>
  221. <B>{() => "foo"}</B>
  222. </>
  223. );
  224. },
  225. };
  226. ```
  227. ### In TypeScript
  228. `tsconfig.json`:
  229. ```json
  230. {
  231. "compilerOptions": {
  232. "jsx": "preserve"
  233. }
  234. }
  235. ```
  236. ## Who is using
  237. <table>
  238. <tbody>
  239. <tr>
  240. <td align="center">
  241. <a target="_blank" href="https://www.antdv.com/">
  242. <img
  243. width="32"
  244. src="https://qn.antdv.com/logo.png"
  245. />
  246. <br>
  247. <strong>Ant Design Vue</strong>
  248. </a>
  249. </td>
  250. <td align="center">
  251. <a target="_blank" href="https://youzan.github.io/vant/#/zh-CN/">
  252. <img
  253. width="32"
  254. style="vertical-align: -0.32em; margin-right: 8px;"
  255. src="https://img.yzcdn.cn/vant/logo.png"
  256. />
  257. <br>
  258. <strong>Vant</strong>
  259. </a>
  260. </td>
  261. <td align="center">
  262. <a target="_blank" href="https://github.com/element-plus/element-plus">
  263. <img
  264. height="32"
  265. style="vertical-align: -0.32em; margin-right: 8px;"
  266. src="https://user-images.githubusercontent.com/10731096/91267529-259f3680-e7a6-11ea-9a60-3286f750de01.png"
  267. />
  268. <br>
  269. <strong>Element Plus</strong>
  270. </a>
  271. </td>
  272. <td align="center">
  273. <a target="_blank" href="https://github.com/leezng/vue-json-pretty">
  274. <img
  275. height="32"
  276. style="vertical-align: -0.32em; margin-right: 8px;"
  277. src="https://raw.githubusercontent.com/leezng/vue-json-pretty/master/static/logo.svg"
  278. />
  279. <br>
  280. <strong>Vue Json Pretty</strong>
  281. </a>
  282. </td>
  283. </tr>
  284. </tbody>
  285. </table>
  286. ## Compatibility
  287. This repo is only compatible with:
  288. - **Babel 7+**
  289. - **Vue 3+**