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

How to create a custom global component in Vue?

亚连
亚连Original
2018-06-08 18:06:182308browse

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:


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:

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:


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!

Statement:
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