Detailed explanation of Vue3.0 + TypeScript + Vite first experience

Detailed explanation of Vue3.0 + TypeScript + Vite first experience

Project Creation

npm:

$ npm init vite-app <project-name>
$ cd <project-name>
$ npm install
$ npm run dev

or yarn:

$ yarn create vite-app <project-name>
$ cd <project-name>
$ yarn
$ yarn dev

Project Structure

insert image description here

main.js

main.ts

In my opinion, createApp() is an instance of vue application, and createApp supports chain calls.

App.vue:

insert image description here

This is the syntax compatible with vue2.0. The following is the vue3.0 rfc writing method (still in the experimental stage).
rfc official description

setup

data

insert image description here

Setup combines the created lifecycle function of vue2.0 with data and methods (mentioned later)

Can directly export attributes (data) and methods (methods)

insert image description here

It can be seen that the current name is not responsive. Responsiveness will be introduced later.

methods

insert image description here

Methods are the same as data, export directly

Effect:

insert image description here

Composition API

ref

statement:

insert image description here

Ref can make some basic properties responsive

insert image description here

reactive

insert image description here

The above picture shows the mixed use of reactive and ref. As for the effect, please copy the code below to experience it.

<template>
 <div id="app">
 <div v-for="(item, index) in state.persons" :key="index">
  {{ item.name }} is {{ item.age }} years old</div>
 <div>
  <h3>Modify zhangsan's age</h3>
  <input type="text" v-model="zAge" />
 </div>
 </div>
</template>

<script lang="ts" setup="props, {emit}">
import { reactive, ref } from 'vue'
export const zAge = ref(12)
export const state = reactive({
 persons:
 {
  name: 'zhangsan',
  age: zAge
 },
 {
  name: 'lisi',
  age: 20
 }
 ]
})
</script>

computed

statement:

insert image description here

Effect:

insert image description here

watchEffect

statement:

insert image description here

Effect:

insert image description here

Component system

Global Registration

App.vue

insert image description here

main.js

insert image description here

Partial Registration

App.vue

insert image description here

setup

props

insert image description here

Declare the props object. In watchEffect, console.log(props.msg) is used to see the value passed by the parent component. Default values ​​and filtering of props are under study. For specific functions, please refer to the function of vue2.0 props

context

insert image description here

Component Context

emit

insert image description here

Declare the emit function. Write emit in setup="props, { emit }", otherwise an error will be reported. For specific functions, please refer to the emit function in vue2.0.

insert image description here

insert image description here

Here are some examples of using the emit function.

attrs

Researching…

slots

Researching…

vue directives

Focus on v-model, other vue instructions are the same as 2.0

v-model

insert image description here

Vue3.0 began to support multiple two-way binding parameters, which was not available in vue2.0. If there are no other attributes after v-model, then its default value inside this component is modelValue. If you want to update v-model, you need to emit('update:modelValue', data) to update the default value modelValue of v-model. If there is an attribute (dragValue) after v-model, then its value inside this component is this attribute name (dragValue). If you want to update the value of v-model:dragValue, you need to emit('update:dragValue', data) to update the custom value of v-model.

For more usage, refer to the official documentation: https://github.com/vuejs/rfcs/blob/sfc-improvements/active-rfcs/0000-sfc-script-setup.md

This is the end of this article about the initial experience of Vue3.0 + TypeScript + Vite. For more related Vue3.0 TypeScript Vite content, 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:
  • Pros and Cons of Vite and Vue CLI
  • Detailed explanation of vite2.0+vue3 mobile project
  • Detailed explanation of vite+ts to quickly build vue3 projects and introduce related features
  • Vue3.0+vite2 implements dynamic asynchronous component lazy loading
  • How to use vite to build vue3 application
  • How to add Vite support to old Vue projects

<<:  Commonplace talk about the usage of MYSQL pattern matching REGEXP and like

>>:  How to upload projects to Code Cloud in Linux system

Recommend

A brief analysis of the difference between static and self in PHP classes

Use self:: or __CLASS__ to get a static reference...

JS gets the position of the nth occurrence of a specified string in a string

Learn about similar methods for getting character...

Solution to MySQL startup successfully but not listening to the port

Problem Description MySQL is started successfully...

Practice of dynamically creating dialog according to file name in vue+el-element

Table of contents background accomplish 1. Encaps...

Interactive experience trends that will become mainstream in 2015-2016

The most important interactive design article in ...

MySQL 8.0.15 installation graphic tutorial and database basics

MySQL software installation and database basics a...

Vue uses rules to implement form field validation

There are many ways to write and validate form fi...

Several ways to use v-bind binding with Class and Style in Vue

Adding/removing classes to elements is a very com...

HTML table markup tutorial (6): dark border color attribute BORDERCOLORDARK

In a table, you can define the color of the lower...

Detailed process of installing various software in Docker under Windows

1. Install MySQL # Download mysql in docker docke...

Install and deploy java8 and mysql under centos7

Generally, learning Java and deploying projects a...

Detailed steps to install Anaconda on Linux (Ubuntu 18.04)

Anaconda is the most popular python data science ...