Vue Component Loading Icon
March 7, 2025 · AI Conversations
Loading…
and display when loading is true
<script>
nimport { defineProps } from 'vue';nndefineProps({n loading: Booleann});n
</script>
nnn n n Loading…n n n"}
<IconLoading :loading="isLoading" />
Make sure `isLoading` is a reactive variable in your parent component:
<script setup>
import { ref } from 'vue';
import IconLoading from './IconLoading.vue';
const isLoading = ref(true);
</script>
<template>
<IconLoading :loading="isLoading" />
</template>
<script>
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
</script>
SYT
News
Youtube
Pricing
add class active when on current page
### Updated Code:
<script setup lang="ts">
import { RouterLink, RouterView, useRoute } from 'vue-router';
const route = useRoute();
</script>
<template>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-2">
<div class="container">
<a class="navbar-brand" href="#">SYT</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<RouterLink :class="{'nav-link': true, 'active': route.path === '/news'}" to="/news">News</RouterLink>
</li>
<li class="nav-item">
<RouterLink :class="{'nav-link': true, 'active': route.path === '/youtube'}" to="/youtube">Youtube</RouterLink>
</li>
<li class="nav-item">
<RouterLink :class="{'nav-link': true, 'active': route.path === '/about'}" to="/about">Pricing</RouterLink>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<RouterView />
</div>
</template>
### Explanation:
– The `useRoute()` function is used to get the current route.
– The `:class` binding dynamically adds the `active` class when the `route.path` matches the `to` path of the `RouterLink`.
– This ensures that the `active` class is applied to the currently active link.
This will correctly highlight the active navigation item based on the current route. 🚀
### **Updated Code**
<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router';
</script>
<template>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-2">
<div class="container">
<a class="navbar-brand" href="#">SYT</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<RouterLink class="nav-link" to="/news" active-class="active">News</RouterLink>
</li>
<li class="nav-item">
<RouterLink class="nav-link" to="/youtube" active-class="active">YouTube</RouterLink>
</li>
<li class="nav-item">
<RouterLink class="nav-link" to="/about" active-class="active">Pricing</RouterLink>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<RouterView />
</div>
</template>
### **Why This Works:**
1. Vue Router automatically applies the `router-link-active` class to active links.
2. You can override this by specifying `active-class="active"` to ensure Bootstrap's `.active` class is applied.
3. Bootstrap’s `.nav-link.active` class ensures proper styling.
This should now correctly highlight the active menu item! 🚀 Let me know if you need further adjustments. 😊
—
### **1️⃣ Install Vuetify (Recommended)**
Vuetify is the most popular Material Design framework for Vue.
yarn add vuetify
# OR
npm install vuetify
### **2️⃣ Setup Vuetify in Vue 3**
Modify `main.ts` (if using TypeScript) or `main.js`:
import { createApp } from 'vue';
import App from './App.vue';
import vuetify from './plugins/vuetify';
const app = createApp(App);
app.use(vuetify);
app.mount('#app');
Create a new file `src/plugins/vuetify.ts`:
import 'vuetify/styles';
import { createVuetify } from 'vuetify';
export default createVuetify();
—
### **3️⃣ Use Vuetify Form in a Vue Component**
<script setup>
import { ref } from 'vue';
const form = ref({
name: '',
email: '',
});
const submitForm = () => {
console.log(form.value);
};
</script>
<template>
<v-container>
<v-form @submit.prevent="submitForm">
<v-text-field v-model="form.name" label="Name" outlined></v-text-field>
<v-text-field v-model="form.email" label="Email" outlined type="email"></v-text-field>
<v-btn type="submit" color="primary">Submit</v-btn>
</v-form>
</v-container>
</template>
—
### **Alternative: Use Material Web Components (MWC)**
If you want to use **Google’s Material Components** directly, install:
npm install @material/web
Then, use it inside your Vue component:
<script setup>
import '@material/web/all.js';
</script>
<template>
<md-filled-text-field label="Name"></md-filled-text-field>
<md-filled-text-field label="Email"></md-filled-text-field>
<md-filled-button @click="submitForm">Submit</md-filled-button>
</template>
—
### **Which One Should You Use?**
– **Vuetify**: Best for full Material UI experience in Vue.
– **MWC (Material Web Components)**: If you want the closest thing to Google's official Material UI.
– **Manually Styling Bootstrap/MUI Components**: If you need full control.
Let me know which approach fits your project best! 🚀