美文网首页
Vue3核心知识

Vue3核心知识

作者: _我和你一样 | 来源:发表于2024-05-31 21:08 被阅读0次

Vue 3 是一个现代化的前端框架,通过一系列简单而强大的特性,使得构建用户界面变得更加高效。以下是Vue 3的核心知识点,通过这些知识点,你可以快速上手并运用 Vue 3。

创建你的应用

Vue 3 推荐使用 Vite 来快速创建开发环境:

npm init vite-app <project-name>
cd <project-name>
npm install
npm run dev

模板语法

文本插值

<span> {{ msg }} </span>
<span v-text='msg'></span>

设置内部 HTML

<span v-html='rawHTML'></span>

使用 JS 表达式

<span> {{ msg.split('').reverse().join('') }} </span>

指令

条件渲染

<div v-if='show'>显示</div>
<div v-else-if='showElse'>显示其他</div>
<div v-else>隐藏</div>

事件处理

<button @click='handleClick'>点击我</button>
const handleClick = (event) => {
  console.log(event.target);
};

列表渲染

<ul>
  <li v-for='(item, index) in items' :key='index'>{{ item }}</li>
</ul>

双向绑定

<input v-model='inputValue' />

绑定数据

简单绑定

<div :id='dynamicId'></div>

绑定类和样式

<div :class='{ active: isActive }'></div>
<div :style='{ margin: marginValue + "px" }'></div>

父子组件通信

父组件向子组件传递数据

<child-component :msg='parentMsg' @update='handleUpdate'></child-component>

子组件通过 emit 发送事件

// 子组件
context.emit('update', 'new value');

插槽 (Slots)

基本插槽

<!-- 子组件 -->
<div>
  <slot></slot>
</div>

<!-- 父组件 -->
<child-component>
  这里的内容会替换插槽内容
</child-component>

命名插槽

<!-- 子组件 -->
<div>
  <slot name='header'></slot>
  <slot></slot>
</div>

<!-- 父组件 -->
<child-component>
  <template v-slot:header>头部内容</template>
  默认插槽内容
</child-component>

组合 API (Composition API)

Vue 3 引入了 Composition API,使得逻辑组织更加灵活:

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';

const count = ref(0);
const state = reactive({ message: 'Hello Vue 3' });

const increment = () => {
  count.value++;
};

onMounted(() => {
  console.log('Component mounted!');
});

</script>

<template>
  <div>{{ state.message }}</div>
  <button @click="increment">Count: {{ count }}</button>
</template>

生命周期钩子

onMounted

import { onMounted } from 'vue';

setup() {
  onMounted(() => {
    console.log('组件挂载完成');
  });
}

其他钩子

  • onBeforeMount
  • onBeforeUpdate
  • onUpdated
  • onBeforeUnmount
  • onUnmounted

计算属性和侦听器

计算属性

<script lang="ts" setup>
import { ref, computed } from 'vue';

const a = ref(1);
const b = computed(() => a.value * 2);
</script>

<template>
  <div>{{ b }}</div>
</template>

侦听器

<script lang="ts" setup>
import { ref, watchEffect } from 'vue';

const site = ref('learnvue.co');

watchEffect(() => {
  console.log(site.value);
});
</script>

通过以上内容,你可以快速掌握 Vue 3 的核心知识,并开始构建功能强大的前端应用。对于更多详细的用法和示例,可以参考官方文档和相关教程。

相关文章

  • 【浅谈Vue3 effect】

    Vue3 中引入了 proxy进行数据劫持,而effect是响应式系统的核心,而响应式系统又是 vue3 中的核心...

  • vue3源码之reactive

    vue3源码之reactive.ts解析 reactivity是vue响应式的核心 这块在vue3中完全重构了 采...

  • 组合式API介绍

    ?最近重新夯实Vue3,梳理的相关知识点和细节 本文关于Vue3中的组合API的介绍和基本使用 我们知道Vue3引...

  • vue3 实现 v-model 原理

    vue3 源码正式放出来了,想必大家也都开始争先恐后的学习 vue3 的知识了。由于 vue3 已经不再支持 v-...

  • 手写vue3的响应式核心reactive

    近期在看Vue3的源码,看完后也根据一些阅读文档的时候的注释,打算手写实现模拟vue3的核心代码,作为读书笔记,方...

  • vue3-compositionapi使用尝试

    最近看了看vue3知识,对组合api做个简单的使用尝试,记录一下

  • vue基础题

    Vue2和Vue3的双向数据绑定的原理 Vue2的核心就是通过Object.defineProperty()方法设...

  • Vue3-TypeScript-接口-async-axios用法

    1.前言 1.书接上文vue3 ts核心语法[https://www.jianshu.com/p/0da266d1...

  • vue3项目

    安装过程 1、安装vue3 2、创建vue3项目 3、启动vue3项目 4、vue3加载过程 加载index.ht...

  • 每天5分钟玩转Docker容器技术(二)

    容器核心知识 本篇通过 Docker 讨论容器的核心知识。 概述 容器核心知识主要回答有关容器 What、Why ...

网友评论

      本文标题:Vue3核心知识

      本文链接:https://www.haomeiwen.com/subject/jmydqjtx.html