vue-component通信

//main.js
import Vue from 'vue'
import App from './App'
import './filter/timeFormat.js'
Vue.config.productionTip = false
Vue.prototype.$bus = new Vue()
/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>'
})
//app.vue
<template>
    <div id="app">
		<header>
			<div class="recommend" @click="comName = 'Recommend'" :class="{'active': comName == 'Recommend'}">推荐</div>
			<div class="sort" @click="comName = 'Sort'" :class="{'active': comName == 'Sort'}">排行</div>
		</header>
		<main>
			<div :is="comName"></div>
		</main>
        <Play v-show="flag" />
	</div>
</template>

<script>
import Recommend from './components/Recommend'
import Sort from './components/Sort'
import Play from './components/play'
export default {
    name: "App",
	components: {
		Recommend,
		Sort,
        Play
	},
    mounted() {
        this.$bus.$on("show", (bool) => {
            this.flag = bool
        })
    },
	data() {
		return {
			comName: "Recommend",
            flag: false
		}
	},
};
</script>

<style>
* {
	margin: 0;
	padding: 0;
}
html, body, #app {
	width: 100%;
	height: 100%;
}
#app {
	display: flex;
	flex-direction: column
}
header {
	height: 44px;
	background-color: #eee;
	display: flex;
}
header div {
	flex: 1;
	text-align: center;
	line-height: 44px;
}
header div.active {
	color: red;
}
main {
	flex: 1;
}
</style>
//filter
import Vue from 'vue'
Vue.filter("format", (time) => {
    // 173 => 02:53
    var min = parseInt(time / 60)
    var sec = time % 60
    min = min < 10 ? "0" + min : min;
    sec = sec < 10 ? "0" + sec : sec;
    return min + ":" + sec
})

//sort.vue
<template>
    <div>
        <button @click="show">跳到音乐播放</button>
    </div>
</template>

<script>
    export default {
        methods: {
            show() {
                this.$bus.$emit("show", true)
            }
        }
    }
</script>

<style scoped>

</style>
//song.vue
<template>
    <div>
        <div :style="{background: bgc}" class="box"></div>
        <p>{{ title }}</p>
    </div>
</template>

<script>
    export default {
        props: ["bgc", "title"]
    }
</script>

<style scoped>
    .box {
        width: 100px;
        height: 100px;
    }
</style>
//recommend.vue
<template>
    <div>
        <div class="guanfang">
            <h3>官方歌单</h3>
            <div class="list">
                <Song v-for="(item, index) in officeList" :key="index" :bgc="item.bgc" :title="item.title" />
            </div>
        </div>
        <div class="daren">
            <h3>大人歌单</h3>
            <div class="list">
                <Song v-for="(item, index) in fashionList" :key="index" :bgc="item.bgc" :title="item.title" />
            </div>
        </div>
    </div>
</template>

<script>
    import Song from './song'
    export default {
        components: {
            Song
        },
        data() {
            return {
                officeList: [{
                        bgc: "#ccc",
                        title: "流行节奏控"
                    },
                    {
                        bgc: "#eee",
                        title: "流行请有氧"
                    },
                    {
                        bgc: "#333",
                        title: "陈奕迅"
                    }
                ],
                fashionList: [{
                        bgc: "#ccc",
                        title: "重温经典"
                    },
                    {
                        bgc: "#eee",
                        title: "DJ歌曲"
                    },
                    {
                        bgc: "#333",
                        title: "值得收藏"
                    }
                ]
            }
        }
    }
</script>

<style scoped>
    .list {
        display: flex;
        justify-content: space-between;
        padding: 0 10px;
    }
</style>
//

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值