Vue3 + TypeScript Learning1. Environment Configuration 1.1 Install the latest Vue scaffoldingnpm install -g @vue/cli yarn global add @vue/cli 1.2 Create a Vue3 projectvue create projectName 1.3 Upgrading existing Vue 2 projects to Vue 3 vue add typescript 2. Attack on Vue32.1 Vue 2 Limitations
2.2 How Vue 3 solves the limitations of Vue 2
[Write a composite function in Vue3 and use Compositon Api setUp to solve it]
3. Vue3 Composition API3.1 About Composition API In Vue3, you can also write components without using So you can continue to use the Vue2 way to write components. 3.2 When to use the Composition API TypeScript` supports writing large components, you can use 4. Essential foundation of Composition API 4.1 What is setup is another implementation used to configure component state. The state defined in setup, the method must return if it is to be used in the template Notice:
4.2 ref Create responsive variablesIn Vue2, we define a responsive variable directly in data and use the variable in the template. If we use the composition api, we have to use ref in setup to create a responsive variable and return it before we can use it on the page. use:
The benefits of this:
<template> <div> <h1>{{title}}</h1> </div> </template> <script> import {ref,defineComponent} from 'vue' export default defineComponent({ setup () { // Define responsive variables const title = ref('Front-end self-study community') // Access the variable console.log(title.value) // Return variable return {title} } }) </script> 4.3 LifecycleThe Composition API lifecycle hooks have the same name as the Vue 2 optional lifecycle hooks, except that when using the Composition API, they are prefixed with on, onMounted. There are two mounted life hooks in the code below. Which one do you think will be executed first? setup () { // Define responsive variables const title = ref('Front-end self-study community') console.log(title) // Return variable function getTitle(){ console.log(title.value) } // The page is loading onMounted(getTitle) return {title} }, mounted() { console.log('Test mounted execution order') }, 4.4 watchUse watch to react to changes in setup 1. Import watch, import { watch } from 'vue' 2. Use watch directly. Watch accepts 3 parameters
import {wathc} from 'vue' // Define responsive variables const num = ref(0) // Update responsive variables function changeNum(){ num.value++ } // wathc monitors the responsive variable watch( num,(newValue, oldValue) => { console.log(`newValue is: ${newValue},--------oldValue is: ${oldValue}`) } ) 4.5 computedIt is also imported from vue, and the computed function returns a read-only reactive reference to the output of the getter-like callback passed as the first argument to computed. To access the value of the newly created computed variable, we need to use the .value property just like we did with a ref. When the value of num changes, the value of nums will be *3 import {ref,computed} from 'vue'; const num = ref(0) // Update num function changeNum(){ num.value++ } //Monitor num changes const nums = computed(() =>{ return num.value * 3 }) 5. Setup5.1 Accepting two parametersprops: The properties passed by the parent component. The props in the setup` function are responsive. They will be updated as the data is updated, and ES6 destructuring cannot be used because it will not make props responsive. context: It is a normal object that exposes 3 components
props:{ obj:{ type:Object } }, setup (props,{attrs,slots,emit}) { console.log(attrs) console.log(slots) console.log(emit) console.log(props.obj) } 5.2 Note when loading component When a component executes
6. Life Cycle When using the lifecycle in 7. Transferring values across components In Used in When using How to set it up for responsive updates?
Parent component: import { provide, defineComponent, ref, reactive } from "vue"; <template> <Son/> </template> <script> import { provide, defineComponent, ref, reactive } from "vue"; export default defineComponent({ setup() { const father = ref("my parent component"); const info = reactive({ id: 23, message: "Front-end self-study community", }); function changeProvide(){ info.message = 'Test' } provide('father',father) provide('info',info) return {changeProvide}; } }) </script> Subcomponents: <template> <div> <h1>{{info.message}}</h1> <h1>{{fatherData}}</h1> </div> </template> <script> import {provide, defineComponent,ref,reactive, inject} from 'vue' export default defineComponent({ setup () { const fatherData = inject('father') const info = inject('info') return {fatherData,info} } }) </script> 8. Tips on using TypeScript in Vue 8.1 Interface Constraints Constraint Attributes Using the characteristics of interface Paging query field attribute type verification: export default interface queryType{ page: Number, size: Number, name: String, age: Number } Used in components: import queryType from '../interface/Home' data() { return { query:{ page:0, size:10, name:'test', age: 2 } as queryType } }, 8.2 Components are defined using defineComponent This way TypeScript correctly infers the types in the Vue component options import { defineComponent } from 'vue' export default defineComponent({ setup(){ return{ } } }) 8.3 Reactive Type Declarations export default interface Product { name:String, price:Number, address:String } import Product from '@/interface/Product' import {reactive} from 'vue' const product = reactive({name:'xiaomi 11',price:5999,address:'北京'}) as Product return {fatherData,info,product} The above is the detailed content of the Vue3 + TypeScript development summary. For more information about Vue3 + TypeScript, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Simple setup of VMware ESXi6.7 (with pictures and text)
>>: Analysis of MySQL latency issues and data flushing strategy process
/****************** * Advanced character device d...
location / { index index.jsp; proxy_next_upstream...
I have always wondered why the MySQL database tim...
Database migration is a problem we often encounte...
Background: Linux server file upload and download...
Recent product testing found a problem that when ...
This article introduces the method of using CSS3 ...
<br />User experience is increasingly valued...
Recently, WeChat Mini Program has proposed adjust...
Table of contents 1. Create a new project 2. Add ...
Full-text indexing requires special query syntax....
"What's wrong?" Unless you are accus...
Table of contents 1. Class 1.1 constructor() 1.2 ...
Table of contents Mysql master-slave synchronizat...
1. Framework A browser document window can only d...