Explain how Vue supports JSX syntax and VueJSX syntax

Source: Internet
Author: User

Explain how Vue supports JSX syntax and VueJSX syntax

We usually use the template syntax for vue development. In fact, there is also the same syntax as react, that is, the render function, which also supports the jsx syntax.

The Vue template is actually compiled into the render function.

1. Traditional createElement Method

createElement( 'anchored-heading', {  props: {   level: 1  } }, [  createElement('span', 'Hello'),  ' world!' ])

Rendering as below

<anchored-heading :level="1">  <span>Hello</span> world!</anchored-heading>

2. Use jsx syntax

This is why a Babel plugin plug-in is used to use the JSX syntax in Vue. It allows us to return to the syntax closer to the template.

1. Install

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

2. Edit the. babelrc File

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

Edit the Code as follows:

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

Using h as the alias of createElement is a common practice in the Vue ecosystem and is actually required by JSX. If h is ineffective in the scope, an error will be reported in the application.

Official Website instructions: https://cn.vuejs.org/v2/guide/render-function.html#JSX

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.