Vuejs2 未调用Nuxt自定义模块挂钩

Vuejs2 未调用Nuxt自定义模块挂钩,vuejs2,nuxt.js,server-side-rendering,Vuejs2,Nuxt.js,Server Side Rendering,我想在中间件运行后从ssr服务器传递一些额外的数据,并在客户端中间件上使用这些数据。与nuxt对vuex所做的有点类似 在render:contexthook: 每次服务器渲染路由时以及渲染之前:路由挂钩。在将Nuxt上下文序列化到窗口中之前调用。_uNUXT_uux,用于添加一些可以在客户端获取的数据 现在,我的自定义插件定义了文档中所述的一些钩子,但并不是所有钩子都被正确调用: module.exports = function() { this.nuxt.hook('render:ro

我想在中间件运行后从ssr服务器传递一些额外的数据,并在客户端中间件上使用这些数据。与nuxt对vuex所做的有点类似

render:context
hook:

每次服务器渲染路由时以及渲染之前:路由挂钩。在将Nuxt上下文序列化到窗口中之前调用。_uNUXT_uux,用于添加一些可以在客户端获取的数据

现在,我的自定义插件定义了文档中所述的一些钩子,但并不是所有钩子都被正确调用:

module.exports = function() {
  this.nuxt.hook('render:route', (url, result, context) => {
    console.log('This one is called on every server side rendering')
  }

  this.nuxt.hook('renderer', renderer => {
    console.log('This is never called')
  }

  this.nuxt.hook('render:context', context => {
    console.log('This is only called once, when it starts loading the module')
  }
}

我做错了什么?如何将自定义ssr数据传递到客户端渲染器?

好的,刚刚找到了将自定义数据从(ssr)服务器传递到客户端的核心问题的解决方案:

创建插件:
plugins/my plugin.js

export default ({ beforeNuxtRender, nuxtState }) => {
  if (process.server) {
    beforeNuxtRender(({ nuxtState }) => {
      nuxtState.myCustomData = true
    })
  } else {
    console.log('My cystom data on the client side:', nuxtState.myCustomData)
  }
}
然后在
numxt.config.js
中注册插件:

module.exports = {
  plugins: ['~/plugins/my-plugin']
}

文档。

好的,刚刚找到了将自定义数据从(ssr)服务器传递到客户端的核心问题的解决方案:

创建插件:
plugins/my plugin.js

export default ({ beforeNuxtRender, nuxtState }) => {
  if (process.server) {
    beforeNuxtRender(({ nuxtState }) => {
      nuxtState.myCustomData = true
    })
  } else {
    console.log('My cystom data on the client side:', nuxtState.myCustomData)
  }
}
然后在
numxt.config.js
中注册插件:

module.exports = {
  plugins: ['~/plugins/my-plugin']
}

Docs.

您使用哪个nuxt版本?我使用1.4.0只是为了测试,您可以用
nuxt edge
在一个简单的示例中尝试一下吗?我尝试过,但我一直得到与此处相同的错误:您使用哪个nuxt版本?我使用1.4.0只是为了测试,您能用
nuxt edge
在一个简单的示例中尝试一下吗?我尝试过,但一直得到与此处状态相同的错误: