使用vue+element-ui 切换主题色

文章讲述了如何在Vue应用中实现头部组件的点击事件触发主题颜色切换,通过el-dialog展示可选主题,并通过props和计算属性实现在子组件和父组件之间的数据传递和样式更新。
摘要由CSDN通过智能技术生成

 效果:点击切换主题颜色

头部组件:

<!-- 子组件 -->
<template>
	<view class="box">
		<el-tooltip class="item" effect="dark" content="特色主题" placement="bottom">
			<i class="el-icon-s-promotion" @click="dialogVisible = true"></i>
		</el-tooltip>
		<!-- 对话框 -->
		<el-dialog title="选择" :visible.sync="dialogVisible" width="50%">
			<div>
				<el-radio-group v-model="radio" @change="switchoverClick">
					<el-radio label="1" border size="mini">默认主题</el-radio>
					<el-radio label="2" border size="mini">红色主题</el-radio>
					<el-radio label="3" border size="mini">黑色主题</el-radio>
					<el-radio label="4" border size="mini">渐变主题</el-radio>
				</el-radio-group>
			</div>
		</el-dialog>
	</view>
</template>

<script>
	export default {
		props: ["radioIndex"], //接收父组件传过来的值
		data() {
			return {
				dialogVisible: false, //弹窗默认关闭状态
				radio: this.radioIndex //赋值(获取父组件传递过来的值)
			};
		},
		methods: {
			//方法用于点击切换radio获取value
			switchoverClick(e) {
				//this.$emit子传父方法
				this.$emit('bgRadio', e)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.box {
		padding: 0 10px;
		width: 100%;
		height: 50px;
		background-color: #ffffff;
		box-shadow: 0 0 1px #000000;
		display: flex;
		align-items: center;

		.el-icon-s-promotion {
			cursor: pointer;
			font-size: 20px;
		}
	}
</style>

 父组件(页面):

<!-- 父组件(页面) -->
<template>
	<view>
		<!-- 组件 -->
		<demo @bgRadio="bgRadio" :radioIndex="radioIndex"></demo>
		<!-- 页面 -->
		<view :style="[bgComputed]" class="box2"></view>
	</view>
</template>

<script>
	// 引入子组件
	import demo from '@/components/demo/index.vue'
	export default {
		data() {
			return {
				radioIndex: '1', //默认第一项
			};
		},
		// 注册组件
		components: {
			demo
		},
		// 计算属性
		computed: {
			bgComputed() {
				let style = {}
				switch (this.radioIndex) {
					case '1':
						style.backgroundColor = '#55ff7f'
						break;
					case '2':
						style.background = 'red'
						break;
					case '3':
						style.backgroundColor = '#000000'
						break;
					case '4':
						style.background = 'linear-gradient(120deg, #bc00e3, #4efffb)';
						break;
					default:
						console.log('没有该主题色')
				}
				return style //这一步不能漏掉
			}
		},
		methods: {
			//点击事件 子组件方法传递参数,这里需要用this.radioIndex接收参数
			bgRadio(e) {
				this.radioIndex = e
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box2 {
		width: 100%;
		height: calc(100vh - 50px); //总高度-组件的高度=页面显示高度
	}
</style>


 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
很抱歉,我无法提供完整的代码。不过我可以给你提供一个简单的代码示例,用于播放音乐。你可以根据这个示例来开发你自己的音乐播放器组件。 ```html <template> <div> <audio ref="audio" :src="currentSong.url"></audio> <div>{{ currentSong.name }}</div> <div> <button v-if="!isPlaying" @click="play">Play</button> <button v-if="isPlaying" @click="pause">Pause</button> <button @click="next">Next</button> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { songs: [], currentSongIndex: 0, isPlaying: false }; }, computed: { currentSong() { return this.songs[this.currentSongIndex]; } }, mounted() { axios.get('/api/songs').then(response => { this.songs = response.data; }); }, methods: { play() { this.$refs.audio.play(); this.isPlaying = true; }, pause() { this.$refs.audio.pause(); this.isPlaying = false; }, next() { if (this.currentSongIndex === this.songs.length - 1) { this.currentSongIndex = 0; } else { this.currentSongIndex++; } this.$refs.audio.load(); this.play(); } } }; </script> ``` 在这个示例中,我们使用了 `audio` 标签来播放音乐。我们使用 Axios 发送 HTTP 请求来获取歌曲列表。我们在 `data` 对象中定义了一些状态,比如当前歌曲的索引和播放状态。我们使用 `computed` 属性来获取当前正在播放的歌曲。在 `mounted` 钩子中,我们发送 HTTP 请求来获取歌曲列表。在 `methods` 对象中定义了一些方法,比如播放、暂停和切换到下一首歌曲。在模板中,我们使用 `v-if` 指令来根据播放状态来显示不同的按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值