Home > Web Front-end > JS Tutorial > body text

Introduction to the use of jsx syntax of vue components

不言
Release: 2018-07-04 11:57:47
Original
1330 people have browsed it

This article mainly introduces the specific use of jsx syntax of vue components. The content is quite good. Now I will share it with you and give it as a reference.

If you use the render function to write more complex vue components, it will be unfriendly to readability and maintainability, and using jsx will bring us back to a syntax closer to templates. The babel translator will translate jsx into render function for rendering.

Configuration

Requires babel plug-in

Installation

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

.babelrc configuration

Add transform-vue-jsx in plugins

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

Basic example

Before escaping

{this.text}

Copy after login

After translation

h('p', {
 attrs: {
  id: 'foo'
 }
}, [this.text])
Copy after login

Note: The h function is the $createElement method of the vue instance. It must exist in the scope of jsx and must be used in the rendering function. Pass in the first parameter, such as:

render (h) { // <-- h 函数必须在作用域内
 return 

bar

}
Copy after login

Automatically inject h function

Starting from 3.4.0, babel will automatically inject h in methods declared with ES2015 syntax and getter accessors (except when using function keywords or arrow functions) (const h = this.$createElement) function, so the (h) parameter can be omitted.

Vue.component('jsx-example', {
 render () { // h 会自动注入
  return 

bar

}, myMethod: function () { // h 不会注入 return

bar

}, someOtherMethod: () => { // h 不会注入 return

bar

} }) @Component class App extends Vue { get computed () { // h 会自动注入 return

bar

} }
Copy after login

Comparison between Vue JSX and React JSX

First, the vnode of Vue2.0 The format is different from react. The second parameter of the createElement function is a data object, which accepts a nested object. Each nested object will have a corresponding module for processing.

Vue2.0 render syntax

render (h) {
 return h('p', {
  // 组件props
  props: {
   msg: 'hi'
  },
  // 原生HTML属性
  attrs: {
   id: 'foo'
  },
  // DOM props
  domProps: {
   innerHTML: 'bar'
  },
  // 事件是嵌套在`on`下面的,所以将不支持修饰符,如:`v-on:keyup.enter`,只能在代码中手动判断keyCode
  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'
 })
}
Copy after login

Corresponding Vue2.0 JSX syntax

render (h) {
 return (
  

) }
Copy after login

JSX expansion operator

supports JSX expansion, and the plug-in will intelligently merge data attributes, such as:

const data = {
 class: ['b', 'c']
}
const vnode = 

Copy after login

The merged data is:

{ class: ['a', 'b', 'c'] }
Copy after login

Vue command

JSX does not support most Vue built-in commands. The only exception is v-show, which can be used v-show={value} syntax. Most instructions can be implemented programmatically. For example, v-if is a ternary expression, v-for is a array.map(), etc. .

If it is a custom instruction, you can use the v-name={value} syntax, but the modified syntax does not support the instruction parameters arguments and modifiers modifier. There are two solutions:

Pass all the content as an object, such as: v-name={{ value, modifier: true }}

Use the native vnode instruction data format, such as:

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

return 

Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's learning, For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to Vue-based lazy loading plug-in vue-view-lazy

vue component name Introduction

jquery to achieve the effect of horizontal scrolling of images

The above is the detailed content of Introduction to the use of jsx syntax of vue components. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!