前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记。

项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star。


歌曲列表 收藏歌曲
一、用户个人中心开发

基础功能开发

  • 设置路由跳转
  1. 在components->user-center目录下:创建user-center.vue
  2. router->index.js中配置路由:
    {
    path: '/user',
    component: UserCenter
    }
  3. m-header.vue中通过<router-link>实现跳转
    <router-link tp="/user" class="mine" tag="div">
    <i class="icon-mine"></i>
    </router-link>
  • 应用Switches组件
  1. 引用注册并使用

    <switches @switch="switchItem" :switches="switches"
    :currentIndex="currentIndex"></switches>
  2. 定义data数据绑定
    data() {
    return {
    currentIndex: 0,
    switches: [
    {name: '我喜欢的'},
    {name: '最近听的'}
    ]
    }
    }
  3. 定义方法将获得的index赋值给currentIndex
    switchItem(index) {
    this.currentIndex = index
    }
  • 返回上一级路由
  1. 给按钮添加点击事件

    @click="back"
  2. 定义方法通过router.back返回
    back(){
    this.$router.back() //回退到上一级路由
    }

收藏列表开发

  • Vuex数据配置
  1. states.js中:添加数据

    favoriteList: []
  2. mutation-types.js中:定义事件类型常量
    export const SET_FAVORITE_LIST = 'SET_FAVORITE_LIST'
  3. mutations.js中:定义修改方法
    [types.SET_FAVORITE_LIST](state, list){
    state.favoriteList = list
    }
  4. getters.js中:设置数据映射
    export const favoriteList = state => state.favoriteList
  • catch.js中操作storage
  1. 定义本地缓存的Key和最大存储歌曲值

    const FAVORITE_KEY = '_favorite_'
    const FAVORITE_MAX_LENGTH = 200
  2. 保存歌曲到本地缓存
    export function saveFavorite(song) {
    let songs = storage.get(FAVORITE_KEY, [])
    insertArray(songs, song, (item) => {
    return song.id === item.id
    }, FAVORITE_MAX_LENGTH)
    storage.set(FAVORITE_KEY, songs)
    return songs
    }
  3. 从本地缓存中删除歌曲
    export function deleteFavorite(song) {
    let songs = storage.get(FAVORITE_KEY, [])
    deleteFromArray(songs, (item) => {
    return song.id === item.id
    })
    storage.set(FAVORITE_KEY, songs)
    return songs
    }
  4. 从本地缓存中获取全部歌曲
    export function loadFavorite() {
    return storage.get(FAVORITE_KEY, [])
    }
  • actions.js中:封装方法
  1. 同时保存到Vuex和本地缓存

    export const saveFavoriteList = function ({commit}, song) {
    commit(types.SET_FAVORITE_LIST, saveFavorite(song))
    }
  2. 同时从Vuex和本地缓存删除
    export const deleteFavoriteList = function ({commit}, song) {
    commit(types.SET_FAVORITE_LIST, deleteFavorite(song))
    }
  • states.js中修改初始数据为本地缓存数据

    favoriteList: loadFavorite()
  • player.vue中修改按钮,动态绑定class,监听点击事件
    <i class="icon" @click="toggleFavorite(currentSong)"
    :class="getFavoriteIcon(currentSong)">
  • mixin.js中在playerMixin中添加收藏歌曲需要的共享逻辑
  1. 通过mapGetters获得已收藏的歌曲数据:'favoriteList'
  2. 抽象出一个方法判断所选歌曲是否在已收藏的歌曲数据中
    isFavorite(song){
    const index = this.favoriteList.findIndex((item) => {
    return item.id === song.id
    })
    return index > -1 //如果index > -1 isFavorite 返回true
    }
  3. 定义方法依据所选歌曲是否为已收藏的歌曲,取反改变icon样式
    getFavoriteIcon(song){
    if(this.isFavorite(song)){
    return 'icon-favorite'
    }
    return 'icon-not-favorite'
    }
  4. 定义方法调用通过mapActions引用的action,同上取反进行保存或删除
    toggleFavorite(song){
    if(this.isFavorite(song)){
    this.deleteFavoriteList(song)
    }else{
    this.saveFavoriteList(song)
    }
    }
  • playlist.vue中添加数据映射和点击事件

    <span class="like" @click.stop="toggleFavorite(item)">
    <i :class="getFavoriteIcon(item)"></i>
    </span>
  • usercenter.vue中渲染收藏列表和播放历史列表
  1. 布局DOM: 同add-song.vue

    <scroll ref="favoriteList" class="list-scroll" v-if="currentIndex===0"
    :data="favoriteList">
    <div class="list-inner">
    <song-list :songs="favoriteList" @select="selectSong"></song-list>
    </div>
    </scroll>
    <scroll ref="playList" class="list-scroll" v-if="currentIndex===1"
    :data="playHistory">
    <div class="list-inner">
    <song-list :songs="playHistory" @select="selectSong"></song-list>
    </div>
    </scroll>
  2. 通过mapGetters获取收藏歌曲数据和播放历史数据
    computed: {
    ...mapGetters([
    'favoriteList',
    'playHistory'
    ])
    }
  3. 定义方法,调用通过mapActions获取到的insertSong方法,将song实例化之后插入
    selectSong(song) {
    this.insertSong(new Song(song))
    },
    ...mapActions([
    'insertSong'
    ])

剩余功能开发

  • 随机播放全部功能实现
  1. 给按钮添加点击事件,通过mapActions获取到randomPlay方法

    <div ref="playBtn" class="play-btn" @click="random">
  2. 定义方法,判断currentIndex获取对应list,通过实例化处理传入action
    random(){
    let list = this.currentIndex === 0 ? this.favoriteList : this.playHistory
    //这时list还不是实例,需要遍历list进行实例化
    list = list.map((song) => {
    return new Song(song)
    })
    this.randomPlay({
    list
    })
    }
  • 播放器底部自适应

    import {playlistMixin} from '@/common/js/mixin'
    
    mixins:[playlistMixin],
    
    handlePlaylist(playlist){
    const bottom = playlist.length > 0 ? '60px' : ''
    this.$refs.listWrapper.style.bottom = bottom
    //判断列表DOM存在后再执行refresh
    this.$refs.favoriteList && this.$refs.favoriteList.refresh()
    this.$refs.playList && this.$refs.playList.refresh()
    }
  • no-result组件的应用
  1. 布局DOM:

    <div class="no-result-wrapper" v-if="noResult">
    <no-result :title="noResultDesc"></no-result>
    </div>
  2. 显示条件和显示提示内容都需要动态绑定计算属性,判断currentIndex
    noResult() {
    if(this.currentIndex === 0) {
    return !this.favoriteList.length
    }else{
    return !this.playHistory.length
    }
    },
    noResultDesc() {
    if(this.currentIndex === 0) {
    return '暂无收藏歌曲'
    }else{
    return '你还没有听过歌曲'
    }
    }
  3. 优化:当列表中无数据,点击随机播放全部时,rendom()不执行任何操作
    if(list.length === 0) {
    return
    }
二、性能优化
  • 坑:快速的点击播放暂停歌曲,发现歌曲和歌词还在播放
  1. 原因:player.vue中watch currentSong会做一些清理操作,并在1s内开启play()播放;此时如果很快的切换歌曲切换播放状态,调用ready()和watch playing中的pause()会在1s内执行完;虽然看起来pause执行了,但1s过去之后,又会重新开启play()
  2. 解决:在每次setTimeout前清理掉旧的timer,保证只有一个timer;同时修改ready的触发事件为play,保证ready()和pause()一定发生在play()后
  3. 区别:

    ①事件canplay -- 当浏览器可以播放音频/视频时

    ②事件play -- 当音频/视频已开始或不再暂停时

  4. 注: 这里添加clearTimeout(this.timer)时总报timeout.close is not function,top-tip中使用过清理timer,没有问题,这里就不知道为什么了,待解决!
  • 坑:快速切换歌曲时,歌词的播放异常
  1. 原因:异步时机问题 -- setTImeout执行1s的操作中play()是一个同步的动作,而getLyric()是异步的操作;在异步获得回调时,有可能又切换到了下一首歌,这时之前的歌会在new一次,相当于new了两次,会有多个歌词同时存在
  2. 解决:在异步获得回调后先判断currentSong.lyric是否改变了,如果改变了不为layric,不执行任何操作
    if(this.currentSong.lyric !== lyric){
    return
    }
  • 坑:当前歌曲只有一首歌时,切换下一首,不会再触发ready()了
  1. 原因:next()中当列表长度为1时,会调用loop(),后面的ready标志位会一直为false,redy()也就不会再触发了
  2. 解决:在next()和prev()中,如果调用了loop()就直接return,不再修改标志位为false
三、项目打包及VConsole的使用

       编译打包 

npm run build

       VConsole的使用  

  • 安装
  1. 在页面引入一个JS文件:下载地址
  2. 使用npm安装
    npm install vconsole
  • 使用webpack,然后在js代码中应用VConsole

    import VConsole from 'vconsole/dist/vconsole.min.js' //引入vconsole
    let vConsole = new VConsole() // 初始化

    或者找到这个模块下面的 dist/vconsole.min.js ,然后复制到自己的项目中

    <head>
    <script src="dist/vconsole.min.js"></script>
    </head>
    <!--建议在 `<head>` 中引入哦~ -->
    <script>
    // 初始化
    var vConsole = new VConsole();
    console.log('VConsole is cool');
    </script>

注:项目来自慕课网

【音乐App】—— Vue-music 项目学习笔记:用户个人中心开发的更多相关文章

  1. 【音乐App】—— Vue-music 项目学习笔记:推荐页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 上一篇总结了项目概述.项目准备.页面骨架搭建.这一篇重点梳理推荐页面开发.项目github地址:https://github.com/66We ...

  2. 【音乐App】—— Vue-music 项目学习笔记:搜索页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 搜索歌手歌曲 搜索历史保存 ...

  3. 【音乐App】—— Vue-music 项目学习笔记:歌手页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 一.歌手页面布局与设计 需 ...

  4. 【音乐App】—— Vue-music 项目学习笔记:项目准备

    前言: 学习慕课网Vue高级实战课程后,在实践中总结一些这个项目带给自己的收获,希望可以再次巩固关于Vue开发的知识.这一篇主要梳理:项目概况.项目准备.页面骨架搭建.项目github地址:https ...

  5. 【音乐App】—— Vue-music 项目学习笔记:歌单及排行榜开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 歌单及详情页 排行榜及详情 ...

  6. 【音乐App】—— Vue-music 项目学习笔记:歌手详情页开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 歌曲列表 歌曲播放 一.子 ...

  7. 【音乐App】—— Vue-music 项目学习笔记:播放器内置组件开发(二)

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 播放模式切换 歌词滚动显示 ...

  8. 【音乐App】—— Vue-music 项目学习笔记:歌曲列表组件开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 当前歌曲播放列表 添加歌曲 ...

  9. 最新 Vue 源码学习笔记

    最新 Vue 源码学习笔记 v2.x.x & v3.x.x 框架架构 核心算法 设计模式 编码风格 项目结构 为什么出现 解决了什么问题 有哪些应用场景 v2.x.x & v3.x.x ...

随机推荐

  1. jquery左右滑动菜单

    <div class="mini-container" style="position:relative;height:100%;"> <di ...

  2. CSU-1980 不堪重负的树

    CSU-1980 不堪重负的树 Description 小X非常喜欢树,然后他生成了一个大森林给自己玩. 玩着玩着,小X陷入了沉思. 一棵树由N个节点组成,编号为i的节点有一个价值Wi. 假设从树根出 ...

  3. c++11特性使用

    #include <list> #include <iostream> int main(){ list<int> lst; for(list<int> ...

  4. rpm包管理 命令

    rpm -ivh package.rpmrpm -ivh --force  package_name.rpm # ...conflict with...rpm -ivh --nodeps packag ...

  5. IE IE8 iframe的onload方法分析 IE浏览器onload事件失效

    判断iframe是否加载完成的完美方法 IE 支持 iframe 的 onload 事件,不过是隐形的,需要通过 attachEvent 来注册. 第二种方法比第一种方法更完美(采用readystat ...

  6. ruby 发送邮件

    产品构建.打包.部署完需要发送邮件通知相关人员进行验证.请看过程 #encoding:utf-8 require 'mail' #~ branch = ARGV.to_s.sub('[','').su ...

  7. 如何获取JavaCard剩余空间

    0x01应用场景 获取JavaCard卡内剩余空间,一方面是在评估一张卡的时候需要用到,另一方面是在应用个人化或者运行时需要用到. 例如:应用提供商为了保证自己的应用在卡内运行期间能够不受空间影响,一 ...

  8. iOS----------设计模式的六大设计原则------>开放-关闭原则(OCP,Open-Close Principle)

    定义 一个软件实体(如类.模块.函数)应当对扩展开放,对修改关闭. 定义解读 在项目开发的时候,都不能指望需求是确定不变化的,大部分情况下,需求是变化的.那么如何应对需求变化的情况?这就是开放-关闭原 ...

  9. puppet实战之master-agent

    author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- master作为puppet模块的管理者,通过配置各agent节点的配置文件,使age ...

  10. POJ2749 Building roads 【2-sat】

    题目 Farmer John's farm has N barns, and there are some cows that live in each barn. The cows like to ...