themusecatcher / vue-amazing-ui

Vue3 + TypeScript + Vite + Less 开发的常用基础 UI 组件库!如果好用,记得来颗 ⭐️⭐️ 哦 🫶🫶🫶
https://themusecatcher.github.io/vue-amazing-ui/
207 stars 33 forks source link

如何挂载全局message #22

Closed Ncowaedly closed 2 hours ago

themusecatcher commented 2 hours ago

假设已经全局引入 VueAmazingUIapp.vue 中加入如下代码即可:

<script setup lang="ts">
import { ref } from 'vue' 
const message = ref()
onMounted(() => {
  window['$message'] = message.value
})
onBeforeUnmount(() => {
  delete window['$message']
})
</script>
<template>
  <RouterView />
  <Message ref="message" />
</template>

然后在任何页面中直接使用:

<script setup lang="ts">
const $message = window['$message']
function onClick() {
  $message.success('点击了按钮')
}
</script>
<template>
  <Button @click="onClick">按钮</Button>
</template>
Ncowaedly commented 2 hours ago

我想在main.ts使用app.config.globalProperties.$message挂载

themusecatcher commented 2 hours ago

挂载到 app.config.globalProperties 的变量 用起来并没有 window 上方便