SoFunction
Updated on 2025-04-04

How to use jsx syntax in vue3

background

vue3Promote use in the projectcomposition-api + setupform
Cooperatejsx+hooksForm, separate by business, clearer logic and more convenient maintenance;

grammar

The following mainly passesComparing different syntaxes of jsx and templateto achieve the same function

1. Ordinary content

Ordinary content constants, the writing method is the same

//jsx writing methodsetup() {
  return ()=><p type="email">hello</p>
}

//template writing method  <tempalte>
     <p type="email">hello</p>
  </template>

2. Dynamic variables

jsx Use braces to wrap variables, no quotes, e.g.<div age={age}>{age}</div>
template content usageDouble braces packageAttribute variables start with colonlike<div :age="age">{{age}}</div>

{} is a universal usage of jsx, which can be written in itjs expressionLoop logicOperations and so on

//jsx writing methodsetup() {
  let age = 1           
  return ()=&gt;&lt;div age={age}&gt;{age}&lt;/div&gt; //No " " package, unified is {}}

//template writing method  &lt;tempalte&gt;
     &lt;div :age="age"&gt;{{age}}&lt;/div&gt;
  &lt;/template&gt;

3. Function events

1. Basic usage

jsxandtempalteThe functions are different as follows:

  • jsxuseon+big hump form(Capitalization of the initial letter),templateuse@+short horizontal lineform
  • jsxMethod name needs to be used{}Wrap it up,tempalteuse" "Wrap up

Use Case 1:

//jsx writing methodsetup() {
  const age= ref(0);
  let inc = ()=&gt;{
      ++
   }
  return ()=&gt; &lt;div onClick={inc}&gt;{}&lt;/div&gt;
}

//template writing method  &lt;tempalte&gt;
     &lt;div @click="inc"&gt;{{age}}&lt;/div&gt;
   &lt;/template&gt;

2. Parameters advanced

jsxandtempalteAll the same:No custom parametersFunctions ofNo need to end with()
jsxIn useFunctions with parameters, you need to useArrow function wrap{()=>fn(args)}
jsxNeed helpwithModifiers,accomplish.self ,.stopEffects of modifiers

Use Case 2:

//jsx writing methodimport { withModifiers} from "vue";
...
setup() {
  const age= ref(0);
  let inc = ()=&gt;{
      ++
   }
  return ()=&gt; (
  &lt;&gt; //The root path must have a node, or use <> to represent the fragment empty node    &lt;div onClick={inc}&gt;{}&lt;/div&gt;  //No custom parameters, no () is required    &lt;div onClick={()=&gt;inc('test')}&gt;{}&lt;/div&gt;  //There are parameters, need ()=>Package    //withModifiers wrap vue modifier    &lt;div onClick={withModifiers(inc, ["stop"])}&gt;{}&lt;/div&gt; //You can also use() and so on inc function    &lt;input onKeyup=={(ev) =&gt; {     //Keyboard event enter event        //The logical part can also be written to js        if ( === 'Enter') {
           inc ();
        }
     }}&gt;&lt;/input &gt; 
  &lt;/&gt;
 )
}

//template writing method  &lt;tempalte&gt;
     &lt;div @click="inc"}&gt;{{age}}&lt;/div&gt;
     &lt;div @click="inc('test')"}&gt;{{age}}&lt;/div&gt;
     &lt;div @="inc"}&gt;{{age}}&lt;/div&gt;  //stop modifier     &lt;input @="inc"}&gt;{{age}}&lt;/input&gt;  //Keyboard event enter event  &lt;/template&gt;

4. Ref binding

There are two types of refs in vue3, one refers toBidirectional binding variable defined by ref(), and in addition, it is bound toDom tag ref reference

forref() two-way binding variable, jsx will nottemplateAutomatic processing.valueProblem, you need to manually add value
forDom tag ref reference, jsx is used directlyref(null) variable, no need to add.valuetempalteIt's useString of the same name

Use cases:

//jsx writing methodsetup() {
  const divRef=ref(null);
  const age= ref(0);  
  return ()=&gt;
   (
     &lt;div ref={divRef} &gt; //Ref reference of Dom tag        &lt;span&gt;{}&lt;/span&gt;   //ref() Bidirectional binding variable     &lt;/div&gt;
   ) 
}

//template writing method  &lt;tempalte&gt;
     &lt;div ref='divRef'&gt;  //Dom tag ref, use string with the same name        &lt;span&gt;{{age}}&lt;/span&gt;   //ref() two-way binding variable, no need for .value     &lt;/div&gt;
  &lt;/template&gt;

V. v-model syntax

Used in jsxv-model or v-modelsreplacetemplateofv-model

Components onlyA v-modelWhen using v-model syntax
Components onlyMultiple v-modelsWhen using v-model:xx syntax

Multiple v-modelsIt's OK whenv-modelsSyntax, you need to pass a two-dimensional array (The new version is no longer recommended)

Use cases:

//jsx writing methodsetup() {
  const age= ref(0);  
  const gender =ref('')
  return ()=&gt;
   (
     &lt;tz-input v-model={age} /&gt;   
     &lt;tz-input v-model:foo={age} /&gt;  //v-model with modification, recommended (v-model: modifier)     &lt;tz-input v-model={[age, "foo"]} /&gt;  //v-model with modification (new version is not recommended)
     //Multiple v-models     &lt;tz-input    //Recommended (v-model: modifier)       v-model:foo={age}
       v-model:bar={gender}
     /&gt;  
     &lt;tz-input   
       v-models={[          //Use v-models to pass a two-dimensional array (not recommended for the new version)         [age, "foo"], 
         [gender, "bar"],
         ]}
     /&gt;
   ) 
}

//template writing method  &lt;tempalte&gt;
     &lt;tz-input v-model="age" /&gt;
     &lt;tz-input ="age" /&gt;  //v-model with decoration     &lt;tz-input 
       ="age"     //Multiple v-models       ="gender"
     /&gt;
  &lt;/template&gt;

6. v-slots syntax

Use v-slots instead of v-slot in jsx (abbreviation#)

Use cases:

//jsx writing method//Method 1const App = {
  setup() {
    const slots = {
      default: () =&gt; &lt;div&gt;A&lt;/div&gt;,     //Default slot      bar: () =&gt; &lt;span&gt;B&lt;/span&gt;,       //Name slot    };
    return () =&gt; &lt;A v-slots={slots} /&gt;;
  },
};
//Method 2const App = {
  setup() {
    return () =&gt; (
      &lt;&gt;
        &lt;A&gt;
          {{
            default: () =&gt; &lt;div&gt;A&lt;/div&gt;,   //This method cannot be used for default default            bar: () =&gt; &lt;span&gt;B&lt;/span&gt;,   //Name slot          }}
        &lt;/A&gt;
        &lt;B&gt;{() =&gt; "foo"}&lt;/B&gt;
      &lt;/&gt;
    );
  },
};

//template writing method &lt;tempalte&gt;
   &lt;tempalte v-slot:bar&gt;  //Name slot, also called #bar     &lt;A /&gt;
   &lt;/template&gt;
   &lt;tempalte v-slot:default&gt;
     &lt;A /&gt;
   &lt;/template&gt;
 &lt;/template&gt;

7. v-for syntax

jsxYou can use js inMap loopTo achievetemplate v-forlogic

Use cases:

//jsx writing methodsetup() {
  const arr= ref([{label:'1'},{label:'2'},{label:'3'}]);      
  return ()=&gt;(   
     &lt;&gt;
     { (item=&gt; &lt;span key={}&gt;{}&lt;/span&gt; ) }
     &lt;/&gt;
    ) 
}

//template writing method  &lt;tempalte&gt;
     &lt;span v-for="item in arr" :key=""&gt;{{}}&lt;/span&gt; 
  &lt;/template&gt;

8. v-if syntax

jsxYou can use js inif logicThree-mean operation&&,||Wait for implementationtemplate's v-iflogic

Use cases:

//jsx writing methodsetup() {
  const show= ref(false);      
  return ()=&gt;(   
     &lt;&gt;
     {show &amp;&amp; &lt;span&gt;test vif&lt;/span&gt;}      //Use && operation     {!show &amp;&amp; &lt;span&gt;test velse&lt;/span&gt;}
     &lt;/&gt;
    ) 
}

setup() {
  const show= ref(false);      
  return ()=&gt;(   
     &lt;&gt;
      &lt;span&gt;{ ? 'test vif' : 'test velse'}&lt;/span&gt;    //Three-point operation     &lt;/&gt;
    ) 
}

setup() {
  const show= ref(false); 
  const vif=()=&gt;{
     if(show) {
        return  &lt;span&gt;test vif&lt;/span&gt; 
     }
     return  &lt;span&gt;test velse&lt;/span&gt; 
  }   
  return ()=&gt;(   
     &lt;&gt;
        vif()   // if conditional function     &lt;/&gt;
    ) 
}

//template writing method  &lt;tempalte&gt;
     &lt;span v-if="show"&gt;test vif&lt;/span&gt; 
     &lt;span v-else&gt;test velse&lt;/span&gt; 
  &lt;/template&gt;

9. v-show syntax

Use cases:

//jsx writing methodsetup() {
  const show= ref(false);      
  return ()=&gt;(   
     &lt;&gt;
      &lt;span v-show={}&gt; test vshow&lt;/span&gt;    //v-show
     &lt;/&gt;
    ) 
}

//template writing method  &lt;tempalte&gt;
    &lt;span v-show="show"&gt; test vshow &lt;/span&gt; 
  &lt;/template&gt;

10. Use of modifiers for instructions

Command usageUnderline, for example, v-loading
Use cases:

//jsx writing methodsetup() {
  const isLoading= ref(true);      
  return ()=&gt;(   
     &lt;&gt;
      &lt;span v-loading_fullscreen_lock={isLoading}&gt; loading &lt;/span&gt;    
     &lt;/&gt;
    ) 
}

//template writing method  &lt;tempalte&gt;
    &lt;span ="isLoading"&gt; loading &lt;/span&gt; 
  &lt;/template&gt;

This is the end of this article about using jsx syntax in vue3. For more related content on using jsx syntax for vue3, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!