It is easiest to load content when the page is first opened. Adding content to <div id='app'>
through the root directory
file is excessive content
<body> <div id="app"> <h2>加载中......</h2> </div> <script type="module" src="/src/main.js"></script> </body>
When the vue instance is created, it is mounted into the div of id='app'
through the .mount()
method, and the loading
content inside will be replaced;
If the route is switched excessively, you need to understand one first, the built-in component of vue3
< Suspense>
;
## provides
2 slots????;
#default: A content to be loaded;
#fallback: A content to be displayed after loading;
<Suspense> <template #default> <router-view /> </template> <template #fallback> <h2>加载中......</h2> </template> </Suspense>
Similarly: (Switching of asynchronous components)
<template> <Suspense> <template #default> <AsyncComp v-if = 'vitblie' /> </template> <template #fallback> <h2>加载中......</h2> </template> </Suspense> <button @click='open'> 切换 </button> </template> <script setup> import { defineAsyncComponent , ref } from 'vue'; const asyncComp = defineAsyncComponent(()=>important('./asyncComp.vue)); const vitblie = ref(false); function open(){ vitblie.value = !vitblie.value; } </script>
vue3 built-in components
and
????
: Very simple, only one
is displays the component, which can be used to switch components, such as:
<template>
<Component :is="visible ? TestComp : '' " />
</template>
: Content inserted in
Show/Hide Add transition animation and splice it through the name attribute
class Such as:
<template>
<transition name='anime'>
<TestComp v-if='visblte' />
</transition>
</template>
name attribute here
##anime-enter-active: Transition state (set
Combined????hide=> show
transition time, etc. Parameters)anime-leave-active
: transition state (settingshow=> hide
transition time and other parameters)
anime-enter -from
=>anime-enter-to
Hide=> Show
start and end styles anime-leave-from
= >anime-leave-to
Show=> Hide
Starting and ending styles
<template> <router-view v-slot={ Component } > <transition name='anime'> <component :is="Component" /> <transition> </router-view> <template> <style scoped> .anime-enter-active, .anime-leave-active { transition: all 1s; } .anime-leave-from { transform: translateY(0); } .anime-leave-to { transform: translateY(-100%); } .anime-enter-from { transform: translateY(100%); } .anime-enter-to { transform: translate(0); } </style>
The above is the detailed content of How vue3 solves the problem of excessive loading in each scene. For more information, please follow other related articles on the PHP Chinese website!