NUXT3中有什么办法可以加载插件一次?

发布于 2025-02-12 19:12:41 字数 1147 浏览 0 评论 0原文

我正在尝试集成 sequelize 我的 nuxt 3 项目。但是,我无法弄清楚如何仅将其加载一次,而不是每次刷新 /导航到其他路线时重新加载它。

我找不到有关文档的任何信息。有可能吗?

〜/plugins/quelize.server.ts

import { Sequelize } from "sequelize"

export default defineNuxtPlugin(async (nuxtApp) => {
    const config = useRuntimeConfig()
    
    const sequelize = new Sequelize(config.dbName, config.dbUser, config.dbPass,{
        host: config.dbHost,
        port: parseInt(config.dbPort),
        dialect: 'mysql',
    })

    try {
        await sequelize.authenticate()

        // this log was executed every time I navigate to a new route
        // or refreshing the browser.
        console.log('Connection has been established successfully.');
    } catch (error) {
        console.error('Unable to connect to the database:', error);
    }
    
    return {
        provide: {
            db: sequelize
        }
    }
})

I'm trying to integrate Sequelize to my Nuxt 3 project. However, I couldn't figure out how to make it load only once instead of reloading it every time the page was refreshed / navigating to another routes.

I couldn't find any information on the docs. Is it even possible?

~/plugins/sequelize.server.ts

import { Sequelize } from "sequelize"

export default defineNuxtPlugin(async (nuxtApp) => {
    const config = useRuntimeConfig()
    
    const sequelize = new Sequelize(config.dbName, config.dbUser, config.dbPass,{
        host: config.dbHost,
        port: parseInt(config.dbPort),
        dialect: 'mysql',
    })

    try {
        await sequelize.authenticate()

        // this log was executed every time I navigate to a new route
        // or refreshing the browser.
        console.log('Connection has been established successfully.');
    } catch (error) {
        console.error('Unable to connect to the database:', error);
    }
    
    return {
        provide: {
            db: sequelize
        }
    }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2025-02-19 19:12:41

OP通过删除在组件的安装生命周期挂钩上初始化的合并来解决他的问题。

只是剩余的代码。

OP solved his issue by removing a composable that was initialized on a component's mounted lifecycle hook.

Just a remaining piece of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文