vue3 使用 defineOptions

<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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (一) 基本模块 01基础模板语法 1.1插值语法 -- 解析标签体里的内容 data -> {{}} 里可以写j...
    SteinsGate_5e01阅读 9,208评论 0 31
  • Vue -渐进式JavaScript框架 介绍 vue 中文网 vue github Vue.js 是一套构建用户...
    桂_3d6b阅读 4,284评论 0 0
  • 笔记 脚手架文件结构 关于不同版本的Vue vue.js与vue.runtime.xxx.js的区别:vue.js...
    sskingfly阅读 1,007评论 0 0
  • Vue3目前还没有文档,但git上面却有些样本,本来打算但拿它的样本去写一个tutorial,但发现已经有人写了,...
    KunHo阅读 19,128评论 2 13
  • 一、Vue2 1.1 模板语法 1.1.1 模板的理解 html 中包含了一些 JS 语法代码,语法分为两种,分别...
    Cola_Mr阅读 3,785评论 0 1