简体   繁体   English

将数据从nuxt模块传递到插件

[英]Pass data from nuxt module to plugin

I am trying to pass data that I get from my module options down to a plugin. 我试图将我从模块选项中获得的数据传递给插件。 So let's say this is my module: 因此,这是我的模块:

module.exports = function (moduleOptions) {
  const options = {
    ...this.options.moduleName,
    ...moduleOptions
  }


  this.addPlugin({
    src: resolve(__dirname, 'plugin.js'),
    options
  })
}

and this is my plugin 这是我的插件

import { createStore } from 'lib';

export default async ({ store, app }) => {
  const settings = {
    axios: app.$axios,
    models: <% options.models %>
  }

  settings.axios = app.$axios;

  createStore(settings).install()(store)
};

and this is my config 这是我的配置

const { resolve } = require('path')

module.exports = {
  rootDir: resolve(__dirname, '..'),
  buildDir: resolve(__dirname, '.nuxt'),
  srcDir: __dirname,
  render: {
    resourceHints: false
  },
  modules: [
    'moduleName'
  ],
  moduleName: {
    { models: require(resolve(__dirname, '../example/models')) }
  }
}

it throws 它抛出

axios: app.$axios,
       7 |     models:
    >  8 |   }

where models is just empty, nothing behind it. 模型只是空的,后面什么也没有。 No null, no undefined. 没有null,没有未定义。

But if I do <% console.log(options.models) %> it will show the models that I've loaded. 但是,如果我执行<% console.log(options.models) %> ,它将显示我已加载的模型。 Btw models is just an array of classes. Btw模型只是一组类。

These models must be configurable, so how do I pass these data from my nuxt.config.js via a module to my plugin? 这些模型必须是可配置的,那么如何通过模块将这些数据从nuxt.config.js传递到插件?

Hope somebody knows :) 希望有人知道:)

我使用插件中的require而不是我的配置来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM