温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现高质量音乐Web app

发布时间:2021-10-13 09:35:53 来源:亿速云 阅读:101 作者:iii 栏目:编程语言

本篇内容介绍了“如何实现高质量音乐Web app”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

3.0对比一2.0的区别:
1.Performance //性能
2.Tree-shaking support //通过分析,可以使你代码里没有使用的代码全部删除,简单粗暴点就是代码优化工具,想了解自行百度
3.Composition API //特色语法
4.Fragment, Teleport, Suspense //“碎片”,Teleport即Protal传送门,“悬念”
5.Better TypeScript support //TypeScript支持度
6.Custom Renderer API //自定义rende

了解了大概的新内容,就让我们直接来搭建个项目看看吧!

先初始化一个vue2.0项目
1、安装vue-cli:

npm install -g @vue/cli
可以输入vue -V查看版本

vue -V
现在安装下来的话是@vue/cli 4.3.1版本
2、初始化 vue 项目:

vue create 你的项目名称
确认后:我们选择Manually select feature手动

Please pick a preset: 
  default (babel, eslint)  //自动
❯ Manually select features  //手动
随后勾选我们开发常用的:Router、Vuex、CSS Pre-processors 和 Linter / Formatter,


Vue CLI v4.3.1
? Please pick a preset: Manually select features
? Check the features needed for your project: 
 ◉ Babel
 ◯ TypeScript
 ◯ Progressive Web App (PWA) Support
 ◉ Router
 ◉ Vuex
 ◉ CSS Pre-processors
❯◉ Linter / Formatter
 ◯ Unit Testing
 ◯ E2E Testing
剩下就是vue-cli起步的步骤了:如果不知道可以参考文章:(https://www.jianshu.com/p/ae3fc27eb2c2)
为什么要说到vue-cli起步项目是因为vue3.0必须是从vue2.0升级而来,过程为了不再手动安装Router 和 Vuex,所以在过程中就要勾选这两个

升级到vue3.0
升级3.0需要通过插件的形式来升级,在项目里的命令行输入


vue add vue-next
然后就会自动安装(vue-cli-plugin-vue-next) 插件:
 

插件操作内容

然后理论哔哔和项目升级完了咱就来玩玩,体验下吧!
 

vue3.0的新特性体验
我们先来在/src/views下创建一个test.vue文件


<template>
  <div class="test">
      <div>vue3.0体验</div>
  </div>
</template>

<script>
 export default {
 }
</script>
<style lang="less" scoped>
.test {
  color: #999;
}
</style>
然后在router里面加入路由


import { createRouter, createWebHistory } from 'vue-router'
const routes = [
  {
    path: '/about',
    name: 'About',
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/test',
    name: 'Test',
    component: () => import(/* webpackChunkName: "test" */ '../views/Test.vue')
  }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
export default router
2.0版本的Router 与 3.0 版本变化不大,只是之前采用构造函数的方式,
这里改为使用 createRouter 来创建 Vue Router 实例,使用方法一样
配置完成我们在 App.vue 中给Test.vue 的页面一个入口:


<template>
  <div id="app">
    <div id="nav">
      <router-link to="/about">About</router-link> | 
      <router-link to="/test">Test</router-link> | 
    </div>
    <router-view/>
  </div>
</template>
然后运行即可


npm run serve
状态和事件绑定
在vue3.0里的定义状态不在是data()里面了,而是通过 setup 方法去定义状态,然后通过return把状态抛出去。
然后定义状态需要通过调用ref去定义,我们先来在test.vue里写个简单的累加器试试


<template>
  <div class="test">
     <h2>vue3.0</h2>
      <div>{{count}}</div>
      <div>{{hhhh}}</div>
      <button @click="add">add</button>
      <button @click="theNot">---</button>
  </div>
</template>

<script>
import { ref , } from 'vue'
 export default {
    setup () {
      const hhhh = ref(false)
      const count = ref(0)
      const add = () => {
        count.value++
      }
      const theNot= () => {
        hhhh.value = !hhhh.value
      } 
      return {
        count,
        hhhh,
        theNot,
        add
      }
    },
 }
</script>
很明显,3.0的方法和事件都不用再写在methods中。而是全部都在setup方法中里面声明,但我也试了一下以前的data定义状态个methods写事件还是可以兼容的。
然后补充一个定义状态的的方式就是reactive,这个和ref同样是定义状态的,只不过写法不一样而已,通过一个案例来了解下吧。


<template>
  <div class="ref">
      <h3>{{count}}</h3>
      <h3>{{pos.countA}}</h3>
      <h3>{{pos.countB}}</h3>
      <button @click="add">+++</button>
      <div>
        <button @click="subtraction">---</button>
      </div>
  </div>
</template>

<script>
import {reactive,ref} from 'vue'
 export default {
    //ref修改数据需要使用这样count.value=xxx的形式,而reactive只需要state.reactiveField=值这样来使用
    setup() {
        const pos  = reactive({ countA: 0,countB: 100 }) //refreactive
        const count = ref("单个")  //ref只能监听一些如数字、字符串、布尔之类的简单数据
        const add = () => { 
            pos.countA++;
        }
        const subtraction = () => {
            pos.countB-=2;
        }
        return { 
            count,
            pos,
            add,
            subtraction
        }
    },
 }
</script>
通过案例可以看出来,ref是定义单个状态,而reactive可以通过对象来定义多个状态,而ref改变数据需要(状态).value=xxx,reactive则可以通过state.reactiveField=值来操作。

计算属性和监听器
然后再说说vue3.0的computed 和 watch 方法:


<template>
  <div class="test">
      <div>{{count}}</div>
      <button @click="add">add</button>
      <div>count * 2 = {{doubleCount}}</div>
  </div>
</template>

<script>
import { ref , computed, watch } from 'vue'
 export default {
    setup () 
      const count = ref(0)
      const add = () => {
        count.value++
      }
      //第一个参数是监听的值,count.value 表示当 count.value 发生变化就会触发监听器的回调函数,即第二个参数可以执行监听时候的回调
      watch(() => count.value, val => {
        console.log(`count is ${val}`)
      })
      const doubleCount = computed(() => count.value * 2)
      //计算属性 computed 是一个方法,里面需要包含一个回调函数,当我们访问计算属性返回结果时,会自动获取回调函数的值:
      return {
        count,
        doubleCount,
        add
      }
    },
 }
</script>
获取路由
3.0 中通过 getCurrentInstance 方法获取当前组件的实例,然后通过 ctx 属性获得当前上下文,
ctx.$router 是 Vue Router 实例,里面包含了 currentRoute 可以获取到当前的路由信息


<script>
  import { getCurrentInstance } from 'vue'
  export default {
    setup () {
      const { ctx } = getCurrentInstance()
      console.log(ctx.$router.currentRoute.value)
    }
  }
</script>
Vuex 集成

<template>
  <div class="test">
      <div>{{count}}</div>
      <button @click="add">add</button>
      <div>state from vuex {{a}}</div>
      <button @click="take">take a</button>
  </div>
</template>

<script>
import { ref, computed, watch, getCurrentInstance } from 'vue'
 export default {
    setup () {
      const count = ref(0)
      const add = () => {
        count.value++
      }
      const { ctx } = getCurrentInstance()
      const a = computed(() => ctx.$store.state.test.a)
      //通过getCurrentInstance()方法获取实例,从而
      const take= () => {
        ctx.$store.commit('setTestA', count)
      }
      //Vuex 状态仍然使用 commit 方法,
      return {
        count,
        add,
        take,
        a
      }
    },
 }
</script>
vuex的操作其实和2.0是没太大差别的,只不过3.0的实例获取得通过getCurrentInstance()方法

总结时间
1、vue3.0是通过 createRouter 来创建 Vue Router 实例。
2、vue3.0定义状态和事件、计算属性都是写在setup里,以前数据状态都写在data里,事件写在methods中,但是测试以前的写法还是兼容。
3、3.0的状态定义要通过ref和reactive来定义,两者区别只在于写法不一样。
4、vuex的操作操作和定义属性和2.0无区别,但实例获取要通过getCurrentInstance()方法来获取

“如何实现高质量音乐Web app”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI