我正试图将Sequelize集成到我的next 3项目。然而,我不知道如何让它只加载一次,而不是每次刷新页面或导航到另一个路由时重新加载它。
我在文件上找不到任何信息。这可能吗?
~/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
}
}
})
OP通过删除在组件的mounted
生命周期钩子上初始化的可组合组件来解决这个问题。
只是剩下的一段代码。