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.

78 lines
1.7 KiB

3 months ago
  1. ## @vue/babel-sugar-composition-api-render-instance
  2. > Ported from [luwanquan/babel-preset-vca-jsx](https://github.com/luwanquan/babel-preset-vca-jsx) by [@luwanquan](https://github.com/luwanquan)
  3. Babel syntactic sugar for replacing `this` with `getCurrentInstance()` in Vue JSX with @vue/composition-api
  4. ### Babel Compatibility Notes
  5. - This repo is only compatible with Babel 7.x
  6. ### Usage
  7. Install the dependencies:
  8. ```sh
  9. # for yarn:
  10. yarn add @vue/babel-sugar-composition-api-render-instance
  11. # for npm:
  12. npm install @vue/babel-sugar-composition-api-render-instance --save
  13. ```
  14. In your `.babelrc`:
  15. ```json
  16. {
  17. "plugins": ["@vue/babel-sugar-composition-api-render-instance"]
  18. }
  19. ```
  20. However it is recommended to use the [configurable preset](../babel-preset-jsx/README.md) instead.
  21. ### Details
  22. This plugin automatically replaces `this` in `setup()` with `getCurrentInstance()`. This is required for JSX to work in @vue/composition-api as `this` is not available in `setup()`
  23. Input:
  24. ```jsx
  25. defineComponent({
  26. setup() {
  27. return () => <MyComponent vModel={a.b} />
  28. }
  29. })
  30. ```
  31. Output (without @vue/babel-sugar-composition-api-render-instance):
  32. ```jsx
  33. defineComponent({
  34. setup() {
  35. return () => <MyComponent model={{
  36. value: a.b,
  37. callback: $$v => {
  38. this.$set(a, "b", $$v);
  39. }
  40. }} />
  41. }
  42. })
  43. ```
  44. Output (with @vue/babel-sugar-composition-api-render-instance):
  45. ```jsx
  46. import { getCurrentInstance } from "@vue/composition-api";
  47. defineComponent({
  48. setup() {
  49. const __currentInstance = getCurrentInstance();
  50. return () => <MyComponent model={{
  51. value: a.b,
  52. callback: $$v => {
  53. __currentInstance.$set(a, "b", $$v);
  54. }
  55. }} />
  56. }
  57. })
  58. ```