使用自定义构建的 Nuxt 模块将 nuxt.config.js 配置注入全局注册组件

如何解决使用自定义构建的 Nuxt 模块将 nuxt.config.js 配置注入全局注册组件

所以这个问题可能很复杂,但我会尽量解释清楚。

我目前正在创建 Nuxt 模块。如果您熟悉创建它们,您就会知道可以在您的 nuxt.config.js 中对其进行配置。我在这里做的:

components: {
    modules: ['carousel','filter','post-loop','gform'],gform: {
         title: true
    }
},

我有一系列要全局注册的模块。我已经使用以下配置这样做了:

(index.js)

const path = require('path');

export default function ClassName(moduleOptions) {

    const options = Object.assign({},this.options.components,moduleOptions);
    const availableModules = ['carousel','gform','post-loop'];
    const unkNownModules = options.modules.filter(mod => !availableModules.includes(mod));

    if(!options.modules) {
        throw new Error('ERROR: Please check the ReadMe,you need to include a modules array specifying what modules you want to enable');
    }

    if(unkNownModules.length > 0) {
        throw new Error(`ERROR: The following modules do not exist in the @blueelevation/components module: ${unkNownModules}`);
    }

    this.addplugin({
        src: path.resolve(__dirname,'./plugin/plugin.js'),options: options
    })

    const cssFilesToLoad = ['carousel.css'];

    cssFilesToLoad.forEach(file => this.options.css.push(path.resolve(__dirname,`dist/css/${file}`)));

    this.addplugin({
        src: path.resolve(__dirname,'./plugin/eventbus.js'),})
}

需要关注的重要部分是 plugin.js,我在 Nuxt 实例中全局注册组件:

import Vue from 'vue';

/**
 * If the module array contains carousel,we register it as a global component.
 */
<% if(options.modules.includes('carousel')) { %>
    import Carousel from '@blueelevation/components/components/Carousel/Carousel.vue';
    Vue.component('Carousel',Carousel);
<% } %>

/**
 * If the module array contains post-loop,we register it as a global component.
 */
<% if(options.modules.includes('post-loop')) { %>
    import PostLoop from '@blueelevation/components/components/PostLoop/PostLoop.vue';
    Vue.component('pwa-post-loop',PostLoop);
<% } %>

/**
 * If the module array contains filter,we register it as a global component.
 */
<% if(options.modules.includes('filter')) { %>
    import Filter from '@blueelevation/components/components/Filter/Filter.vue';
    import Filterheading from '@blueelevation/components/components/Filter/Filterheading.vue';
    Vue.component('pwa-filter',Filter);
    Vue.component('pwa-filter-heading',Filterheading);
<% } %>

/**
 * If the module array contains gform,we register it as a global component.
 */
<% if(options.modules.includes('gform')) { %>
    import GForm from '@blueelevation/components/components/GForm/GForm.vue';
    Vue.component('pwa-gform',GForm);
<% } %>

如您所见,我全局注册了组件,以防它在数组中指定。我目前遇到的问题是我的 gform.title 中有一个 nuxt.config.js 属性。如何将此 Boolean 传递给全局注册的 GForm 组件?所以我可以有条件地呈现标题,以防它是 true?

解决方法

我回答了我自己的问题。如果有人想知道如何做到这一点,您可以通过在 appBar: PreferredSize( preferredSize: Size.fromHeight(150.0),child: AppBar( title: Text('appbar'),), 中执行以下操作将某些内容注入到您的组件中:

plugin.js

注意 gform 组件全局注册下面的 import Vue from 'vue'; import Carousel from '@blueelevation/components/components/Carousel/Carousel.vue'; import PostLoop from '@blueelevation/components/components/PostLoop/PostLoop.vue'; import Filter from '@blueelevation/components/components/Filter/Filter.vue'; import FilterHeading from '@blueelevation/components/components/Filter/FilterHeading.vue'; import GForm from '@blueelevation/components/components/GForm/GForm.vue'; export default(context,inject) => { /** * If the module array contains carousel,we register it as a global component. */ <% if(options.modules.includes('carousel')) { %> Vue.component('Carousel',Carousel); <% } %> /** * If the module array contains post-loop,we register it as a global component. */ <% if(options.modules.includes('post-loop')) { %> Vue.component('pwa-post-loop',PostLoop); <% } %> /** * If the module array contains filter,we register it as a global component. */ <% if(options.modules.includes('filter')) { %> Vue.component('pwa-filter',Filter); Vue.component('pwa-filter-heading',FilterHeading); <% } %> /** * If the module array contains gform,we register it as a global component. */ <% if(options.modules.includes('gform')) { %> Vue.component('pwa-gform',GForm); inject('GForm',{config: <%= serialize(options.gform) %> }); <% } %> } 函数。我用序列化对象(inject() 中的 GForm)注入 options.gform。之后,此 nuxt.config.js 配置将通过以下方式在组件中可用:

GForm

希望这对未来的人有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?