【VUE3】

Vue 3 是当下最流行的前端框架之一,其主要特点是性能更好、体积更小、更易于维护。下面是 Vue 3 的一些重要知识点和代码示例:

  1. 创建 Vue 实例

    import { createApp } from 'vue'

    const app = createApp({
    data() {
    return {
    message: 'Hello, Vue 3!'
    }
    }
    })

    app.mount('#app')

2.组件

复制代码
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    title: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      message: 'Hello, Vue 3!'
    }
  },
  methods: {
    handleClick() {
      console.log('Button clicked')
    }
  },
  template: `
    <div>
      <h1>{{ title }}</h1>
      <p>{{ message }}</p>
      <button @click="handleClick">Click me</button>
    </div>
  `
})

3.生命周期钩子函数

复制代码
import { onMounted, onUpdated, onUnmounted } from 'vue'

export default {
  setup() {
    onMounted(() => {
      console.log('Component mounted')
    })

    onUpdated(() => {
      console.log('Component updated')
    })

    onUnmounted(() => {
      console.log('Component unmounted')
    })
  }
}

4.模板引用和插槽

复制代码
<template>
  <div>
    <h1 ref="titleRef">{{ title }}</h1>
    <slot></slot>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      required: true
    }
  },
  mounted() {
    // 操作模板引用
    console.log(this.$refs.titleRef)
  }
}
</script>

5.响应式数据

复制代码
import { reactive } from 'vue'

export default {
  setup() {
    const state = reactive({
      message: 'Hello, Vue 3!'
    })

    return {
      state
    }
  }
}

6.生命周期函数

复制代码
import { onMounted, onUpdated, onUnmounted } from 'vue'

export default {
  setup() {
    onMounted(() => {
      console.log('Component mounted')
    })

    onUpdated(() => {
      console.log('Component updated')
    })

    onUnmounted(() => {
      console.log('Component unmounted')
    })
  }
}

7.组件通信

复制代码
// 父组件
<template>
  <div>
    <child :message="message" @update-message="updateMessage" />
  </div>
</template>

<script>
import Child from './Child.vue'

export default {
  components: {
    Child
  },
  data() {
    return {
      message: 'Hello, Vue 3!'
    }
  },
  methods: {
    updateMessage(newMessage) {
      this.message = newMessage
    }
  }
}
</script>

// 子组件
<template>
  <div>
    <p>{{ message }}</p>
    <button @click="handleClick">Update message</button>
  </div>
</template>

<script>
import { defineEmits, defineProps } from 'vue'

export default {
  props: defineProps({
    message: {
      type: String,
      required: true
    }
  }),
  emits: defineEmits(['update-message']),
  methods: {
    handleClick() {
      this.$emit('update-message', 'New message')
    }
  }
}
</script>

这些是 Vue 3 的一些重要知识点和代码示例,但不仅限于此。作为一个全面的前端框架,Vue 3 还有许多其他功能和工具,可以让您编写出更好的 Web 应用程序。

相关推荐
Moment4 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
爱敲代码的小鱼5 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
吹牛不交税5 小时前
admin.net-v2 框架使用笔记-netcore8.0/10.0版
vue.js·.netcore
MZ_ZXD0016 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
铅笔侠_小龙虾7 小时前
Flutter 实战: 计算器
开发语言·javascript·flutter
大模型玩家七七7 小时前
梯度累积真的省显存吗?它换走的是什么成本
java·javascript·数据库·人工智能·深度学习
2501_944711437 小时前
JS 对象遍历全解析
开发语言·前端·javascript
发现一只大呆瓜8 小时前
虚拟列表:支持“向上加载”的历史消息(Vue 3 & React 双版本)
前端·javascript·面试
阔皮大师8 小时前
INote轻量文本编辑器
java·javascript·python·c#
lbb 小魔仙8 小时前
【HarmonyOS实战】React Native 表单实战:自定义 useReactHookForm 高性能验证
javascript·react native·react.js