How to create a custom global component in Vue?
This article mainly introduces the usage of vue custom global components (custom plug-ins). Now I share it with you and give you a reference.
Sometimes when we are doing development, we want to write a plug-in ourselves and then use our own plug-in, which gives us a strong sense of accomplishment. When bloggers recently studied element-ui and axios, they found that they are custom components, but the only difference is that when using element-ui, they use the Vue.use() statement, while when using axios , without Vue.use(), just import can be imported. It feels very magical. I found out carefully that the difference between them is because there is no install method written in axios, while element-ui has written this method. Below Just use this install to write your own plug-in.
First, before writing this plug-in, generate a directory to store the plug-in. As a blogger, I put it in the loading directory of a component:
In this directory, according to the blogger's habit, I write an index.js file and a component. Loading.vue, index.js is written about the install method of loading.vue. The code is as follows:
import LoadingComponent from './Loading.vue' const Loading={ install:function (Vue) { Vue.component('Loading',LoadingComponent) } } export default Loading
The install method is represented in main.js. If the Vue.use() method is used, the install method will be called by default. Components are also registered in the install method. 'Loading' here refers to the component name used by the external App.vue, and LoadingComponent refers to the Loading.vue quoted above. Then export via export default Loading.
Then the Loading.vue code is as follows:
<template> <p class="loading-box"> Loading... </p> </template> <script></script>
After the Loading.vue code is written, it is imported in the default main.js file and imported by using the Vue.use() method The index.js you just wrote:
import Vue from 'vue' import App from './App.vue' import Loading from './components/loading' Vue.use(Loading) new Vue({ el: '#app', render: h => h(App) })
Then just use the template in the App.vue method:
<template> <p id="app"> <Loading></Loading> </p> </template>
You can also write your own in the loading.vue file just now Some code, such as writing a calendar plug-in, button plug-in, etc. For example, this one:
<template> <p class="loader"> <p class="loader-inner ball-spin-fade-loader"> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> </p> </p> </template> <style scoped> .loader{ width:80px; height: 80px; margin:50px auto; } @keyframes ball-spin-fade-loader { 50% { opacity: 0.3; -webkit-transform: scale(0.4); transform: scale(0.4); } 100% { opacity: 1; -webkit-transform: scale(1); transform: scale(1); } } .ball-spin-fade-loader { position: relative; } .ball-spin-fade-loader > p:nth-child(1) { top: 25px; left: 0; -webkit-animation: ball-spin-fade-loader 1s 0s infinite linear; animation: ball-spin-fade-loader 1s 0s infinite linear; } .ball-spin-fade-loader > p:nth-child(2) { top: 17.04545px; left: 17.04545px; -webkit-animation: ball-spin-fade-loader 1s 0.12s infinite linear; animation: ball-spin-fade-loader 1s 0.12s infinite linear; } .ball-spin-fade-loader > p:nth-child(3) { top: 0; left: 25px; -webkit-animation: ball-spin-fade-loader 1s 0.24s infinite linear; animation: ball-spin-fade-loader 1s 0.24s infinite linear; } .ball-spin-fade-loader > p:nth-child(4) { top: -17.04545px; left: 17.04545px; -webkit-animation: ball-spin-fade-loader 1s 0.36s infinite linear; animation: ball-spin-fade-loader 1s 0.36s infinite linear; } .ball-spin-fade-loader > p:nth-child(5) { top: -25px; left: 0; -webkit-animation: ball-spin-fade-loader 1s 0.48s infinite linear; animation: ball-spin-fade-loader 1s 0.48s infinite linear; } .ball-spin-fade-loader > p:nth-child(6) { top: -17.04545px; left: -17.04545px; -webkit-animation: ball-spin-fade-loader 1s 0.6s infinite linear; animation: ball-spin-fade-loader 1s 0.6s infinite linear; } .ball-spin-fade-loader > p:nth-child(7) { top: 0; left: -25px; -webkit-animation: ball-spin-fade-loader 1s 0.72s infinite linear; animation: ball-spin-fade-loader 1s 0.72s infinite linear; } .ball-spin-fade-loader > p:nth-child(8) { top: 17.04545px; left: -17.04545px; -webkit-animation: ball-spin-fade-loader 1s 0.84s infinite linear; animation: ball-spin-fade-loader 1s 0.84s infinite linear; } .ball-spin-fade-loader > p { background-color: #399; width: 15px; height: 15px; border-radius: 100%; margin: 2px; -webkit-animation-fill-mode: both; animation-fill-mode: both; position: absolute; } </style>
The effect is a rolling circle:
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to compile, package and view the index file in vue
How to use Jade template in vue
Passing templates to components in Angular
The above is the detailed content of How to create a custom global component in Vue?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.
