Example of automatic import method of vue3.0 common components

Example of automatic import method of vue3.0 common components

1. Prerequisites

We use the require.context method to import. If we use it in a project created by vite, an error "require not found" will be reported, so we must use webpack to create a project. Or someone can tell me how Vite can solve this problem.

II. Rules

The registration rule I use is to search all directories and subdirectories under the src/components/ path, search for files named "index.vue", and use the name of the parent directory as the component name for registration. The structure is as follows:

Only index.vue is registered, components with other names are not registered.

3. Registration

Since vue3.0 does not have import "Vue" from vue, we need to use app to register, so we can only do it in main.js

Entry file registration

// src/main.js

import { createApp } from 'vue'
const app = createApp(App)
//Dynamically register public components const requireComponent = require.context(
    // The relative path of its component directory is '@/components',
    // Whether to query its subdirectory true,
    // Regular expression matching the base component file name /index.vue$/
)
const jieguo = requireComponent.keys().filter((item:any)=> true)
jieguo.forEach((item:any)=>{
    const componentConfig = requireComponent(item)
    const name = item.split("/")[1]
    app.component(name,componentConfig.default || componentConfig)
})
// Registration ends app.mount('#app')

When we create, delete, or rename a public component, no registration is required. Restart the project and take a sip of water.

Summarize

This is the end of this article about automatic import of vue3.0 public components. For more relevant vue3.0 public component import content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue's method of introducing all public components at one time in a concise and clear manner

<<:  Using Docker to create static website applications (multiple ways)

>>:  Instructions for using JSON operation functions in Mysql5.7

Recommend

Tutorial on downloading, installing, configuring and using MySQL under Windows

Overview of MySQL MySQL is a relational database ...

Ideas and methods for incremental backup of MySQL database

To perform incremental backup of the MySQL databa...

Detailed usage of docker-maven-plugin

Table of contents Docker-Maven-Plugin Maven plugi...

Write a React-like framework from scratch

Recently I saw the article Build your own React o...

Windows platform configuration 5.7 version + MySQL database service

Includes the process of initializing the root use...

Skin change solution based on Vue combined with ElementUI

Table of contents Written in front Solution 1: Us...

Introduction to the use of CSS3 filter attribute

1. Introduction When writing animation effects fo...

Hadoop 2.x vs 3.x 22-point comparison, Hadoop 3.x improvements over 2.x

Question Guide 1. How does Hadoop 3.x tolerate fa...

How to deploy Spring Boot using Docker

The development of Docker technology provides a m...

MySQL 5.6.24 (binary) automatic installation script under Linux

This article shares the mysql5.6.24 automatic ins...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

Detailed explanation of MySQL data rows and row overflow mechanism

1. What are the formats of lines? You can see you...

Vue custom component implements two-way binding

Scenario: The interaction methods between parent ...

Vuex modularization and namespaced example demonstration

1. Purpose: Make the code easier to maintain and ...

MySQL 8.0.14 installation and configuration method graphic tutorial

This article records the installation and configu...