Setting Up a Vue 3 Project with Vite and Implementing a Loading Screen
Project Initialization with Vite
Prerequisites
- Node.js version 16.14.2
- pnpm version 8.15.4
Creating the Project
To install pnpm globally, use:
npm install -g pnpm
Initialize a new Vite project by running:
pnpm create vite
During setup, specify:
- Project name: e.g.,
ruoyi-ui - Framework: Select
Vue - Variant: Choose
JavaScript
Running the Project
Navigate to the project directory and install dependencies:
pnpm install
Start the development server:
pnpm run dev
The applicasion will be accessible at http://127.0.0.1:5173/.
Project Structure Cleanup
- Remove
HelloWorld.vuefromsrc/components/ - Delete
vue.svgfromsrc/assets/ - Remove
style.cssfromsrc/ - Update
App.vueinsrc/with:
<script setup>
</script>
<template>
<div>hello vite</div>
</template>
<style scoped>
</style>
- Modify
main.jsinsrc/to:
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
Loading Screen Implementation
CSS Styling
Add the following CSS to style the loading screen:
html, body, #app {
height: 100%;
margin: 0;
padding: 0;
}
#loader-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 999999;
background-color: #7171C6;
}
#loader {
display: block;
position: relative;
left: 50%;
top: 50%;
width: 150px;
height: 150px;
margin: -75px 0 0 -75px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: #FFF;
animation: rotate 2s linear infinite;
z-index: 1001;
}
#loader::before {
content: "";
position: absolute;
top: 5px;
left: 5px;
right: 5px;
bottom: 5px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: #FFF;
animation: rotate 3s linear infinite;
}
#loader::after {
content: "";
position: absolute;
top: 15px;
left: 15px;
right: 15px;
bottom: 15px;
border-radius: 50%;
border: 3px solid transparent;
border-top-color: #FFF;
animation: rotate 1.5s linear infinite;
}
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.load_title {
font-family: 'Open Sans';
color: #FFF;
font-size: 19px;
width: 100%;
text-align: center;
position: absolute;
top: 60%;
opacity: 1;
line-height: 30px;
}
HTML Structure
Update the HTML in index.html or a Vue component to include:
<div id="app">
<div id="loader-wrapper">
<div id="loader"></div>
<div class="load_title">Loading system resources, please wait...</div>
</div>
</div>
<script type="module" src="/src/main.js"></script>
This setup displays a loading animation with a background color and text while the application initializes.