feat: 处理编译问题

This commit is contained in:
Tim
2025-08-07 22:28:39 +08:00
parent 676e959d4b
commit 51e958799d
3 changed files with 93 additions and 33 deletions

View File

@@ -0,0 +1,74 @@
// 导出一个便捷的 toast 对象
export const toast = {
success: async (message) => {
if (process.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
toastInstance.success(message)
} catch (error) {
console.warn('Toast not available:', error)
}
}
},
error: async (message) => {
if (process.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
toastInstance.error(message)
} catch (error) {
console.warn('Toast not available:', error)
}
}
},
warning: async (message) => {
if (process.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
toastInstance.warning(message)
} catch (error) {
console.warn('Toast not available:', error)
}
}
},
info: async (message) => {
if (process.client) {
try {
const { useToast } = await import('vue-toastification')
const toastInstance = useToast()
toastInstance.info(message)
} catch (error) {
console.warn('Toast not available:', error)
}
}
}
}
// 导出 useToast composable
export const useToast = () => {
if (process.client) {
return new Promise(async (resolve) => {
try {
const { useToast: useVueToast } = await import('vue-toastification')
resolve(useVueToast())
} catch (error) {
console.warn('Toast not available:', error)
resolve({
success: () => {},
error: () => {},
warning: () => {},
info: () => {}
})
}
})
}
return Promise.resolve({
success: () => {},
error: () => {},
warning: () => {},
info: () => {}
})
}

View File

@@ -5,28 +5,5 @@ export const GITHUB_CLIENT_ID = 'Ov23liVkO1NPAX5JyWxJ'
export const DISCORD_CLIENT_ID = '1394985417044000779'
export const TWITTER_CLIENT_ID = 'ZTRTU05KSk9KTTJrTTdrVC1tc1E6MTpjaQ'
// 导入真实的 toast 功能
import { useToast } from 'vue-toastification'
// 创建一个全局的 toast 实例
let toastInstance = null
// 初始化 toast 实例的函数
export const initToast = () => {
if (!toastInstance) {
toastInstance = useToast()
}
return toastInstance
}
// 导出 toast 对象,提供 success 和 error 方法
export const toast = {
success: (message) => {
const toast = initToast()
toast.success(message)
},
error: (message) => {
const toast = initToast()
toast.error(message)
}
}
// 重新导出 toast 功能,使用 composable 方式
export { toast } from './composables/useToast'

View File

@@ -1,13 +1,22 @@
import { defineNuxtPlugin } from '#app'
import Toast, { POSITION } from 'vue-toastification'
import 'vue-toastification/dist/index.css'
import '~/assets/toast.css'
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.vueApp.use(Toast, {
position: POSITION.TOP_RIGHT,
containerClassName: 'open-isle-toast-style-v1',
transition: 'Vue-Toastification__fade',
timeout: 2000,
})
export default defineNuxtPlugin(async (nuxtApp) => {
// 确保只在客户端环境中注册插件
if (process.client) {
try {
// 使用动态导入来避免 CommonJS 模块问题
const { default: Toast, POSITION } = await import('vue-toastification')
nuxtApp.vueApp.use(Toast, {
position: POSITION.TOP_RIGHT,
containerClassName: 'open-isle-toast-style-v1',
transition: 'Vue-Toastification__fade',
timeout: 2000,
})
} catch (error) {
console.warn('Failed to load vue-toastification:', error)
}
}
})