美文网首页现代前端指南!
vue3 使用 defineOptions

vue3 使用 defineOptions

作者: BitMonkey | 来源:发表于2022-11-25 11:21 被阅读0次

<script setup lang="ts">

import { ref, reactive, ComponentPublicInstance } from 'vue'

// * defineExpose暴露出来的方法,接口实现
interface IInstance extends ComponentPublicInstance {
getData(): void
}

defineOptions({
name: '***',
beforeRouteEnter(_to, _from, next) {
next((vm) => {
const instance = vm as IInstance
instance.getData() // 刷新列表数据(不缓存)
})
}
})

// 获取表格数据(示例方法)
const listData = ref([])
const getData = async () => {
listData.value = []
}

// * beforeRouteEnter中要用到的方法,需要暴露出来
defineExpose({ getData })
</script>

安装依赖

npm i unplugin-vue-macros -D

配置依赖

// vite.config.ts
import DefineOptions from 'unplugin-vue-define-options/vite'
export default defineConfig({
  plugins: [DefineOptions()],
})

ts 支持

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "types": ["unplugin-vue-define-options/macros-global" /* ... */]
  }
}

更多使用细节请看文档

https://vue-macros.sxzz.moe/macros/define-options.html

相关文章

  • vue3 使用 defineOptions

    import { ref, reactive, ComponentPublicInstance } from 'v...

  • vue3对比vue2使用keep-alive router-vi

    vue 2 使用 vue3 使用 vue3 keep-alive 文档地址: https://v3.cn.vuej...

  • provide inject在vue2和vue3中的使用

    vue2父组件 vue2子组件 vue3父组件 vue3子组件 vue3官方详细使用provide inject地...

  • docker 搭建 vue3 + vite

    vue3发布了,今天就分享一下我使用docker 搭建 vue3 + vite 开发环境。至于为什么使用docke...

  • vue3特性之一 : 组合api

    setup() 函数是 vue3 中,专门为组件提供的新属性。它为我们使用 vue3 的 Composition ...

  • vue3拖拽demo

    vue3使用vue-draggable会有如下bug vue3要安装vue-draggable-next 代码 v...

  • vue2与vue3的区别

    最近接触并了解了vue3,发现vue2的语法vue3能够继续使用,并没有任何影响,但是vue3还是和vue2...

  • 组合式API介绍

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

  • Vue3中使用Pinia

    Vue2 中使用Vuex进行状态管理,在Vue3中,引入了Pinia,如果使用Vue3的脚手架搭建项目,其中包含了...

  • vue安装veux

    现在使用npm i 会默认安装vuex4,vuex4只适用于vue3,如果使用的vue环境是vue3,安装vuex...

网友评论

    本文标题:vue3 使用 defineOptions

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