当前位置:   article > 正文

Vue3+TypeScript+Vuetify+Vite 实现动态主题切换_vuetify自定义主题

vuetify自定义主题

创建项目

  • 创建一个 Vite 项目
npm create vite@latest
  • 输入项目名称
? Project name: vue3-ts
  • 选择vue
  1. ? Select a framework: » - Use arrow-keys. Return to submit.
  2. vanilla
  3. > vue
  4. react
  5. preact
  6. lit
  7. svelte
  • 选择ts
  1. ? Select a variant: › - Use arrow-keys. Return to submit.
  2. JavaScript
  3. ❯ TypeScript
  4. Customize with create-vue ↗
  5. Nuxt ↗

下一步就是创建完了。

安装Vuetify

  • 我们进到项目里面安装vuetify
vue add vuetify
  • 选择Vite Preview (Vuetify 3 + Vite)
  1. ✔ Successfully installed plugin: vue-cli-plugin-vuetify
  2. ? Choose a preset:
  3. Vuetify 2 - Configure Vue CLI (advanced)
  4. Vuetify 2 - Vue CLI (recommended)
  5. Vuetify 2 - Prototype (rapid development)
  6. ❯ Vuetify 3 - Vite (preview)
  7. Vuetify 3 - Vue CLI (preview)
  • 安装完成
✔  Successfully invoked generator for plugin: vue-cli-plugin-vuetify
  • 启动服务器
npm run dev

访问: http://localhost:3000/

动态主题切换

App.vue文件里:

  1. <template>
  2. <v-app :theme="theme">
  3. <v-main>
  4. <v-btn @click="toggleTheme">点击切换主题</v-btn>
  5. <HelloWorld/>
  6. </v-main>
  7. </v-app>
  8. </template>
  9. <script setup lang="ts">
  10. import {ref} from 'vue'
  11. import HelloWorld from './components/HelloWorld.vue'
  12. const theme = ref('light')
  13. const toggleTheme = () => theme.value = theme.value === 'light' ? 'dark' : 'light'
  14. </script>

自定义主题

我们可以在vuetify.ts文件里面配置主题颜色:

  1. //vuetify.ts
  2. import { createVuetify } from 'vuetify'
  3. const light = {
  4. dark: false,
  5. colors: {
  6. background: '#808080',
  7. surface: '#6200EE',
  8. primary: '#6200EE',
  9. 'primary-darken-1': '#3700B3',
  10. secondary: '#03DAC6',
  11. 'secondary-darken-1': '#018786',
  12. error: '#B00020',
  13. info: '#2196F3',
  14. success: '#4CAF50',
  15. warning: '#FB8C00',
  16. }
  17. }
  18. const dark = {
  19. dark: true,
  20. colors: {
  21. }
  22. }
  23. export default createVuetify({
  24. theme: {
  25. defaultTheme: 'light',
  26. themes: {
  27. light,
  28. dark
  29. }
  30. }
  31. })

效果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/475511
推荐阅读