Highly recommended! Setup syntax sugar in Vue 3.2

Highly recommended! Setup syntax sugar in Vue 3.2

Previous

As a front-end programmer, you must be familiar with Vue 3. As one of the most popular front-end frameworks nowadays, many people use it as an entry-level framework.

However, even though Vue 3 has been put into use a long time ago, some people inevitably complain that Vue 3 has too many and too complicated knowledge points and is updated too quickly. Recently, Vue 3 finalized a new technology: script-setup syntax sugar.

1. What is setup syntax sugar

Initially, Vue 3.0 exposed variables had to be returned before they could be used in the template;

Now we only need to add setup in the script tag. Components only need to be imported without registration, properties and methods do not need to be returned, there is no need to write a setup function, and there is no need to write export default. Even custom instructions can be automatically obtained in our template.

<template>
  <my-component :num="num" @click="addNum" />
</template>

<script setup>
  import { ref } from 'vue';
  import MyComponent from './MyComponent .vue';

  // Write like in normal setup, but don't need to return any variables const num = ref(0) // num defined here can be used directly const addNum = () => { // Functions can also be referenced directly, without returning num.value++ in return
  }
</script>

// Must use camelCase

2. Use the setup component to automatically register

In script setup, the introduced components can be used directly without registering them through components, and the name of the current component cannot be specified. It will automatically be based on the file name, which means there is no need to write the name attribute. Example:

<template>
    <zi-hello></zi-hello>
</template>

<script setup>
  import ziHello from './ziHello'
</script>

3. Add new API after using setup

Since there is no setup function, how do we get props and emit?

The setup script syntax sugar provides a new API for us to use

3.1 defineProps

Used to receive props from the parent component. Example:

Parent component code

<template>
  <div class="die">
    <h3>I am the parent component</h3>
    <zi-hello :name="name"></zi-hello>
  </div>
</template>

<script setup>
  import ziHello from './ziHello'
  
  import {ref} from 'vue'
  let name = ref('赵小磊========')
</script>

Subcomponent code

<template>
  <div>
    I am a subcomponent {{name}} // Zhao Xiaolei========
  </div>
</template>

<script setup>
  import { defineProps } from 'vue'

  defineProps({
   name:{
     type:String,
     default:'I am the default value'
   }
 })
</script>

3.2 defineEmits

Child components pass events to parent components. Example:

Subcomponents

<template>
  <div>
    I am the child component {{name}}
    <button @click="ziupdata">Button</button>
  </div>
</template>

<script setup>
  import { defineEmits } from 'vue'

  //Custom function, parent component can trigger const em=defineEmits(['updata'])
  const ziupdata=()=>{
    em("updata",'I am the value of the child component')
  }

</script>

Parent Component

<template>
  <div class="die">
    <h3>I am the parent component</h3>
    <zi-hello @updata="updata"></zi-hello>
  </div>
</template>

<script setup>
  import ziHello from './ziHello'
  
  const updata = (data) => {
    console.log(data); //I am the value of the child component}
</script>

3.3 defineExpose

The component exposes its own properties, which can be obtained in the parent component. Example:

Subcomponents

<template>
  <div>
    I am a child component</div>
</template>

<script setup>
  import { defineExpose, reactive, ref } from 'vue'
  let ziage = ref(18)
  let ziname = reactive({
    name:'Zhao Xiaolei'
  })
  //Exposure variables defineExpose({
    ziage,
    ziname
  })
</script>

Parent Component

<template>
  <div class="die">
    <h3 @click="isclick">I am the parent component</h3>
    <zi-hello ref="zihello"></zi-hello>
  </div>
</template>

<script setup>
  import ziHello from './ziHello'
  import {ref} from 'vue'
  const zihello = ref()

  const isclick = () => {
    console.log('Receive the value exposed by ref',zihello.value.ziage)
    console.log('Receive the value exposed by reactive',zihello.value.ziname.name)
  }
</script>

The result obtained by the parent component

How to enable setup syntax sugar in vue3 project

https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar

1. First, close the vetur plugin of the editor and open Volar

2. Create a new tsconfig.json / jsconfig.json file and add "strict": true and "moduleResolution": "node" configuration items in compilerOptions.

Summarize:

The above is the understanding and knowledge of setup syntax sugar. This article about setup syntax sugar in Vue3.2 is introduced here. For more relevant content about setup syntax sugar in Vue3.2, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the setup syntax sugar example for Vue3 update
  • Vue3 - setup script usage experience sharing
  • Front-end vue3 setup tutorial
  • Application example of setup-script in vue3
  • Vue3 based on script setup syntax $refs usage
  • setup+ref+reactive implements vue3 responsiveness
  • Vue3 setup() advanced usage examples detailed explanation
  • About the use of setup function in vue3

<<:  Implementation code for adding links to FLASH through HTML (div layer)

>>:  Introduction to major browsers and their kernels

Recommend

The marquee element implements effects such as scrolling fonts and pictures

The marquee element can achieve simple font (image...

Detailed installation and configuration tutorial of mysql5.7 on CentOS

Install Make sure your user has permission to ins...

Summary of methods to include file contents in HTML files

In the forum, netizens often ask, can I read the ...

Five guidelines to help you write maintainable CSS code

1. Add a comment block at the beginning of the sty...

Detailed explanation of the use of MySQL mysqldump

1. Introduction to mysqldump mysqldump is a logic...

How to prohibit vsftpd users from logging in through ssh

Preface vsftp is an easy-to-use and secure ftp se...

XHTML Getting Started Tutorial: Simple Web Page Creation

Create your first web page in one minute: Let'...

CSS clear float clear:both example code

Today I will talk to you about clearing floats. B...

Summary of Mysql-connector-java driver version issues

Mysql-connector-java driver version problem Since...

A brief discussion on the principle of shallow entry and deep exit of MySQL

Table of contents 1. Overview of the page 2. Infi...

Example code for implementing photo stacking effect with CSS

Achieve results step 1. Initial index.html To bui...

Two ways to build a private GitLab using Docker

The first method: docker installation 1. Pull the...

Share 13 basic syntax of Typescript

Table of contents 1. What is Ts 2. Basic Grammar ...

A small collection of html Meta tags

<Head>……</head> indicates the file he...