vueJs开发音乐播放器第二篇(点击歌单跳出详情页)
(1.使用router定义跳转链接,2. 使用axios得到音乐第三方数据,并渲染到页面上,3.组件之间传值(props))
1.接下来使用了vue-router路由动态传值,父子嵌套式路由
this.$router.push({
path: `/recommend/${item.id}`
});

2.router入口文件定义路由路径:

3. 此时需要渲染路由在父组件中:

B.使用vuex进行状态管理:
npm install vuex --save
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

定义一个state,getters,mutations,方便组件中使用:

export 导出vuex定义模块
第二栏效果图:

主要代码:
<template>
<div >
<div class="wrapper" ref='wrapper'>
<div class="content">
<listview
title="推荐歌单"
:showPlayCount="true"
:showSinger="false"
:limit="6"
:resources="recommendList"
@select="selectedList"></listview>
<listview
title="最In音乐"
:showPlayCount="false"
:showSinger="true"
:limit="6"
:resources="newList"
@select="selectedList"></listview>
<listview
title="主播电台"
:showPayCount="false"
:showSinger="true"
:limit="6"
:resources="djrecommend"
@select="selectedList"></listview>
</div>
<router-view></router-view>
</div> </div>
</template>
<script>
import Listview from '@/components/listview/listview'
import {mapGetters, mapMutations} from 'vuex'
import store from "@/store/store.js";
export default{
name:"recommend",
store,
data(){
return {
list:null
} },
components:{
Listview, },
methods: {
//定义selectedList方法,router.push路由跳转
selectedList(item) {
this.$router.push({
path: `/recommend/${item.id}`
});
console.log(item);
this.setSongList(item);
},
...mapMutations([
'setSongList'
]) },
created(){
/*this.axios
.get('http://localhost:3000/top/playlist/highquality')
.then((response)=>{
this.list = response.data.playlists // console.log(this.list)
})
.catch((response)=>{
console.log(response)
})*/
this.recommendList = 'http://localhost:3000/top/playlist/highquality';
this.newList='http://localhost:3000/top/playlist?order=hot';
this.djrecommend='http://localhost:3000/personalized/djprogram';
} }
</script>
<style lang="scss" rel="style/scss"> </style>
歌单列表页
<template>
<transition name="fade">
<music-list :songs="songs" :id="id">
</music-list>
</transition>
</template> <script>
import {mapGetters} from 'vuex';
import musicList from '@/components/music-list/music-list' export default {
name: "list-detail",
data() {
return {
songs: {},
id: ''
}
},
created() {
this._getSongList();
},
methods: {
_getSongList() {
if (!this.songList || !this.songList.id) {
this.$router.push('/recommend');
return;
}
//使用vuex定义的数据得到父组件的相关值
this.songs = this.songList;
this.id = this.songList.id;
},
},
computed: {
...mapGetters([
'songList'
])
},
components: {
musicList
}, }
</script> <style lang="scss">
.fade-enter-active, .fade-leave-active {
transition: all 0.3s;
} .fade-enter, .fade-leave-to {
opacity: 0;
} </style>
子组件传值给歌曲列表页
<template>
<div class="music-list">
<div class="header">
<div class="header-img">
<img :src="songs.coverImgUrl">
<div class="play-count"><i class="icon iconfont icon-headset"></i>{{songs.playCount|unitConvert}}</div>
<div class="detail-btn"><i class="icon iconfont icon-detail"></i></div>
</div>
<div class="header-content">
<div class="name">{{songs.name}}</div>
<div class="tags" v-show="songs.tags">
<span>标签: </span><span class="tag" v-for="(tag,idx1) in songs.tags" :key="idx1">{{tag}}</span>
</div>
<div class="subname" v-if="songs.subscribers && songs.subscribers.length > 0">
{{songs.subscribers[0].nickname}}<span class="create-time">创建于{{songs.createTime}}</span>
</div>
</div>
<div class="back" @click="back">
<i class="icon iconfont icon-close"></i>
</div>
<div class="background">
<img :src="songs.coverImgUrl">
</div>
</div>
<div class="body">
<div class="header-bar">
<i class="icon iconfont icon-play"></i><span class="playAll">播放全部<i
class="count">(共{{detail.length}}首)</i></span>
<span class="collect"><i class="icon iconfont icon-add"></i>收藏({{subscribedCount|unitConvert}})</span>
</div>
<div class="songList-wrapper" ref="listWrapper">
<ul class="songlist">
<li class="song-item" v-for="(item, idx) in detail" :key="idx" @click="selectItem(item, idx, $event)">
<div class="line-number">
<span>{{idx+1}}</span>
</div>
<div class="item-content">
<div class="songname">{{item.name}}</div>
<div class="songer">{{item.ar[0].name}} - {{item.al.name}}</div>
</div>
<div class="tool">
<i class="icon iconfont icon-tool"></i>
</div>
</li>
</ul> </div> </div> </div>
</template> <script> import {unitConvert} from '@/common/js/unitConvert'
import {mapGetters, mapActions} from 'vuex' export default {
name: "music-list",
//得到list-detail传过来的id,并获取第三方数据,渲染到页面中
props: {
songs: {
type: Object
},
id: {
type: [String, Number]
}
},
data() {
return {
detail: [],
subscribedCount: ''
}
},
created() {
if (this.id) {
let url = `http://localhost:3000/playlist/detail?id=${this.id}`;
this.axios.get(url).then((res) => {
this.detail = res.data.playlist.tracks;
this.subscribedCount = res.data.playlist.subscribedCount; })
} },
methods: {
back(){ },
...mapActions([
'selectPlay'
])
},
filters: {
unitConvert(num) {
return unitConvert(num);
}
}
}
</script> <style lang="scss">
@function px2rem($px) {
@return $px / 30 + rem;
}
.music-list {
position: fixed;
z-index: 200;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: #fff;
.header {
position: relative;
display: flex;
width: 100%;
height: px2rem(400);
align-items: center;
overflow: hidden;
background-color: rgba(0, 0, 0, 0.2);
.header-img {
display: flex;
position: relative;
justify-content: center;
flex: 0 0 px2rem(300);
width: px2rem(300);
height: px2rem(250);
img {
width: px2rem(250);
height: px2rem(250);
}
.play-count {
position: absolute;
top: 0;
right: px2rem(40);
color: #fff;
font-size: px2rem(24);
font-weight: bold;
.icon-headset {
color: #fff;
font-size: px2rem(24);
font-weight: bold;
margin-right: px2rem(8);
}
}
.detail-btn {
position: absolute;
bottom: 0;
right: px2rem(42);
color: #fff;
.icon-detail {
font-size: px2rem(36);
font-weight: bold; }
}
}
.header-content {
flex: 1;
height: px2rem(250);
padding-right: px2rem(25);
color: #fff;
.name {
margin: px2rem(20) 0;
height: px2rem(90);
font-size: px2rem(32);
font-weight: bold;
}
.tags {
margin-bottom: px2rem(24);
font-size: px2rem(26);
line-height: px2rem(26);
height: px2rem(26);
.tag {
padding: 0 px2rem(12);
border: 2px solid #fff;
border-radius: px2rem(8);
margin-left: px2rem(12);
}
}
.subname {
font-size: px2rem(26);
line-height: px2rem(26);
height: px2rem(26);
.create-time {
margin-left: px2rem(10);
} } }
.icon-close {
position: absolute;
top: 0;
right: px2rem(16);
padding: px2rem(20);
color: #fff;
font-size: px2rem(32);
font-weight: bold;
}
.background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: px2rem(400);
z-index: -1;
filter: blur(px2rem(100));
overflow: hidden; }
}
.body {
.header-bar {
display: flex;
line-height: px2rem(80);
height: px2rem(80);
border-bottom: 1px solid #eee;
justify-content: center;
text-align: center;
.icon-play {
width: px2rem(80);
font-size: px2rem(48);
}
.playAll {
flex: 1;
font-size: px2rem(26);
text-align: left;
.count {
font-size: px2rem(24);
font-style: normal;
color: #7e8c8d;
}
}
.collect {
padding: 0 px2rem(24);
background: #F93021;
color: #fff;
font-size: px2rem(26);
font-weight: bold;
text-align: center;
.icon-add {
font-size: px2rem(26);
color: #fff;
font-weight: bold;
}
}
}
.songList-wrapper {
position: absolute;
top: px2rem(480);
bottom: 0;
left: 0;
width: 100%;
overflow: hidden;
.songlist {
background: #fff;
.song-item {
display: flex;
&.list-complete-enter-active, &.list-complete-leave-active {
transition: all 0.2s linear;
}
&.list-complete-enter, &.list-complete-leave-to {
opacity: 0;
transform: translateY(px2rem(30));
}
.line-number {
flex: 0 0 px2rem(80);
width: px2rem(80);
height: px2rem(80);
line-height: px2rem(80);
text-align: center;
font-size: px2rem(30);
color: #7e8c8d; }
.item-content {
flex: 1;
border-bottom: 1px solid #eee;
width: 80%;
.songname {
font-size: px2rem(28);
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis; }
.songer {
font-size: px2rem(22);
color: #7e8c8d;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis; } }
.tool {
flex: 0 0 px2rem(80);
width: px2rem(80);
line-height: px2rem(80);
border-bottom: 1px solid #eee;
text-align: center;
.icon-tool {
font-size: px2rem(30); }
} }
}
} }
} </style>
歌曲列表页
router的相关定义:
import Vue from 'vue'
import Router from 'vue-router'
import Helloworld from '@/components/HelloWorld'
import Recommend from '@/page/recommend'
import HotRecommend from '@/page/hotrecommend'
import Search from '@/page/search'
//引入子组件
import recommendList from '@/page/list-detail' Vue.use(Router) export default new Router({
routes: [
{
path: '/',
component: Helloworld
},
{
path: '/recommend',
component: Recommend,
//子组件路由
children: [
{
path: ':id',
component: recommendList
}
]
},
{
path: '/Hotrecommend',
component: HotRecommend
},
{
path: '/search',
component: Search
}
]
})
嵌套式路由实用
vuex-- store.js设定:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)
const state = {
// count:1,
songList:null
}
const getters = {
songList:function(state) {
return state.songList;
}
}
const mutations = {
/*increment(state){
state.count++
},
decrement(state){
state.count--
},*/
setSongList(state,item) {
state.songList=item;
}
}
const actions={
/*increment: ({commit})=>{
commit('increment')
},
decrement: ({commit})=>{
commit('decrement')
},*/
/* selectPlay: ({commit, state}, {list, index})=>{
commit('setPlayList',list)
},
*/
} export default new Vuex.Store({
state,
mutations,
getters,
actions
})
state,mutations,getters定义
vueJs开发音乐播放器第二篇(点击歌单跳出详情页)的更多相关文章
- Linux终端音乐播放器cmus攻略: 操作歌单
目录 1. 安装 2. 操作说明 2.1. *PlayList歌单 2.2. 其他 3. 视图切换 4. 使响应Media/play按键 4.1. 编译安装 cmus是一款开源的终端音乐播放器.它小巧 ...
- android开发音乐播放器--Genres和Art album的获取
最近在做一个项目,其中涉及到音乐播放器.当用到Genres和Art album时花费了一些时间才搞定,今天把方法草草列出,以供自己以后忘记时查看,也希望可以帮助碰到同样问题的道友!! 一.Genres ...
- python音乐播放器第二版
此代码是上一期的改版 需要用到的Python库有 .pygame 2.time 3.xmusic(我自己写的用来做音乐索引) .colorama(美观) 推荐使用pip安装 方法: pip ins ...
- 用Vue来实现音乐播放器(九):歌单数据接口分析
z这里如果我们和之前获取轮播图的数据一样来获取表单的数据 发现根本获取不到 原因是qq音乐在请求头里面加了authority和refer等 但是如果我们通过jsonp实现跨域来请求数据的话 是根本 ...
- iOS开发拓展篇—音频处理(音乐播放器3)
iOS开发拓展篇—音频处理(音乐播放器3) 说明:这篇文章主要介绍音频工具类和播放工具类的封装. 一.控制器间数据传递 1.两个控制器之间数据的传递 第一种方法:self.parentViewCont ...
- iOS开发拓展篇—音频处理(音乐播放器1)
iOS开发拓展篇—音频处理(音乐播放器1) 说明:该系列文章通过实现一个简单的音乐播放器来介绍音频处理的相关知识点,需要重点注意很多细节的处理. 一.调整项目的结构,导入必要的素材 调整后的项目结构如 ...
- iOS开发拓展篇—音频处理(音乐播放器2)
iOS开发拓展篇—音频处理(音乐播放器2) 说明:该文主要介绍音乐播放界面的搭建. 一.跳转 1.跳转到音乐播放界面的方法选择 (1)使用模态跳转(又分为手动的和自动的) (2)使用xib并设置跳转 ...
- iOS开发拓展篇—音频处理(音乐播放器4)
iOS开发拓展篇—音频处理(音乐播放器4) 说明:该文主要介绍音乐播放器实现过程中的一些细节控制. 实现的效果: 一.完整的代码 YYPlayingViewController.m文件 // // Y ...
- iOS开发拓展篇—音频处理(音乐播放器5)
iOS开发拓展篇—音频处理(音乐播放器5) 实现效果: 一.半透明滑块的设置 /** *拖动滑块 */ - (IBAction)panSlider:(UIPanGestureRecognizer *) ...
- iOS开发拓展篇—音频处理(音乐播放器6)
iOS开发拓展篇—音频处理(音乐播放器6) 一.图片处理 说明: Aspect表示按照原来的宽高比进行缩放. Aspectfit表示按照原来的宽高比缩放,要求看到全部图片,后果是不能完全覆盖窗口,会留 ...
随机推荐
- Python爬取imdb电影数据并存储到mysql数据库
数据获取方式:微信搜索关注[靠谱杨阅读人生]回复[电影].整理不易,资源付费,谢谢支持. Python爬虫代码: 1 import re 2 import time 3 import tracebac ...
- C语言线程安全问题
线程安全问题 #include <stdio.h> #include <tinycthread.h> #include <io_utils.h> int count ...
- 线上问题分析之java dump文件生成
一.查看java进程 jps or ps aux | grep java 二.生成dump文件 jmap -dump:live,format=b,file=xxxx.bin 进程ID 三.查看dump ...
- Postman模拟浏览器网页请求并获取网页数据
本文介绍在浏览器中,获取网页中的某一个请求信息,并将其导入到Postman软件,并进行API请求测试的方法. Postman是一款流行的API开发和测试工具,它提供了一个用户友好的界面,用于创 ...
- #莫比乌斯反演,欧拉函数#洛谷 5518 [MtOI2019]幽灵乐团
题目传送门 分析 前置知识:\(\sum_{d|n}\mu(d)=[n==1]\),\(\sum_{d|n}\mu(d)\frac{n}{d}=\varphi(n)\) 把最小公倍数拆开可以得到 \[ ...
- #CDQ分治,单调栈,双指针#BZOJ 4237 稻草人 AT1225 かかし
洛谷传送门 BZOJ 4237 稻草人 题意 在一个平面直角坐标系上给出\(n\)个点, 问有多少个点对\((i,j)\)满足\(x_i<x_j,y_i<y_j\), 而且对于\(n\)个 ...
- 共筑使能千行百业的数字底座 | HDC 2022松湖对话顺利召开
11月5日,华为开发者大会2022松湖对话在东莞松山湖凯悦酒店召开,开放原子开源基金会秘书长冯冠霖.华为终端BG软件部总裁龚体.深圳国家金融科技测评中心董事长钟剑.鸿湖万联(江苏)科技发展有限公司董 ...
- C# 运算符详解:包含算术、赋值、比较、逻辑运算符及 Math 类应用
运算符用于对变量和值执行操作.在C#中,有多种运算符可用,包括算术运算符.关系运算符.逻辑运算符等. 算术运算符 算术运算符用于执行常见的数学运算: int x = 100 + 50; // 加法,结 ...
- 快速加入Health Kit,一文了解审核流程
HUAWEI Health Kit是为华为生态应用打造的基于华为帐号和用户授权的运动健康数据开放平台. 在获取用户授权后,开发者可以使用Health Kit提供的开放能力获取运动健康数据,基于多种类型 ...
- Push failed idea将项目发布到gitHub失败
此时需要点击VCS --> inport into version..-->create git ...重新生成仓库