Home >Web Front-end >Vue.js >Vue component practice: scrolling loading component development

Vue component practice: scrolling loading component development

WBOY
WBOYOriginal
2023-11-24 08:28:571290browse

Vue component practice: scrolling loading component development

Vue component practice: rolling loading component development

Introduction:
Scrolling loading is a common web page optimization technology that dynamically loads data when scrolling the page. It can improve the loading speed of web pages and reduce user waiting time. This article will introduce how to use the Vue framework to develop a scroll-loading component and provide specific code examples.

1. Project preparation:
Before starting development, we need to ensure that the Node.js and Vue development environments have been installed. You can check whether the installation is successful by running the following command:

node -v
npm -v
vue --version

2. Component development:

  1. Create project
    First, we need to create a new Vue project. Execute the following command in the command line:
vue create scroll-load-demo

Then follow the prompts to select the required configuration and wait for the project to be created.

  1. Create components
    In the created project, we can create a new folder in the src directory, named components, to store our component code.

In the components folder, create a new file ScrollLoad.vue. This file is the implementation of our rolling loading component.

ScrollLoad.vue code example:

<template>
  <div class="scroll-load" ref="scrollLoad">
    <slot></slot>
    <div v-if="loading" class="loading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false
    };
  },
  mounted() {
    const scrollLoad = this.$refs.scrollLoad;
    scrollLoad.addEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll() {
      const scrollLoad = this.$refs.scrollLoad;
      const scrollTop = scrollLoad.scrollTop;
      const offsetHeight = scrollLoad.offsetHeight;
      const scrollHeight = scrollLoad.scrollHeight;

      if (scrollHeight - scrollTop - offsetHeight < 100 && !this.loading) {
        this.loading = true;
        this.$emit('loadMore');
      }
    }
  }
};
</script>

<style scoped>
.scroll-load {
  height: 300px;
  overflow: auto;
  border: 1px solid #ccc;
}

.loading {
  text-align: center;
  padding: 10px;
  background: #f6f6f6;
}
</style>
  1. Using components
    In pages that use scroll loading components, we can use them in the following ways:
<template>
  <div>
    <h1>滚动加载示例</h1>
    <scroll-load @loadMore="loadMoreData">
      <ul>
        <li v-for="(item, index) in dataList" :key="index">{{ item }}</li>
      </ul>
    </scroll-load>
  </div>
</template>

<script>
import ScrollLoad from "./components/ScrollLoad.vue";

export default {
  components: {
    ScrollLoad
  },
  data() {
    return {
      dataList: ["数据1", "数据2", "数据3", "数据4", "数据5"],
      page: 1
    };
  },
  methods: {
    loadMoreData() {
      this.page++;
      // 模拟异步请求数据
      setTimeout(() => {
        this.dataList.push(...["数据" + this.page]);
        this.$refs.scrollLoad.loading = false;
      }, 1000);
    }
  }
};
</script>

In the above code example, we use the ScrollLoad component and trigger the loading of more data through the loadMore event. The specific loading logic can be adjusted according to actual needs.

3. Test run:
Enter the root directory of the project in the command line and execute the following command to run the project:

npm run serve

Then visit http://localhost in the browser :8080, you can see the scrolling loading sample page. As you scroll to the bottom, more data loads.

Summary:
This article introduces how to use the Vue framework to develop a scrolling loading component and provides specific code examples. By using scrolling loading components, you can improve the loading speed of web pages and optimize user experience. I hope the content of this article is helpful to you.

The above is the detailed content of Vue component practice: scrolling loading component development. 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