【问题标题】:trigger function after mounting any page in nuxt在nuxt中挂载任何页面后触发功能
【发布时间】:2020-12-03 23:04:46
【问题描述】:

是否可以在 Nuxt 中编写中间件挂载任何页面后触发。

如果我使用以下中间件

export default function ({ app }) {
  if (!process.server) {
    app.$somePluginFunction()
  }
}

导航到该页面时触发它,因此在安装它之前。这不是我想要的。

我也知道您可以在单个页面上使用 mounted() 挂钩,这正是我想要的,但我不想手动为我的应用程序中的每个页面编写相同的 mounted() 挂钩。我该怎么做?

【问题讨论】:

    标签: vue.js nuxt.js


    【解决方案1】:

    在路由改变之前触发函数的方法有很多:

    在默认布局中首次使用

    // layout/default.vue
    export default {
      watch: {
        $route () {
          console.log('route changed', this.$route)
        }
      },
    }
    

    路线前的第二次使用:

    https://router.vuejs.org/guide/advanced/navigation-guards.html#in-component-guards

    router.beforeEach((to, from, next) => {
      if (to.name !== 'Login' && !isAuthenticated) next({ name: 'Login' })
      else next()
    })
    

    第三次写这样的插件:

    how to write global router-function in nuxt.js

    然后像这样写mixin:

    Run function AFTER route fully rendered in Nuxt.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-05
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      相关资源
      最近更新 更多