Vue3常用知识点总结

在Vue 3中,常用的知识点和API有:

  1. Composition API: 使用setup函数来配合ref, reactive, computedwatch等函数。



import { ref, reactive, computed, watch, onMounted } from 'vue';
 
export default {
  setup() {
    const count = ref(0);
    const state = reactive({ message: 'Hello' });
    const doubledCount = computed(() => count.value * 2);
 
    watch(count, (newValue, oldValue) => {
      console.log(`The new count is ${newValue}, old count was ${oldValue}`);
    });
 
    onMounted(() => {
      console.log('Component is mounted!');
    });
 
    return {
      count,
      state,
      doubledCount
    };
  }
};
  1. Teleport: 使用<Teleport>组件将子组件的内容传送到DOM中的其他位置。



<Teleport to="#some-other-element-id">
  <div>This will be teleported</div>
</Teleport>
  1. Fragments: 使用<!>标签来创建不需要额外DOM节点的组件。



<template>
  <!>
    <div>Part 1</div>
    <div>Part 2</div>
  <!>
</template>
  1. Emits: 使用emit函数来触发自定义事件。



import { defineComponent } from 'vue';
 
export default defineComponent({
  setup(props, { emit }) {
    function doSomething() {
      emit('custom-event', 'some argument');
    }
 
    return { doSomething };
  }
});
  1. Custom Renderer API: 可以创建自定义渲染器来集成Vue到非Web平台。



import { createRenderer } from 'vue';
 
const { render, createApp } = createRenderer({
  patchProp,
  insert,
  remove,
  // ...
});
 
const app = createApp(rootComponent);
app.mount(rootContainer);
  1. Provide/Inject: 父组件提供数据,子组件注入使用。



<!-- ParentComponent.vue -->
<script setup>
import { provide } from 'vue';
 
provide('message', 'Hello from parent');
</script>
 
<!-- ChildComponent.vue -->
<script setup>
import { inject } from 'vue';
 
const message = inject('message');
</script>
  1. Suspense: 用于处理异步组件的加载中、失败等情况。



<Suspense>
  <template #default>
    <AsyncComp />
  </template>
  <template #fallback>
    Loading...
  </template>
</Suspense>
  1. Script Setup 语法糖: 使用<script setup>可以更快速地编写组件。



<script setup>
import { ref } from 'vue';
 
const count = ref(0);
</script>
 
<template>
  <button @click="count++">{{ count }}</button>
</template>
  1. Data Option Composition: 使用setup函数返回的对象来合并datacomputedmethods等。



import { ref } fr
VUE
最后修改于:2024年08月21日 15:30

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日