思路來源: https://blog.csdn.net/qq_42611074/article/details/128236458文章來源地址http://www.zghlxwxcb.cn/news/detail-517238.html
- 引用store做全局css變量替換;
- store.js
import { createStore } from 'vuex'
const store = createStore({
state: {
// default-theme dark-theme
themeName: 'default-theme',
themeStyle: {
'default-theme': `
--solar-color1: #ff5000;
--solar-color2: #000000;
--solar-color3: #b1b1b1;
--solar-color4: rgba(0, 0, 0, 0.1);
--solar-back-color1: linear-gradient(270deg, #ff8800 0%, #ff5000 100%);
--solar-back-color2: rgba(255, 255, 255, 0.9);
--solar-back-color3: linear-gradient(101deg, #ffffff 0%, #f2f1f6 100%);
--solar-back-color4: #ffffff;
`,
'dark-theme': `
--solar-color1: #000000;
--solar-color2: #000000;
--solar-color3: #000000;
--solar-color4: #000000;
--solar-back-color1: #000000;
--solar-back-color2: #000000;
--solar-back-color3: #000000;
--solar-back-color4: #ffffff;
`
}
},
getters: {
theme(state) {
return state.themeStyle[state.themeName]
}
},
mutations: {
setTheme(state, themeName = 'default-theme') {
state.themeName = themeName
}
}
})
export default store
- 添加全局的監(jiān)聽函數(shù)
- common/themeMixin.js
import { mapState, mapGetters } from 'vuex'
export default {
install(Vue) {
Vue.mixin({
computed: {
...mapState({
themeName: 'themeName'
}),
...mapGetters({
theme: 'theme'
})
}
})
}
}
- main.js
import store from '@/store/index'
import themeMixin from '@/common/themeMixin.js'
import { createSSRApp } from 'vue'
const app = createSSRApp(App)
app.use(store)
app.use(themeMixin)
- 給要切換的頁面加上css變量
- login.vue
<template>
<view class="container" :style="theme">
<view class="btn">
<button
class="btn-primary"
@click="handleSwitch"
>切換</button>
</view>
</view>
</template>
<script setup>
import { useStore } from 'vuex'
const store = useStore()
function handleSwitch() {
store.commit('setTheme', 'dark-theme')
}
</script>
<style lang="scss" scoped>
.container {
width: 100vw;
min-height: 100vh;
position: relative;
background: var(--solar-back-color2);
backdrop-filter: blur(23px);
}
.btn-primary {
cursor: pointer;
width: 670rpx;
height: 96rpx;
display: flex;
justify-content: center;
align-items: center;
font-size: 32rpx;
font-weight: 400;
color: #fff;
background: linear-gradient(
270deg,
#ff8800 0%,
#ff5000 100%
);
border-radius: 24rpx;
box-sizing: border-box;
}
</style>
升級版
- 在base.css寫好主題配色;
.default-theme {
--solar-color1: #ff5000;
--solar-color2: #000000;
--solar-color3: #b1b1b1;
--solar-color4: rgba(0, 0, 0, 0.1);
--solar-back-color1: linear-gradient(270deg, #ff8800 0%, #ff5000 100%);
--solar-back-color2: rgba(255, 255, 255, 0.9);
--solar-back-color3: linear-gradient(101deg, #ffffff 0%, #f2f1f6 100%);
--solar-back-color4: #ffffff;
}
.dark-theme {
--solar-color1: #000000;
--solar-color2: #000000;
--solar-color3: #000000;
--solar-color4: #000000;
--solar-back-color1: #000000;
--solar-back-color2: #000000;
--solar-back-color3: #000000;
--solar-back-color4: #ffffff;
}
- 引用store做全局css變量替換;
- store.js
import { createStore } from 'vuex'
const store = createStore({
state: {
// default-theme dark-theme
themeName: 'default-theme'
},
mutations: {
setTheme(state, themeName = 'default-theme') {
state.themeName = themeName
}
}
})
export default store
- 添加全局的監(jiān)聽函數(shù)
- common/themeMixin.js
import { mapState, mapGetters } from 'vuex'
export default {
install(Vue) {
Vue.mixin({
computed: {
...mapState({
themeName: 'themeName'
})
}
})
}
}
- main.js
import '@/common/base.scss'
import store from '@/store/index'
import themeMixin from '@/common/themeMixin.js'
import { createSSRApp } from 'vue'
const app = createSSRApp(App)
app.use(store)
app.use(themeMixin)
- 給要切換的頁面加上css變量
- login.vue
<template>
<view class="container" :class="themeName">
<view class="btn">
<button
class="btn-primary"
@click="handleSwitch"
>切換</button>
</view>
</view>
</template>
<script setup>
import { useStore } from 'vuex'
const store = useStore()
function handleSwitch() {
store.commit('setTheme', 'dark-theme')
}
</script>
<style lang="scss" scoped>
.container {
width: 100vw;
min-height: 100vh;
position: relative;
background: var(--solar-back-color2);
}
.btn-primary {
cursor: pointer;
width: 670rpx;
height: 96rpx;
display: flex;
justify-content: center;
align-items: center;
font-size: 32rpx;
font-weight: 400;
color: #fff;
background: linear-gradient(
270deg,
#ff8800 0%,
#ff5000 100%
);
border-radius: 24rpx;
box-sizing: border-box;
}
</style>
文章來源:http://www.zghlxwxcb.cn/news/detail-517238.html
到了這里,關于uni-app+vue3微信小程序切換主題皮膚的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!