从0开始学vuejsvuejs前端Vue专辑

Vuejs2.0开发仿QQ音乐webpp案例分析

2018-01-19  本文已影响448人  Evtion

仿QQ音乐webapp是基于Vue2.0,vuex,vue-router,axios和html5的flexible box布局与及css3的transform,animation,transition组成。Vuejs是一套MVVM框架,主要注重于view层,实现数据双向绑定与及虚拟DOM,解放你的双手,让你不用直接和真实的DOM进行操作,只要数据改变,vue自动检查DOM结构不同,更新DOM元素结构。其中开发过程之中就遇到手动操作DOM,然后在样式表之中书写的样式无法发挥作用,所以使用Vuejs构建SPA单页应用的时候应该避免显式地去操作DOM结构。

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import Vuex from 'vuex'
import store from './store/store.js'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import VueLazyload from 'vue-lazyload'
import FastClick from 'fastclick'
FastClick.attach(document.body)
Vue.use(Vuex)
Vue.use(VueAwesomeSwiper)
Vue.use(VueLazyload,{
  preLoad:1.3,
  error:"../static/images/load.png",
  loading:"../static/images/load1.png",
  attempt:1
})
Vue.config.productionTip = false
Vue.prototype.$http=axios
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
import Vue from 'vue'
import Router from 'vue-router'
import Index from "@/components/index"
import List from "@/components/List"
import Search from "@/components/Search"
import Toplist from "@/components/toplist"
import IndexList from '@/components/indexlist'
Vue.use(Router)
export default new Router({
  mode: 'history',
  routes: [
    {
      path:'/',
      redirect:'/recomment'
    },
    {
      path: '/recomment',
      name: 'Index',
      component: Index
    },{
      path: '/list',
      name: 'List',
      component: List
    },{
      path: '/search',
      name: 'Search',
      component: Search
    },{
      path:"/toplist",
      component:Toplist
    },{
      path:'/indexlist',
      component:IndexList
    },{
      path:"*",
      redirect:"/recomment"
    }]
})

/**
 * Created by Administrator on 2017/12/18 0018.
 */
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store =new Vuex.Store({
  //定义状态
  state:{
    toggle:false,
    songPlayList:[{
      mid:'4962990',
      singerAvartar:"https://y.gtimg.cn/music/photo_new/T006R300x300M00000333So02drvak.jpg?max_age=2592000",
      songName:"七里香",
      singer:"周杰伦",
      songSrc:"http://dl.stream.qqmusic.qq.com/C400002ZKnKQ34rbZu.m4a?guid=957707267&vkey=A31FA931F2D4AE48FC9262A169955162673D2882402CF3D4E5FE40F32CD0035C97A18C3BEEA3AE0864D62ED9A8E0DDDC0853D532C1B055AB&uin=&fromtag=999"
    }],
    prevPlayList:[],
    searchHistory:[],
    Mkey:"guid=957707267&vkey=5C23405C26338354E28E61E4679EFE31BCE634C3BC1F4141131DA3C42E45F9C2F20CEA658F41B279C328AA7EB7219CB2B889FCC73918580D&uin=&fromtag=999"
  },
  mutations:{
    addSearchWord(state,word){
      state.searchHistory.push(word);
    },
    clearHistory(state){
      for(var i=0;i<state.searchHistory.length;i++){
        state.searchHistory.pop();
      }
    },
    changeTog(state,toggle){
      state.toggle=toggle;
    },
    nextSong(state,num){
      var prevSong=state.songPlayList.shift();
      state.prevPlayList.push(prevSong);
    },
    prevSong(state,num){

      var stackTop=state.prevPlayList.pop();
      state.songPlayList.unshift(stackTop);
    },
    changeList(state,index){

      var song=state.songPlayList.splice(index,1);
      state.songPlayList.unshift(song[0]);
    },
    addSong(state,data){
      for(var i=0;i<data.length;i++){
        var singleSong={
          singerAvartar:data[i].singerAvartar,
          songName:data[i].title,
          singer:data[i].singer,
          songSrc:data[i].songSrc,
          mid:data[i].mid
        };
        state.songPlayList.push(singleSong);
      }
    }
  }
})
export default store

<template>
  <div id="app">
    <Mheader></Mheader>//头部
    <router-view></router-view>//动态加载路由匹配的内容将生成这里
    <musicDetail></musicDetail>//为保证整个项目运行过程中,音乐播放器可以不中断,把他挂载在App.vue
    <audioPane></audioPane>
    <audio id="play"  :src="songPlayList[0].songSrc" @ended="nextSong" @error="nextSong" preload="auto"></audio>//播放器audio
  </div>
</template>

<script type="text/ecmascript-6">
import Mheader from '@/components/my-header';
import audioPane from'@/components/AudioPane';
import musicDetail from '@/components/MusicDetail'
export default {
  components: {
      Mheader,
      audioPane,
      musicDetail,
    },
    methods: {
      nextSong(){

        if(this.$store.state.songPlayList){
          this.$store.commit("nextSong",1);
          var audio=document.getElementById('play');
          audio.addEventListener("canplaythrough", function(){
              audio.play();
          },false);
          if(!this.toggle){
            var state=!this.toggle;
            this.$store.commit('changeTog',state);
          }

        }else{
          console.log("not found this song");
        }

      }
    },
  computed:{
    songPlayList(){
      return this.$store.state.songPlayList;
    }
  },
  mounted() {
    //do something after mounting vue instance
    // this.$http.get("http://localhost:3000/songList").then((response)=>{
    //   var data=response.data.cdlist[0].songlist;
    //   this.$store.commit('addSong',data);
    // }).catch((e)=>{
    //   console.log(e);
    // });
  }
}
</script>

<style scoped>
body{
  margin:0;
  overflow: hidden !important;
}
</style>

github地址:https://github.com/Harhao/QQMusicPlayerWebApp;该项目依赖nodejs做中间http代理,api的github地址是:https://github.com/Harhao/api。如果觉得有帮助,可以给个star哟。

上一篇下一篇

猜你喜欢

热点阅读