npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

uni-global-component-inject

v1.2.0

Published

uniapp(uni-app) 全局组件注入

Readme

简介

一个为 uniapp(uni-app) 项目注入全局组件的工具,它支持 Vue2(基于 Webpack)和 Vue3(基于 Vite)项目,让你的 App 也能够拥有全局组件。

特性

  • 支持 UniApp Vue 2(Webpack)和 Vue 3(Vite)项目。
  • 通过 pages.json 配置全局组件注入。
  • 自动合并组件的脚本、模板和样式。
  • 提供灵活的排除机制,允许特定页面跳过注入。

配置过程 切换到VUE3配置

第一步 安装依赖

npm install uni-global-component-inject -D

第二步 配置 Loader

在 vue.config.js 中配置 Loader:

const path = require('node:path')
const { defineConfig } = require('@vue/cli-service')

// src 的工程目录下的 vue.config.js
const projectRoot = path.resolve(__dirname, '../')
const uniGlobalComponentInject = path.resolve(projectRoot, 'node_modules/uni-global-component-inject/loader.js')

// 使用 configureWebpack 配置 loader
module.exports = defineConfig({
  configureWebpack: {
    module: {
      rules: [
        {
          test: /\.vue$/,
          loader: uniGlobalComponentInject,
          options: {
            platforms: ['app-plus']
          }
        }
      ]
    }
  }
})

/* 或者 使用 chainWebpack 配置 loader */

// module.exports = defineConfig({
//   chainWebpack: (config) => {
//     config.module
//       .rule('vue')
//       .use('uni-global-component-inject')
//       .loader(uniGlobalComponentInject)
//       .tap((options) => ({
//         ...options,
//         platforms: ['app-plus'],
//       }));
//   },
// });

第三步 创建全局组件 CustomModal

uni-app 的全局组件了解一下 easycom配置 & easycom规范

简单说就是在 components 目录下创建 components/CustomModal/CustomModal.vue 文件,然后在任意页面写下 <CustomModal /> 组件标签,该页面就自动导入该组件进行使用了。

第四步 配置 pages.json 文件添加 injectLoader

"injectLoader": {
  // 将 <CustomModal /> 作为全局组件注入到所有页面
  "injectTags": [
    {
      // 必填,指的是 components 目录下的组件名称
      "name": "CustomModal"
    }
  ],
  "rootEle": "view"
}

第五步 重新编译项目

重新编译项目,在每个页面都会自动注入 <CustomModal /> 组件了。

案例运行

配置选项

vue.config.js 配置选项

options: {
  // 必填:指定的平台才生效。例如 ['app-plus','mp-weixin']
  platforms: ['app-plus'],
  // 可选:指定自定义的 pages.json 文件路径(默认值为 'src/pages.json')
  pagesPath: path.resolve(__dirname, './src/pages.json')
}

pages.json 配置选项

"injectLoader": {
  // 以标签的形式插入到每一个页面
  "injectTags": [
    {
      "name": "CustomModal",
      // 自定义注入的标签的 参数/回调函数
      "element": "<CustomModal :title=\"title\" @close=\"closeHandle\" />",
      // 排除哪些页面不需要注入该 CustomModal 全局组件
      "excludes": ["pages/about"]
    }
  ],
  // 排除哪些页面不需要注入任何的全局组件
  "excludeTagsPaths": ["pages/setting"],
  // 必填,填写你的项目所有页面的根元素是: view / div
  "rootEle": "view"
}

常见问题

  1. 如果让特定页面不注入任何的全局组件标签?
"injectLoader": {
  "injectTags": [...],
  // 排除哪些页面不需要注入任何的全局组件
  "excludeTagsPaths": ["pages/about"]
}
  1. 如何让特点页面不注入某个全局组件?
"injectLoader": {
  "injectTags": [
    {
      "name": "CustomModal",
      // 排除哪些页面不需要注入该 CustomModal 全局组件
      "excludes": ["pages/about"]
    }
  ]
}
  1. 如何自定义全局组件的注入标签的 参数/回调
"injectLoader": {
  "injectTags": [
    {
      "name": "CustomModal",
      // 自定义注入的标签的 参数/回调函数
      "element": "<CustomModal :title=\"title\" @confirm=\"confirmHandle\" />",
    }
  ]
}
  1. 指定特点的平台下使用全局组件注入?
options: {
  // 必填,指定的平台才生效。例如 ['app-plus','mp-weixin']
  platforms: ['app-plus']
}

许可证

MIT License