Home > Web Front-end > JS Tutorial > How to create a custom global component in Vue?

How to create a custom global component in Vue?

亚连
Release: 2018-06-08 18:06:18
Original
2492 people have browsed it

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
Copy after login

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>
Copy after login

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 &#39;vue&#39;
import App from &#39;./App.vue&#39;
import Loading from &#39;./components/loading&#39;
Vue.use(Loading)
new Vue({
 el: &#39;#app&#39;,
 render: h => h(App)
})
Copy after login

Then just use the template in the App.vue method:

<template>
 <p id="app">
  <Loading></Loading>
 </p>
</template>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template