美文网首页
基础组件的自动化全局注册

基础组件的自动化全局注册

作者: Minxgo | 来源:发表于2019-12-25 15:16 被阅读0次
原文链接
// Globally register all base components for convenience, because they
// will be used very frequently. Components are registered using the
// PascalCased version of their file name.

import Vue from 'vue'

// https://webpack.js.org/guides/dependency-management/#require-context
// 全局导入组件文件
const requireComponent = require.context(
  // Look for files in the current directory
  // 根据路径查找当前目录中的文件
  '.',
  // Do not look in subdirectories
  // 是否查询其子目录
  false,
  // Only include "_base-" prefixed .vue files
  // 匹配组件文件名的正式表达式,示例:_base-button.vue
  /_base-[\w-]+\.vue$/
)

// For each matching file name...
// 循环所有匹配的文件名
requireComponent.keys().forEach((fileName) => {
  // Get the component config
  // 获取组件配置
  const componentConfig = requireComponent(fileName)
  // Get the PascalCase version of the component name
  // 获取 PascalCase 形式的组件名,ex: './_base-button.vue'
  // 这一段可根据实际组件名进行修改
  const componentName = fileName
    // Remove the "./_" from the beginning
    // 去除名称中的 './_'
    .replace(/^\.\/_/, '')
    // Remove the file extension from the end
    // 去除扩展名
    .replace(/\.\w+$/, '')
    // Split up kebabs
    // 以 '-' 分割,ex:['base', 'button']
    .split('-')
    // Upper case
    // 首字母大写
    .map((kebab) => kebab.charAt(0).toUpperCase() + kebab.slice(1))
    // Concatenated
    // 最后拼接好,ex:BaseButton
    .join('')

  // Globally register the component
  // 全局注册组件
  Vue.component(componentName, componentConfig.default || componentConfig)
})

其他:

  • 这段代码须放在 new Vue() 之前,推荐放在 man.js 文件中
  • 使用:在某页面中直接 <base-button /> 即可

相关文章

  • 深入了解组件

    组件注册   组件注册分为全局注册和局部注册  全局注册   局部注册   基础组件的自动化全局注册可能你的许多组...

  • 基础组件的自动化全局注册

    基础组件的自动化全局注册

  • 基础组件的自动化全局注册

    可能你的许多组件只是包裹了一个输入框或按钮之类的元素,是相对通用的。我们有时候会把它们称为基础组件,它们会在各个组...

  • 基础组件的自动化全局注册

    基础组件的自动化全局注册 可能你的许多组件只是包裹了一个输入框或按钮之类的元素,是相对通用的。我们有时候会把它们称...

  • 基础组件的自动化全局注册

    原文链接 其他: 这段代码须放在 new Vue() 之前,推荐放在 man.js 文件中 使用:在某页面中直接 ...

  • vue组件

    关于vue组件的总结 注册组件 vue中组件的注册分为两种: 全局注册 局部注册 全局注册 全局注册的组件在任何v...

  • Vue 之 组件

    组件注册 组件注册分为两种: 全局注册 和 局部注册 全局注册:全局注册的行为必须在根 Vue 实例 ...

  • vue基础组件的自动化全局注册

    在使用vue构建项目的过程中,我们会设置一些通用组件,包含一些基础的功能,会被我们在项目中频繁调用。此时若在每一个...

  • 注册组件的语法糖写法

    1.全局注册组件语法糖 全局组件不用语法糖的构建过程:生成组件构造器 注册组件 使用组件 全局注册组件的语法糖,省...

  • 组件

    注册组件 注册组件分为全局注册和局部注册。 全局注册 step1.用Vue.component()方法定义一个组件...

网友评论

      本文标题:基础组件的自动化全局注册

      本文链接:https://www.haomeiwen.com/subject/tzycoctx.html