current position:Home>[Vue] understanding and thinking of new features based on Vue 3
[Vue] understanding and thinking of new features based on Vue 3
2022-06-24 08:16:39【GavinUI】
vue The emergence of new features often requires attention to , What better technical solutions can be optimized and adjusted for my previous projects or projects to be developed by his features , It can also be said to be it What problems have been solved . and Composition API This time vue 3 Update focus of .
Composition API
Composition API In order to solve the problem of heavy interaction logic , Make the function more convenient to call and easy for developers to understand . The core idea is to collect the relevant code .
Let go of Vue2 perhaps Vue3 , In my previous project development , Due to the relatively large number of processing logic , I just separated some logic and put it in different functions , Vue2 The code is as follows :
methods: { format() { this.fun1(); this.fun2(); this.fun3(); this.fun4(); this.fun5(); }, fun1() { }, fun2() { }, fun3() { }, fun4() { }, fun5() { }, },
Of course, here is a logical simplification , Writing is definitely not like this , Just to explain format This method references and at least 5 A function , I was very happy when I pulled away from logic , After drawing a large pile of code, you can call feel thief slip .
But I am codeReview When you give it to the big guy , After reading it, he immediately pointed out the shortcomings of this place , Because I write very well , But the viewer is not like this. After a while, he scrolls to 20 OK, look fun1 , I will scroll to 130 OK, look fun2 , In a little while, scroll to 1000 OK, look fun3 , So in the end, not only are others tired , I was confused .
therefore , He proposed to put the relevant function points in the same function as far as possible , The code is as follows
methods: { format() { const fun1 = () => { }; const fun2 = () => { }; const fun3 = () => { }; const fun4 = () => { }; const fun5 = () => { }; }, },
His actual optimization is from the original :
Change it to :
In this way, we can understand this more clearly format Function modules depend on what they do , Not only can I understand the execution of functions very quickly , It's easy for others to read , This is the idea of simplifying code development .
go back to vue here , stay Vue2 Version of , Develop page logic using watch ,methods,data ,computed And other component options are very convenient to organize logic , But facing the pages with heavy interaction , These logic will also look bloated , Not easy to understand .
The official website documentation also gives a simple example that tends to be practical :
export default { components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList }, props: { user: { type: String, required: true } }, data () { return { repositories: [], filters: { ... }, searchQuery: '' } }, computed: { filteredRepositories () { ... }, repositoriesMatchingSearchQuery () { ... }, }, watch: { user: 'getUserRepositories' }, methods: { getUserRepositories () { }, updateFilters () { ... }, }, mounted () { this.getUserRepositories() } }
The options of the components here are all used , The more interaction later , The interaction code of various logic must be one by one “ block ” The units are built up . and Composition API The purpose of using is to aggregate logic .
Composition API Use
vue 3 A new component option has been added in setup, It is executed before creation , stay props When parsing , Just as Composition API Entrance .
export default { components: { ... }, props: { user: { type: String, required: true } }, setup(props) { console.log(props) return {} } }
He and props And other component options , It returns an object , This object can be used elsewhere , It can be said that the component provides a api The generic function .
belt ref The response variable of
stay vue3 in , Through a new ref Function to make any immediate response variable work anywhere .
import { ref } from 'vue' const counter = ref(0) console.log(counter) // { value: 0 } console.log(counter.value) // 0 counter.value++ console.log(counter.value) // 1
Encapsulate a value in an object , The advantage is that the object takes a reference , It will not affect the old data when modifying the new data , Official website DEMO Exhibition .
import { fetchUserRepositories } from '@/api/repositories' import { ref } from 'vue' export default { components: { RepositoriesFilters, RepositoriesSortBy, RepositoriesList }, props: { user: { type: String, required: true } }, setup (props) { const repositories = ref([]) const getUserRepositories = async () => { repositories.value = await fetchUserRepositories(props.user) } return { repositories, getUserRepositories } }, data () { return { filters: { ... }, searchQuery: '' } }, computed: { filteredRepositories () { ... }, repositoriesMatchingSearchQuery () { ... }, }, watch: { user: 'getUserRepositories' }, methods: { updateFilters () { ... }, }, mounted () { this.getUserRepositories() } }
It should be noted that , You need to change the original data Medium repositories Get rid of , because repositories This variable is now represented by setup This component option returns .
setup The use of hook functions in
stay setup in , Hook function and option expression of life cycle API It's the same , But the name of his hook function has been added in front on . give an example mount and beforeount .
setup(props) { const resList = ref([]); const fetchUserList = () => { resList.value = props.user; } onBeforeMount(getClass); onMounted(fetchUserList); return { resList, fetchUserList } },
So here are some ways to avoid hanging on the outer layer , As long as through the onMounted To execute .
setup Medium watch monitor
Methods and options of listener API The method of the master is the same ,watch You can also listen here props The content of , But there are differences , The object to listen to is after toRefs Method , Just give props in user Responsive reference to .
import {toRefs } from 'vue'; const { user } = toRefs(props);
The purpose of this is to better monitor props in user The change of , On the use ,watch The second parameter of is to execute the callback function when the listening object changes .
import { ajax } from 'majax'; import { ref, onMounted, watch, toRefs } from 'vue' setup (props) { const { user } = toRefs(props) const resList = ref([]) const fetchUserList = async () => { resList.value = await ajax(user.value) } onMounted(fetchUserList) watch(user, fetchUserList) return { resList, fetchUserList } }
setup In the middle computed attribute
Combine the most basic demo And instructions :
import { ref, computed } from 'vue' const counter = ref(0) const twiceTheCounter = computed(() => counter.value * 2) counter.value++ console.log(counter.value) console.log(twiceTheCounter.value)
computed Take a parameter , This parameter is equivalent to a fallback function , stay setup You still need Calculate the new variable , As in the above example conter.value. The real use case is :
import { majax } from 'majax' import { ref, onMounted, watch, toRefs, computed } from 'vue' setup (props) { const { user } = toRefs(props) const resList = ref([]) const fetchUserList = async () => { resList.value = await majax(user.value) } onMounted(fetchUserList) watch(user, fetchUserList) const searchQuery = ref('') const MatchingQuery = computed(() => { return resList.value.filter( item => item.name.includes(searchQuery.value) ) }) return { resList, fetchUserList, searchQuery, MatchingQuery } }
Until this whole setup There is a complete function , His options api Contains watch,computed,data also ref function .
Moving the function points to setup In the following , Although the logic will be more clear , But what follows is setup It's getting bigger and bigger . We need to further separate the method just described , For example, there are two functions: query and list rendering , Put their functional logic and hooks, etc Composition API The required attributes are separated and placed in two different JS Module export . I will DEMO The code has been simplified , The following two code blocks , They are the functions of query and list acquisition :
Get the code of the list module :
// fetchUserList.js import { ajax } from 'majax'; import { ref, onMounted, watch } from 'vue'; export default function fetchUserList(user) { const resList = ref([]) const fetchUserList = async () => { resList.value = await ajax(user.value) } onMounted(fetchUserList) watch(user, fetchUserList) return { resList, fetchUserList } }
Filter module code :
// searchUserList.js import { ref, computed } from 'vue' export default function searchName(resList) { const searchQuery = ref('') const MatchingQuery = computed(() => { return resList.value.filter(repository => { return repository.name.includes(searchQuery.value) }) }) return { searchQuery, MatchingQuery } }
With the code for these two modules , Then introduce it back to setup In the options :
import fetchUserList from 'fetchUserList.js' import searchUserList from 'searchUserList.js' import { toRefs } from 'vue' export default { ... setup (props) { const { user } = toRefs(props) const { resList, fetchUserList } = fetchUserList(user) const { searchQuery, MatchingQuery } = searchUserList(repositories) return { resList: MatchingQuery, fetchUserList, searchQuery, } }, ... }
To sum up ,Composition API Did such a thing , He does not use the same logic as before , Logic codes are scattered in watch Or is it computed in , Instead, the related code logic is aggregated into a functional module , Then use the functions on this module , Using a picture to represent is :
They are transformed into functional modules . The above is right Composition API Functional understanding of .
copyright notice
author[GavinUI],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/20210627143233434n.html
The sidebar is recommended
- JavaScript raw values and wrapper objects
- Application of Python simplehttpserver
- The problem of UMI & react packaging and deploying to non root directory and refreshing Error 404 is solved
- Is bootstrap or Vue the first choice for self-study
- Bootstrap react or bootstrap Vue
- One article can understand the detailed explanation of nginx operation. Are you still checking for omissions and filling vacancies!
- How to utilize the old front-end devices to the cloud through easycvr intelligent edge gateway devices to create AI course intelligent applications
- Introduction to web front end to actual combat: absolute path, relative path and root directory of HTML import file
- HTML basics form
- Shadow element CSS3 box shadow attribute
guess what you like
AngularJS Bootstrap
Angularjs contains
Angularjs animation
The nginx server sets the picture anti-theft chain to prohibit the picture from being linked outside
This is a cost-effective SUV. It costs less than 50 cents a kilometer, has a wheelbase of nearly 2.8 meters, and sells for only 79900 yuan_ Europe_ Plus_ Design
About nginx
How does endless restart the Go program without stopping?
http net::ERR_ HTTP2_ PROTOCOL_ Error error finding
On BOM and DOM (7): HTML DOM event object attributes and DOM event detailed list
Analysis and practice of HTML5 mouse drag sorting and resize implementation scheme
Random recommended
- On BOM and DOM (3): DOM node operation - element style modification and DOM content addition, deletion, modification and query
- Embrace cloud native, Tencent releases TCSs container security service!
- Is there any way for FC in react+ts to set the component of function keyword
- How to write the V-model of vue2/3 custom components?
- Error using vuescoll!
- When HTML accesses the parent directory, the file content cannot be displayed normally in the browser after reading
- How to embed tabelau in the front page of react
- Vue component communication data does not update rendering
- It is very convenient to use ECs to build a drawing bed and hexo one click deployment
- How does webstorm set the bootstrap auto prompt
- Nginx cross domain understanding, simulation and solution
- Front page exercise
- How to hide the value in El form item in Vue and set the value of input box to be read-only
- Springboot+vue to realize campus second-hand Mall (graduation design I)
- Node. JS installation
- Summary of 2022 blue team HW elementary interview questions
- Screenshot of angular page (scroll bar displays completely)
- JavaScript object field splicing
- How to repair garbled code in HTML
- Display CSV as a table in JavaScript (simple example)
- How to read files using JavaScript
- Example of JavaScript FileReader API processing files
- Springboot+vue project tourism information recommendation system [source code open source]
- Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
- Elementui used tabs to step on the pit
- Ant Design Vue - remove the input component border and the border blue shadow effect when getting focus
- Get httponly protected cookies across sub domains through XSS
- Coding specification - Vue project
- Code writing specification - Javascript
- Code specification - HTML
- Code writing specification - CSS
- CSS Notes 6
- Quickly set up PgSQL for serverless
- 404405 troubleshooting after Vue uses proxy packaging
- Vue3 TS learning
- What is the charm of python, which is warmly pursued by countless programmers?
- HTML Basics
- CSS Foundation
- CSS (PC side) three methods of traditional page layout
- Inline element, block element, inline block element