Skip to content

Automatic Global Registration of Base Components #565

Closed
@KasraK2K

Description

@KasraK2K

I can't find any document about Automatic Global Registration of Base Components in Vue 3.x Component Registration documents.

i solved this problem using Vue 2.x document and made a little change:

// automatic register global component
import upperFirst from "lodash/upperFirst";
import camelCase from "lodash/camelCase";

const createdApp = createApp(App);

const requireComponent = require.context(
  // The relative path of the components folder
  "./components",
  // Whether or not to look in subfolders
  false,
  // The regular expression used to match base component filenames
  /Base[A-Z]\w \.(vue|js)$/,
);

requireComponent.keys().forEach(fileName => {
  // Get component config
  const componentConfig = requireComponent(fileName);
  // Get PascalCase name of component
  const componentName = upperFirst(
    camelCase(
      // Gets the file name regardless of folder depth
      fileName
        .split("/")
        .pop()
        .replace(/\.\w $/, ""),
    ),
  );
  // Register component globally
  createdApp.component(
    componentName,
    // Look for the component options on `.default`, which will
    // exist if the component was exported with `export default`,
    // otherwise fall back to module's root.
    componentConfig.default || componentConfig,
  );
});

Add it to Vue 3.x document, please

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions