当前位置:首页 > 前端 > Vue.js

vue3学习笔记

vue3 相较于vue2的改进

  1. 性能提升:Vue3引入了新的虚拟DOM算法,使得渲染效率更高,特别是在大型应用中表现尤为突出。
  2. Composition API:使得逻辑复用和组织更加灵活,取代了Vue2中的Options API。
  3. TypeScript支持:Vue3对ts的原生支持更加完善,提供更好的类型推断和开发体验。
  4. 更小的体积:Vue3的核心库相比Vue2更小,同时提供的运行性能。
  5. Fragments和Teleport:允许多个根节点和跨DOM节点传送组件内容。

组合式API-响应式

组合式API的关键字

ref函数:基本类型的响应式数据:number\string\boolean
reactive: 引用类型的响应式数据: Array\Object

  1. const name = ref('飞哥')
  2. const result = reactive({
  3. name: '飞哥'
  4. })

标准写法示例:

  1. <template>
  2. <div class="combinatorial">
  3. <h2 class="title">组合式Api</h2>
  4. <div class="content">
  5. {{ message }}
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. /**
  11. 标准的setup()写法可以同时兼容 选项式和组合式的API写法;但是!不推荐,合体写法可读性差,也不好维护。
  12. **/
  13. import {ref} from 'vue'
  14. export default {
  15. setup () {
  16. const message = ref('Combinatorial')
  17. return { message }
  18. }
  19. }
  20. </script>
  21. <style scoped lang="scss">
  22. </style>

简洁写法示例:

  1. <template>
  2. <div class="combinatorial">
  3. <h2 class="title">组合式Api</h2>
  4. <div class="content">
  5. {{ message }}
  6. </div>
  7. </div>
  8. </template>
  9. <script setup>
  10. import {ref} from 'vue'
  11. const message = ref('Combinatorial')
  12. defineExpose({
  13. message
  14. })
  15. </script>
  16. <style scoped lang="scss">
  17. </style>

事件处理

推荐组合式的代码,因为维护方便、可读性高!

事件-组合式Api:

  1. <template>
  2. <div>
  3. <h3>事件-组合式Api:</h3>
  4. <button @click="increment">点击我</button>
  5. <p>当前计数: {{ count }}</p>
  6. </div>
  7. </template>
  8. <script setup>
  9. import { ref } from 'vue'
  10. const count = ref(0)
  11. function increment() {
  12. count.value++
  13. }
  14. </script>

事件-选项式Api:

  1. <template>
  2. <div>
  3. <h3>事件-选项式Api:</h3>
  4. <button @click="increment">点击我</button>
  5. <p>当前计数: {{ count }}</p>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. count: 0
  13. }
  14. },
  15. methods: {
  16. increment() {
  17. this.count++
  18. }
  19. }
  20. }
  21. </script>

监听器

推荐组合式的代码,因为维护方便、可读性高!
组合式的watch的有点:更灵活,也可以独立函数处理watch多处监听!

watch 独立

  1. import { watch } from 'vue'
  2. function countWatch (count) {
  3. watch(count, (new, old) => {
  4. console.log(new, old)
  5. })
  6. }
  7. export default {
  8. countWatch
  9. }

监听器-组合式Api:

  1. <template>
  2. <div>
  3. <h3>事件-组合式Api:</h3>
  4. <button @click="increment">点击我</button>
  5. <p>当前计数: {{ count }}</p>
  6. </div>
  7. </template>
  8. <script setup>
  9. import { ref, watch } from 'vue'
  10. const count = ref(0)
  11. function increment() {
  12. count.value++
  13. }
  14. watch(count, (newVal, oldVal) => {
  15. console.log('count 变化了', newVal, oldVal)
  16. })
  17. </script>

监听器-选项式Api:

  1. <template>
  2. <div>
  3. <h3>事件-选项式Api:</h3>
  4. <button @click="increment">点击我</button>
  5. <p>当前计数: {{ count }}</p>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. count: 0
  13. }
  14. },
  15. watch: {
  16. count(newVal, oldVal) {
  17. console.log('count 变化了', newVal, oldVal)
  18. }
  19. },
  20. methods: {
  21. increment() {
  22. this.count++
  23. }
  24. }
  25. }
  26. </script>

生命周期

每个 Vue 组件都有自己的生命周期,从创建、更新、卸载都有钩子回调函数给到使用,vue3 组合式的钩子更加灵活,可以多处灵活使用,用到哪个钩子函数导入钩子函数即可;

vue2 生命周期-选项式Api:

  1. <script>
  2. export default {
  3. created() {
  4. console.log('created 钩子函数被调用')
  5. },
  6. mounted() {
  7. console.log('mounted 钩子函数被调用')
  8. },
  9. beforeUpdate() {
  10. console.log('beforeUpdate 钩子函数被调用')
  11. },
  12. updated() {
  13. console.log('updated 钩子函数被调用')
  14. },
  15. beforeUnmount() {
  16. console.log('beforeUnmount 钩子函数被调用')
  17. }
  18. }
  19. </script>

vue3 生命周期-组合式Api:

  1. <script setup>
  2. import { onBeforeMount, onMounted } from 'vue'
  3. onBeforeMount(() => {
  4. console.log('onBeforeMount 钩子函数被调用')
  5. })
  6. onMounted(() => {
  7. console.log('onMounted 钩子函数被调用1')
  8. })
  9. onMounted(() => {// 可多处重复使用执行
  10. console.log('onMounted 钩子函数被调用2')
  11. })
  12. </script>

模板引用

可通过 ref、useTemplateRef获取ref对象

vue2 选项式

  1. <template>
  2. <div>
  3. <h3>模板引用ref - 选项式:</h3>
  4. <p ref="pppp">修改之前-选项式</p>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. mounted() {
  10. this.$refs.pppp.innerHTML = '修改之后-选项式'
  11. }
  12. }
  13. </script>

vue3 组合式

  1. <template>
  2. <div>
  3. <h3>模板引用ref-组合式Api:</h3>
  4. <p ref="pppp">修改之前-组合式</p>
  5. </div>
  6. </template>
  7. <script setup>
  8. import { ref, useTemplateRef, onMounted } from 'vue'
  9. // 方法一: ref 方式
  10. // const pppp = ref(null)
  11. // 方法二: useTemplateRef 方式
  12. const pppp2 = useTemplateRef('pppp')
  13. onMounted(() => {
  14. pppp.value.innerHTML = '修改之后-组合式'
  15. })
  16. </script>

属性Props

vue2

  1. <template>
  2. <div>
  3. <h3>属性props - 选项式:</h3>
  4. <p>{{ message }}</p>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: {
  10. message: {
  11. type: String,
  12. default: '修改之前-选项式'
  13. }
  14. }
  15. }
  16. </script>

vue3组合式

定义接口:defineProps()

  1. <template>
  2. <div>
  3. <h3>属性props-组合式Api:</h3>
  4. <p>{{ message }}</p>
  5. </div>
  6. </template>
  7. <script setup>
  8. const props = defineProps({
  9. message: {
  10. type: String,
  11. default: '修改之前-组合式'
  12. }
  13. })
  14. console.log(props.message)
  15. </script>

事件Emit

vue 2

  1. <template>
  2. <div>
  3. <h3>事件Emit - 选项式:</h3>
  4. <button @click="emitEvent">触发事件</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. emitEvent() {
  11. this.$emit('customEvent', '你好,选项式Api')
  12. }
  13. }
  14. }
  15. </script>

vue3

关键字:emitEvent, 定义Emit事件,和vue2还是区别明显

  1. <template>
  2. <div>
  3. <h3>事件Emit - 组合式Api:</h3>
  4. <button @click="emitEvent">触发事件</button>
  5. </div>
  6. </template>
  7. <script setup>
  8. // 关键字defineEmits 定义事件
  9. const emit = defineEmits(['customEvent'])
  10. const emitEvent = () => {
  11. emit('customEvent', '你好,组合式Api')
  12. }
  13. </script>

自定义指令

vue 2

  1. <template>
  2. <div>
  3. <h3>指令 - 选项式:</h3>
  4. <div v-highlight="color">
  5. 这是一个指令示例
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. color: 'red'
  14. }
  15. },
  16. directives: {
  17. highlight: {
  18. mounted(el, binding) {
  19. el.style.backgroundColor = binding.value
  20. }
  21. }
  22. }
  23. }
  24. </script>

vue3

  1. <template>
  2. <div>
  3. <h3>指令 - 组合式Api:</h3>
  4. <div v-highlight="color">
  5. 这是一个指令示例
  6. </div>
  7. </div>
  8. </template>
  9. <script setup>
  10. import { ref } from 'vue'
  11. const color = ref('red')
  12. const vHighlight = {
  13. mounted(el, binding) {
  14. el.style.backgroundColor = binding.value
  15. }
  16. }
  17. </script>

全局指令示例

  1. import { createApp } from 'vue'
  2. const app = createApp(App)
  3. // 注册全局指令
  4. app.directive('blue', {
  5. mounted(el) {
  6. el.style.backgroundColor = 'blue'
  7. }
  8. })
  9. app.mount('#app')

指令钩子

  1. const myDirective = {
  2. // 在绑定元素的 attribute 前
  3. // 或事件监听器应用前调用
  4. created(el, binding, vnode) {
  5. // 下面会介绍各个参数的细节
  6. },
  7. // 在元素被插入到 DOM 前调用
  8. beforeMount(el, binding, vnode) {},
  9. // 在绑定元素的父组件
  10. // 及他自己的所有子节点都挂载完成后调用
  11. mounted(el, binding, vnode) {},
  12. // 绑定元素的父组件更新前调用
  13. beforeUpdate(el, binding, vnode, prevVnode) {},
  14. // 在绑定元素的父组件
  15. // 及他自己的所有子节点都更新后调用
  16. updated(el, binding, vnode, prevVnode) {},
  17. // 绑定元素的父组件卸载前调用
  18. beforeUnmount(el, binding, vnode) {},
  19. // 绑定元素的父组件卸载后调用
  20. unmounted(el, binding, vnode) {}
  21. }
读后有收获可以支付宝请作者喝咖啡
下一篇 >
文章评论
评论功能改造中...
湘ICP备15005320号-1 似懂非懂 Powered by doyo. 网站地图
返回顶部