Home > Web Front-end > JS Tutorial > Detailed explanation of how Vue supports JSX syntax

Detailed explanation of how Vue supports JSX syntax

小云云
Release: 2017-12-22 11:15:28
Original
1916 people have browsed it

Usually we use template syntax to develop vue. In fact, there is the same syntax as react, that is, the render function, which also supports jsx syntax. This article mainly introduces the detailed explanation of how Vue supports JSX syntax. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Vue’s template is actually compiled into a render function.

1. The traditional createElement method


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

is rendered as follows


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

2. Use jsx syntax

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

1.Installation


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

2.Edit the .babelrc file


##

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

The code is edited as follows


Vue.component(&#39;jsx-example&#39;, {
 render (h) { // <-- h must be in scope
  return <p id="foo">bar</p>
 }
})
Copy after login
Using h as an alias for createElement is a common convention in the Vue ecosystem, and is actually required by JSX. If h loses its role in the scope, in An error message will be triggered in the application.

Related recommendations:


The differences between JSX and HTML

Start using React and JSX_html/css_WEB-ITnose

Introductory Tutorial for Learning JSX Syntax in JavaScript’s React Framework_Basic Knowledge

The above is the detailed content of Detailed explanation of how Vue supports JSX syntax. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template