【问题标题】:vue + nuxt js - how to access context in a plugin for server side?vue + nuxt js - 如何在服务器端的插件中访问上下文?
【发布时间】:2020-04-17 09:25:45
【问题描述】:

我想知道是否有办法在插件中访问上下文变量?除了“进程”对象之外,还存在哪些其他变量/对象?不幸的是,我在nuxt side 处找不到关于“进程”对象的清晰描述或参考

即插件/helper.js

const myHelper = {
  helpFunction(arg) {
    if (process.server) {
      ...
    }
    return ...;
  },

【问题讨论】:

    标签: vue.js plugins nuxt.js


    【解决方案1】:

    您应该在插件中导出一个函数以使用context,如docs 中所述:

    export default (context, inject) => {
      context.app.helpFunction= (arg) => {
          if (process.server) {
             ...
          }
          return ...;
      }
    }
    

    现在您可以在任何组件中使用您的辅助函数,如下所示:

    this.$helpFunction(someArgs)...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-17
      • 2021-06-27
      • 2019-04-17
      • 1970-01-01
      • 2022-11-02
      • 2019-08-25
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多