Skip to content

Automatic Global Registration of Base Components #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KasraK2K opened this issue Sep 29, 2020 · 1 comment
Closed

Automatic Global Registration of Base Components #565

KasraK2K opened this issue Sep 29, 2020 · 1 comment

Comments

@KasraK2K
Copy link

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

@skirtles-code
Copy link
Contributor

Related: #43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants