What is JSXJSX is a syntax extension of Javascript, JSX = Javascript + XML, that is, writing XML in Javascript. Because of this feature of JSX, it has the flexibility of Javascript and the semantics and intuitiveness of HTML. Why use JSX in Vue?Sometimes, we use render functions to abstract components. Render functions are not very clear, please refer to the official documentation, and render functions are sometimes very painful to write. createElement( 'anchored-heading', { props: { level: 1 } }, [ createElement('span', 'Hello'), ' world!' ] ) The corresponding template is as follows: <anchored-heading :level="1"> Hello world! </anchored-heading> This is obviously a thankless task, and this is where JSX comes in. To use JSX in Vue, you need to use the Babel plugin, which allows us to return to a syntax closer to the template. Next, let's start writing JSX in Vue. startQuickly create a Vue project and use vue-cli to create a project: # Just press Enter vue create vue-jsx Installation dependencies: npm install @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props Configure .babelrc: module.exports = { presets: [ '@vue/cli-plugin-babel/preset', ['@vue/babel-preset-jsx', { 'injectH': false }] ] } Basic contentHere we show some basic content written in Vue, including plain text, dynamic content, label usage, and custom component usage, which are similar to the single-file components we usually use, as shown below: render() { return ( <div> <h3>Contents</h3> {/* Plain text */} <p>hello, I am Gopal</p> {/* Dynamic content */} <p>hello { this.msg }</p> {/* Input box*/} <input /> {/* Custom components */} <myComponent></myComponent> </div> ); } Attributes/PropsAttributes binding is the same as normal HTML structure render() { return <div><input placeholder="111" /></div> } Note that if the dynamic attribute is v-bind:placeholder="this.placeholderText" before, it becomes placeholder={this.placeholderText} render() { return <input type="email" placeholder={this.placeholderText} /> } We can also expand an object render (createElement) { return ( <button {...this.largeProps}></button> ) } Like the input tag, you can batch bind attributes as follows const inputAttrs = { type: 'email', placeholder: 'Enter your email' }; render() { return <input {...{ attrs: inputAttrs }} /> } SlotsLet's see how to implement named slots and scoped slots. Named slots: The parent component is written similarly to the single-file component template, and the location to be inserted is specified by slot="header". The subcomponent specifies the name of the slot through this.$slots.header, where header is the name of the slot. Parent component: render() { {/* named slot */} <myComponent> <header slot="header">header</header> <header slot="content">content</header> <footer slot="footer">footer</footer> </myComponent> } Subcomponents: render() { return ( <div> {/* Plain text */} <p>I am a custom component</p> {this.$slots.header} {this.$slots.content} {this.$slots.footer} </div> ); } Scoped slots: In the child component, {this.$scopedSlots.test({ user: this.user })} specifies that the name of the slot is test, and passes the user to the parent component. When the parent component writes the subcomponent tag, it specifies the insertion position as test through the scopedSlots value, and obtains the user value passed in by the subcomponent in the callback function. Parent component: render() { {/* named slot scope slot*/} <myComponent { ...{ scopedSlots: { test: ({user}) => ( <div>{user.name}</div> ) } } }> </myComponent> Subcomponents: render() { return ( <div> {this.$scopedSlots.test({ user: this.user })} </div> ); } instructionCommon instructions are as follows: render() { {/* instructions */} {/* v-model */} <div><input vModel={this.newTodoText} /></div> {/* v-model and modifiers*/} <div><input vModel_trim={this.tirmData} /></div> {/* v-on listener event*/} <div><input vOn:input={this.inputText} /></div> {/* v-on listener events and modifiers*/} <div><input vOn:click_stop_prevent={this.inputText} /></div> {/* v-html */} <p domPropsInnerHTML={html} /> } Functional ComponentsA functional component is a stateless, instanceless component. For details, see the official website. Create a new FunctionalComponent.js file with the following content: export default ({ props }) => <p>hello {props.message}</p> The parent component is called as follows: import funComponent from './FunctionalComponent' ... render() { return {/* Functional component*/} <funComponent message="Gopal"></funComponent> } The above is the details of how to use JSX in Vue. For more information about using JSX in Vue, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Solve the problem of being unable to log in when installing MySQL on mac using homebrew
>>: Example of how to start a container with multiple network interfaces using docker
1. Introduction I wrote an article before: The pr...
1. Command Introduction The ifconfig (configure a...
If the server data is not encrypted and authentic...
This section starts with the details of text modi...
Table of contents mapState mapGetters mapMutation...
Table of contents 1. What kind of backup is a dat...
1. Zabbix backup [root@iZ2zeapnvuohe8p14289u6Z /]...
1 The select tag must be closed <select><...
0. Background Hardware: Xiaomi Notebook Air 13/In...
Table of contents introduce Prepare Download syst...
Port 80 is also configured. First enter the firew...
Table of contents 1. Background Architecture Prob...
1. The div css mouse hand shape is cursor:pointer;...
Table of contents 1:mysql execution process 1.1: ...
Effect: CSS style: <style type="text/css&...