当前位置: 首页 > news >正文

带你走进vue3

Vue 3知识点整合大纲

  1. 介绍

    • 什么是Vue 3
    • 与Vue 2的主要区别
    • Vue 3的优势与应用场景
  2. 创建Vue 3项目

    • 使用Vue CLI创建项目
    • 项目结构介绍
    • 项目配置
  3. 基本概念

    • 响应式系统
      • Reactive和Ref
      • Computed和Watch
    • Composition API
      • setup函数
      • Reactive和Ref在Composition API中的使用
      • 生命周期钩子
    • Options API
      • Data
      • Methods
      • Computed
      • Watchers
      • 生命周期钩子
  4. 模板与指令

    • Vue模板语法
    • 条件渲染
    • 列表渲染
    • 事件处理
    • 表单绑定
    • 组件和插槽
  5. 组件

    • 组件基础
    • 父子组件通信
    • Provide/Inject
    • 动态组件
    • 异步组件

1. 介绍

什么是Vue 3

Vue 3是尤雨溪(Evan You)团队开发的一款用于构建用户界面的渐进式JavaScript框架。Vue 3对比Vue 2进行了全面升级,不仅在性能上有了显著提升,还引入了许多新特性,如Composition API、Fragments、Teleport以及更好的TypeScript支持。

与Vue 2的主要区别
  1. 性能提升: Vue 3通过编译器优化、代理对象(proxy)代替getter/setter以及优化虚拟DOM算法,使得应用性能显著提升。
  2. Composition API: 新引入的Composition API使逻辑复用性更强,并且在大型项目中更易于组织代码。
  3. Fragments和Teleport: Vue 3支持多个根节点(Fragments),并引入Teleport特性,可以将模板的一部分渲染到指定位置,不再局限于父子关系。
  4. 更好的TypeScript支持: Vue 3在设计之初就充分考虑了TypeScript的支持,使开发者能够更安全地编写代码。
  5. Tree-shaking: 提供了更好的Tree-shaking支持,使得打包体积更小。
Vue 3的优势与应用场景

优势:

  • 性能优异:更快的虚拟DOM和响应式系统。
  • 更好的代码组织:Composition API使得代码模块划分更加清晰。
  • 现代工具链:Vue 3与现代开发工具(如Vite)无缝集成,提升开发体验。

应用场景:

  • 中大型前端项目:例如电商平台、社交网络、后台管理系统。
  • 移动端应用:通过Vue与跨平台框架如Ionic、Weex或Capacitor结合。
  • 单页应用(SPA):通过Vue Router和Vuex打造高性能SPA。

2. 创建Vue 3项目

使用Vue CLI创建项目

Vue CLI是Vue官方提供的一款脚手架工具,它极大地简化了项目的创建和配置过程。以下是使用Vue CLI创建Vue 3项目的步骤:

  1. 安装Vue CLI:

    npm install -g @vue/cli
    
  2. 创建新项目:

    vue create my-vue3-project
    
  3. 选择所需的配置:

    • 手动选择特性:如Babel、TypeScript、Router、Vuex、CSS Pre-processors等。
    • 保存配置以供将来使用。
  4. 进入项目目录:

    cd my-vue3-project
    
  5. 启动开发服务器:

    npm run serve
    

于此,你将成功创建并运行一个基本的Vue 3项目。

项目结构介绍

一个典型的Vue 3项目包含以下文件和目录:

  • node_modules/: 项目依赖包存放目录。

  • public/: 静态文件,例如html、ico等。

  • src/: 源代码目录,主要包括以下子目录和文件:

    • assets/: 存放静态资源,如图片、CSS文件。
    • components/: 通用组件存放目录。
    • views/: 页面级别组件存放目录(当使用vue-router时)。
    • App.vue: 根组件。
    • main.js: 入口文件。
    • router/index.js: 路由配置文件(当使用vue-router时)。
    • store/index.js: 状态管理配置文件(当使用vuex时)。
  • package.json: 项目配置文件,记录项目信息和依赖包。

  • babel.config.js: Babel配置文件。

  • vue.config.js: Vue CLI配置文件。

项目配置

Vue CLI提供了极大的灵活性,允许通过vue.config.js文件进行项目配置。以下是一些常见的配置项:

module.exports = {// 基本路径publicPath: './', // 生成的打包文件目录outputDir: 'dist',// 存放静态文件的目录assetsDir: 'static',// eslint-loader 是否在保存的时候检查lintOnSave: true,// 是否使用带有浏览器内编译器的完整构建版本runtimeCompiler: false,// 开启生产环境的 source mapproductionSourceMap: false,// 三方插件的选项pluginOptions: {},// webpack配置configureWebpack: {resolve: {alias: {'@': path.resolve(__dirname, 'src')}}},// 开发服务器配置devServer: {open: true,host: 'localhost',port: 8080,https: false,hotOnly: false,proxy: null,}
};

3. 基本概念

响应式系统

Vue 3的响应式系统是其核心特性之一,Vue 3使用Proxy代替Vue 2中的getter/setter实现,实现了更高效的响应追踪和更少的性能开销。

Reactive和Ref

reactive用于创建响应式对象,而ref用于创建基本类型的响应式引用。

import { reactive, ref } from 'vue';const state = reactive({ count: 0 });const count = ref(0);state.count++;
count.value++;
Computed和Watch

computed用于定义计算属性,watch用于监听数据变化。

import { reactive, computed, watch } from 'vue';const state = reactive({ count: 0 });const doubledCount = computed(() => state.count * 2);watch(() => state.count, (newValue, oldValue) => {console.log(`Count changed from ${oldValue} to ${newValue}`);
});
Composition API

Composition API是Vue 3的亮点,使得代码在逻辑复用和模块化方面更加高效和灵活。

setup函数

setup函数是Composition API的核心,用于组合逻辑片段。

import { reactive, toRefs } from 'vue';export default {setup() {const state = reactive({ count: 0 });function increment() {state.count++;}return {...toRefs(state),increment};}
};
生命周期钩子

Vue 3中,生命周期钩子可以在setup中使用组合式API的函数来管理。

import { onMounted, onUnmounted } from 'vue';export default {setup() {onMounted(() => {console.log('Component is mounted!');});onUnmounted(() => {console.log('Component is unmounted!');});}
};

4. 模板与指令

Vue模板语法

Vue的模板语法基于HTML,允许将模板与数据绑定,从而自动渲染界面。

<template><div><h1>{{ message }}</h1></div>
</template><script>
export default {data() {return {message: 'Hello, Vue 3!'};}
};
</script>
条件渲染

可以使用v-ifv-show进行条件渲染。

<template><div v-if="isShown">Content is shown</div><div v-else>Content is hidden</div>
</template><script>
export default {data() {return {isShown: true};}
};
</script>
列表渲染

可以使用v-for进行列表渲染。

<template><ul><li v-for="item in items" :key="item.id">{{ item.name }}</li></ul>
</template><script>
export default {data() {return {items: [{ id: 1, name: 'Item 1' },{ id: 2, name: 'Item 2' }]};}
};
</script>
事件处理

可以使用v-on进行事件处理。

<template><button @click="handleClick">Click Me</button>
</template><script>
export default {methods: {handleClick() {console.log('Button clicked!');}}
};
</script>
表单绑定

可以使用v-model进行双向绑定。

<template><input v-model="message" />
</template><script>
export default {data() {return {message: ''};}
};
</script>
组件和插槽

可以定义和使用组件,以及使用插槽实现组件内容分发。

<!-- ParentComponent.vue -->
<template><ChildComponent><template v-slot:default><p>This is a slot content</p></template></ChildComponent>
</template><script>
import ChildComponent from './ChildComponent.vue';export default {components: {ChildComponent}
};
</script><!-- ChildComponent.vue -->
<template><div><slot></slot></div>
</template>

5. 组件

组件基础

组件是Vue应用的基本组成单位,可以通过<script setup><script>来定义。

<!-- MyComponent.vue -->
<template><div>{{ message }}</div>
</template><script>
export default {data() {return {message: 'Hello from MyComponent'};}
};
</script><!-- App.vue -->
<template><MyComponent />
</template><script>
import MyComponent from './MyComponent.vue';export default {components: {MyComponent}
};
</script>
父子组件通信

可以通过propsemit进行父子组件通信。

<!-- ChildComponent.vue -->
<template><button @click="emit('increment')">Increment</button>
</template><script>
export default {emits: ['increment']
};
</script><!-- ParentComponent.vue -->
<template><ChildComponent @increment="handleIncrement" />
</template><script>
import ChildComponent from './ChildComponent.vue';export default {components: {ChildComponent},methods: {handleIncrement() {console.log('Increment event received');}}
};
</script>
Provide/Inject

provideinject用于跨层级组件间通信。

<!-- ParentComponent.vue -->
<template><ChildComponent />
</template><script>
import { provide, ref } from 'vue';
import ChildComponent from './ChildComponent.vue';export default {components: {ChildComponent},setup() {const sharedData = ref('Shared Data');provide('sharedData', sharedData);}
};
</script><!-- ChildComponent.vue -->
<template><div>{{ sharedData }}</div>
</template><script>
import { inject } from 'vue';export default {setup() {const sharedData = inject('sharedData');return {sharedData};}
};
</script>
动态组件

可以使用<component>is属性实现动态组件。

<template><component :is="currentComponent" />
</template><script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';export default {data() {return {currentComponent: 'ComponentA'};},components: {ComponentA,ComponentB}
};
</script>
异步组件

可以使用defineAsyncComponent定义异步组件。

import { defineAsyncComponent } from 'vue';export default {components: {AsyncComponent: defineAsyncComponent(() => import('./AsyncComponent.vue'))}
};

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Porcupine - 语音关键词唤醒引擎
  • Vue 3有哪些新特性
  • Linux5-echo,>,tail
  • 站群服务器适用于哪些场景当中?
  • 算法入门-贪心1
  • wordpress源码资源站整站打包32GB数据,含6.7W条资源数据
  • vscode中如何配置c/c++环境
  • C++ Linux多线程同步通信-信号量
  • Linux系统应用之知识补充——OpenEuler(欧拉)的安装和基础配置
  • linux-centos 设置系统时间
  • python selenium网页操作
  • GlusterFS分布式存储
  • 【OJ刷题】双指针问题6
  • pWnOS的第二种全新解法(ssh私钥破解、webmin漏洞提权)
  • C++3D迷宫
  • 【EOS】Cleos基础
  • Angular 响应式表单之下拉框
  • dva中组件的懒加载
  • eclipse(luna)创建web工程
  • GraphQL学习过程应该是这样的
  • gulp 教程
  • JavaScript异步流程控制的前世今生
  • js对象的深浅拷贝
  • Python中eval与exec的使用及区别
  • text-decoration与color属性
  • vuex 笔记整理
  • Vue组件定义
  • 阿里研究院入选中国企业智库系统影响力榜
  • 不发不行!Netty集成文字图片聊天室外加TCP/IP软硬件通信
  • 从零开始学习部署
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 区块链共识机制优缺点对比都是什么
  • 如何编写一个可升级的智能合约
  • 微信小程序开发问题汇总
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 小李飞刀:SQL题目刷起来!
  •  一套莫尔斯电报听写、翻译系统
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • Android开发者必备:推荐一款助力开发的开源APP
  • mysql面试题分组并合并列
  • ​Java基础复习笔记 第16章:网络编程
  • ## 1.3.Git命令
  • #pragma once与条件编译
  • #Z2294. 打印树的直径
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • #预处理和函数的对比以及条件编译
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (07)Hive——窗口函数详解
  • (1)(1.13) SiK无线电高级配置(六)
  • (力扣记录)1448. 统计二叉树中好节点的数目
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (七)glDrawArry绘制
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (十八)三元表达式和列表解析
  • (译)计算距离、方位和更多经纬度之间的点