Vue中使用JSX

作者: YLPeach | 来源:发表于2017-06-22 16:04 被阅读4268次

转载:https://github.com/vuejs/babel-plugin-transform-vue-jsx/blob/master/README.md

babel-plugin-transform-vue-jsx CircleCICircleCI

Babel plugin for Vue 2.0 JSX

Requirements

  • 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.

  • This is mutually exclusive with babel-plugin-transform-react-jsx.

Usage

npm install\
  babel-plugin-syntax-jsx\
  babel-plugin-transform-vue-jsx\
  babel-helper-vue-jsx-merge-props\
  babel-preset-es2015\
  --save-dev

In your .babelrc:

{
  "presets": ["es2015"],
  "plugins": ["transform-vue-jsx"]
}

The plugin transpiles the following JSX:

<div id="foo">{this.text}</div>

To the following JavaScript:

h('div', {
  attrs: {
    id: 'foo'
  }
}, [this.text])

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:

Vue.component('jsx-example', {
  render (h) { // <-- h must be in scope
    return <div id="foo">bar</div>
  }
})

h auto-injection

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.


Vue.component('jsx-example', {
  render () { // h will be injected
    return <div id="foo">bar</div>
  },
  myMethod: function () { // h will not be injected
    return <div id="foo">bar</div>
  },
  someOtherMethod: () => { // h will not be injected
    return <div id="foo">bar</div>
  }
})

@Component
class App extends Vue {
  get computed () { // h will be injected
    return <div id="foo">bar</div>
  }
}

Difference from React JSX

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:

render (h) {
  return h('div', {
    // Component props
    props: {
      msg: 'hi'
    },
    // normal HTML attributes
    attrs: {
      id: 'foo'
    },
    // DOM props
    domProps: {
      innerHTML: 'bar'
    },
    // Event handlers are nested under "on", though
    // modifiers such as in v-on:keyup.enter are not
    // supported. You'll have to manually check the
    // keyCode in the handler instead.
    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'
  })
}

The equivalent of the above in Vue 2.0 JSX is:

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>
  )
}

Component Tip

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:

import Todo from './Todo.js'

export default {
  render (h) {
    return <Todo/> // no need to register Todo via components option
  }
}

JSX Spread

JSX spread is supported, and this plugin will intelligently merge nested data properties. For example:

const data = {
  class: ['b', 'c']
}
const vnode = <div class="a" {...data}/>

转载:https://github.com/vuejs/babel-plugin-transform-vue-jsx/blob/master/README.md
The merged data will be:

{ class: ['a', 'b', 'c'] }

Vue directives

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.

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:

  1. Pass everything as an object via value, e.g. v-name={{ value, modifier: true }}

  2. Use the raw vnode directive data format:

const directives = [
  { name: 'my-dir', value: 123, modifiers: { abc: true } }
]

return <div {...{ directives }}/>

相关文章

  • vue自定义指令jsx语法

    vue中自定义指令使用jsx语法实现

  • vue3 的一些知识点

    vue3 支持 jsx 安装依赖 vite.config.ts 中引用插件 使用 jsx CSS Module 任...

  • vue-cli4.0 - jsx

    官方jsx使用文档[https://github.com/vuejs/jsx#installation] Vue ...

  • vue-渲染和jsx-2020-07-13

    本文记录了vue中使用渲染和jsx的基础使用,更多深入研究待后续项目使用过程中 使用渲染 这里统一采用vue-cl...

  • vue 与react技术选形一些对比

    模板的区别 vue使用模板是初是由angular提出 react 使用jsx(jsx已经是标准) 模板语法上,我更...

  • 在vue中使用jsx

    在vue中除了可以使用render函数,其实也可以用jsx 安装环境 在babel的配置文件中添加配置 使用

  • (二)react入门

    react使用的是JSX语法,和vue、小程序语法类似 class属性在react中要写成className 创建...

  • VUE JSX IDEA

    在 idea ide vue 文件中对 jsx 格式支持

  • vue jsx写法记录

    [toc] 通过本文, 你可以学到一些vue中jsx的语法。 vue更加推荐使用模板开发组件,但是在一些基础组件开...

  • (React)认识React语法

    一、JSX语法 在React中,没有使用HTML标签来绘制UI,而是使用了JSX语法。 当然JSX语法中,也可以嵌...

网友评论

    本文标题:Vue中使用JSX

    本文链接:https://www.haomeiwen.com/subject/cqagtxtx.html