Table of Contents
VUE3 Basic Tutorial: Use the Vue.js plug-in to encapsulate the progress bar component
1. First introduction to the Vue.js progress bar component
2. Use Vue.js to implement the basic logic of the progress bar component
3. Use the Vue.js plug-in to encapsulate the progress bar component
4. Using the Vue.js progress bar component
5. Summary
Home Web Front-end Vue.js VUE3 basic tutorial: Use Vue.js plug-in to encapsulate the progress bar component

VUE3 basic tutorial: Use Vue.js plug-in to encapsulate the progress bar component

Jun 15, 2023 pm 10:33 PM
vue vuejs Progress bar component

In web development, the progress bar component is a common UI component used to display the progress of tasks or page loading. In Vue.js, based on its powerful componentization feature, we can easily encapsulate custom progress bar components and encapsulate them as plug-ins for reuse in various Vue.js applications. This article will demonstrate how to use the Vue.js plug-in to encapsulate the progress bar component through a complete Vue.js progress bar component example.

VUE3 Basic Tutorial: Use the Vue.js plug-in to encapsulate the progress bar component

1. First introduction to the Vue.js progress bar component

The Vue.js progress bar component is not just A simple UI component is an indispensable and important component in the background management system. Today we will use a demonstration of the Vue.js progress bar component to learn how to use the Vue.js plug-in to encapsulate the progress bar component.

First, we need to define a progress bar component, which includes 3 main components: top progress bar, bottom progress bar, and right status icon. The following is the HTML and CSS code snippet of this component:

<div class="progress">
  <div class="progress-top"></div>
  <div class="progress-bottom"></div>
  <i class="icon"></i>
</div>
Copy after login
.progress {
  position: relative;
  height: 14px;
  margin: 5px 0;
  border-radius: 6px;
  background-color: #f2f2f2;
}

.progress-top {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  border-radius: 6px;
  background-color: #5e72e4;
  transition: width .2s ease-in-out;
  z-index: 2;
}

.progress-bottom {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  border-radius: 6px;
  background-color: #fff;
  transition: width .2s ease-in-out;
  z-index: 1;
}

.icon {
  position: absolute;
  top: -5px;
  right: -10px;
  font-size: 18px;
  color: #5e72e4;
}
Copy after login

The corresponding function of this component is to display a progress bar and provides two parameters: value is used to adjust the width of the progress bar (0 ~ 100), color Used to adjust the color of the progress bar.

2. Use Vue.js to implement the basic logic of the progress bar component

Next, we use Vue.js to bind the dynamic data of the progress bar component and implement the basic logic of the component .

First, we define two variables in the data attribute of the Vue component: progressValue and progressColor. The former is used to bind the width of the progress bar, and the latter is used to bind the color of the progress bar.

export default {
  name: 'Progress',
  data() {
    return {
      progressValue: 0,
      progressColor: '#5e72e4'
    }
  }
  
  // ...组件的其他属性和方法
}
Copy after login

Next, in the template attribute of the component, we dynamically render the HTML code of the progress bar component based on the variables defined in the data attribute. Mainly by binding the value of progressValue, the width of the progress bar changes dynamically as the data is updated:

<template>
  <div class="progress">
    <div class="progress-top" :style="{ width: progressValue + '%' }"></div>
    <div class="progress-bottom"></div>
    <i class="icon" :class="['fa', 'fa-circle-o-notch', 'spin', 'text-'+progressColor]"></i>
  </div>
</template>
Copy after login

Finally, in the methods attribute of the component, we define an update method, in which Obtain the initial data of the current progress bar through Ajax asynchronous request, and call the updateProgress method to update the component data:

export default {
  name: 'Progress',
  data() {
    return {
      progressValue: 0,
      progressColor: '#5e72e4'
    }
  },
  methods: {
    update() {
      // 模拟Ajax异步请求
      // 返回progressValue范围在0~100之间的随机数
      const progressValue = Math.floor(Math.random() * 100);
      if(progressValue > 0 && progressValue < 100) {
        this.updateProgress(progressValue, this.progressColor);
      }
    },
    updateProgress(value, color) {
      this.progressValue = value;
      this.progressColor = color;
    }
  }
}
Copy after login

Now, our Vue.js progress bar component can already pass the update method and implement basic data binding fixed and dynamically updated.

3. Use the Vue.js plug-in to encapsulate the progress bar component

After the previous simple implementation, we have obtained a usable Vue.js progress bar component code. Next, we will encapsulate this code into a Vue.js plug-in.

First, we need to create a new VProgress plug-in in our Vue.js project, and define the global install method in the index.js file of the plug-in to register the Vue.js progress bar component. :

import VProgress from './vprogress.vue';

const install = function(Vue) {
  Vue.component(VProgress.name, VProgress);
}

export default install;
Copy after login

On this basis, we can also provide additional global configuration items and global registration methods for the plug-in. For example, we define a global configuration item for the plug-in:

import VProgress from './vprogress.vue';

const defaults = {
  color: '#5e72e4',
  delay: 1000
};

const install = function(Vue, options = {}) {
  const { color, delay } = Object.assign({}, options, defaults);

  Vue.prototype.$vprogress = {
    update(value) {
      VProgress.methods.updateProgress.call({ progressColor: color }, value, color);
    },
    delay
  };

  Vue.component(VProgress.name, VProgress);
}

export default install;
Copy after login

We add a global configuration item for the plug-in. The default color is the color of the progress bar, and delay is the interval between two updates. Each time we update the progress bar, we can update the value and color values ​​of the progress bar through global methods such as the Vue.prototype.$vprogress.update method, and we can control the update interval through Vue.prototype.$vprogress.delay time.

Finally, we package the above code and generate a usable VProgress plug-in instance for use in various Vue.js projects.

4. Using the Vue.js progress bar component

Now, we use the VProgress plug-in in the new Vue.js project. The method of use is very simple. You only need to register through the Vue.use() method in the entry file main.js of the Vue application:

import Vue from 'vue';
import VProgress from 'vprogress';

Vue.use(VProgress, {
  color: '#e74c3c',
  delay: 500
});
Copy after login

Here, we can also pass the Vue.use() method. Enter an options object to override the default VProgress plug-in configuration items.

Next, in the template, we only need to use the VProgress component directly and call the $vporgress.update method to update the value and color values ​​of the progress bar:

<template>
  <div class="app">
    <v-progress></v-progress>
  </div>
</template>

<script>
export default {
  name: 'App',
  mounted() {
    const { update, delay } = this.$vprogress;
    setInterval(() => {
      const value = Math.round(Math.random() * 100);
      update(value);
    }, delay)
  }
}
</script>
Copy after login

We use the setInterval method Automatically update the value of the progress bar. The interval is fixed by $vprogress.delay. Each time the progress bar updates data, the value and color parameters of the progress bar will be automatically updated according to the global configuration items of the plug-in and the local configuration of the project. The color and delay time of the progress bar are updated accordingly.

5. Summary

Through the above demonstration, we learned how to use the Vue.js plug-in to encapsulate the progress bar component and reuse it in the Vue.js application. The code examples in this article are intended to help readers who are new to Vue.js quickly understand the basic implementation methods of Vue.js plug-ins and the basic implementation logic of the progress bar component, laying the foundation for later development of custom components and plug-ins.

The above is the detailed content of VUE3 basic tutorial: Use Vue.js plug-in to encapsulate the progress bar component. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

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.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

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

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

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.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

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.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

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

How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

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.

How to jump a tag to vue How to jump a tag to vue Apr 08, 2025 am 09:24 AM

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

See all articles