Home  >  Article  >  Web Front-end  >  How to reference the component library in vue project

How to reference the component library in vue project

PHPz
PHPzOriginal
2023-05-20 09:55:38668browse

Vue is a very popular front-end framework in today's Internet development. Its component-based development method can effectively improve the maintainability and development efficiency of the project. In Vue project development, we often use some third-party component libraries to quickly build various interactions and page elements. This article will introduce how to reference component libraries in Vue projects.

1. Commonly used component libraries

There are many component libraries used in Vue projects, the more common ones are the following:

  1. Element-UI: A UI component library based on Vue 2.0, including a large number of commonly used components, such as buttons, tables, pop-up boxes, date pickers, etc.
  2. Ant Design of Vue: A Vue version of Ant Design, which provides a set of beautiful component libraries.
  3. Vuetify: A Vue component library based on Material Design, providing a series of beautiful and rich components.
  4. Vue Material: Another Vue component library based on Material Design, providing a complete, rigorous, and professional UI component solution.

2. How to reference the component library

Before you start using the component library, you need to install the corresponding library file and introduce it into the project. Taking the Element-UI component library as an example, we will introduce how to introduce the component library into the Vue project.

  1. Install Element-UI

You can execute the following command in the terminal command line to install:

npm i element-ui -S
  1. Introduce Element-UI

In the Vue project, if you want to use Element-UI components in the page, you need to introduce Element-Ui in the main.js file:

import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

First introduce Element through the import statement -UI and then inject it into the Vue instance via the Vue.use() method. It should be noted that the CSS style file of element-ui also needs to be introduced, otherwise it will not be displayed properly on the page.

If you only want to use some components of Element-UI, you can create a new element-ui.js file in the components folder, introduce the required components into it, and then add the .vue file that needs to use the component. Just import it into .

For example, to use the el-dialog component of Element-ui, create a new element-ui.js file:

import Vue from 'vue'
import { Dialog } from 'element-ui'

Vue.use(Dialog)

Then introduce it in the .vue file that needs to use the el-dialog component:

<template>
  <el-dialog></el-dialog>
</template>

<script>
  import 'element-ui/lib/theme-chalk/dialog.css';
  import Dialog from "@/components/element-ui";
  
  export default {
    components: {
      'el-dialog': Dialog.Dialog
    }
  }
</script>

Here you need to introduce the dialog.css style file of element-ui, and introduce the Dialog component through the import statement and register it in the current Vue instance. In the .components option, map the el-dialog component to Dialog.Dialog, and you can use the el-dialog component in the page.

3. Summary

Using third-party component libraries in Vue projects is a good way to improve development efficiency and enhance the beauty of the website. This article introduces how to introduce the Element-UI component library. Readers can choose other component libraries according to their own needs and introduce and use them in a similar way.

The above is the detailed content of How to reference the component library in vue project. 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