vue scroll loading

  •  

  • In Vue, if a component is a button, you can directly call the input () method to put the button of the component into v-ui. However, in v-ui, a component may have more than one button, and these buttons need to be loaded from the browser into the DOM tree. What should I do if I need to get component data from the DOM tree? Very simple, add a handler method in the DOM tree of the component, when the handler method is executed, return an input () method to update the DOM tree of the component. In v-ui, there is a good request-modifying method to achieve this operation. An onload () method is implemented in v-ui by request-modifying, which will find the next button that needs to be updated in the current component. If the current button does not exist, then the onload () method will return onload (). There is a problem here, when we pass the onload () method to the handler, how can we ensure that it is unique in the DOM tree?

    • 1. First, let's take a look at how to get component data in v-ui

      First of all, in v-ui, the component data is obtained by putting the button of the component into the DOM tree of v-ui. Then we can get component data by calling the handler method in v-ui. However, obtaining data from the DOM tree requires two steps: 1. Find the button to be updated in the DOM tree; 2. Update the button. From the above two steps, we can see that in v-ui, the button is first searched from the DOM tree, and then the button is updated. When we call the onload() method, we will first look for the next button that needs to be updated in the DOM tree. When you find the button that needs to be updated, update it. This process is sequential, if the order is reversed, it will cause a button to change. In v-ui, there is also a nice way to avoid this problem. This method is to use the mapping relationship between the DOM tree and the handler to solve this problem. Specifically, we can obtain component data in the following ways: 3. After updating the button, use the handler method to find the next button that needs to be updated again

    • 2. After obtaining the component data, how to operate the handler

      A handler method is a function that passes the action to be performed to its constructor. This constructor is a method. When we use this method, we need to initialize it once, that is, set its parameters. This parameter will be passed to the constructor. If we initialize it in the constructor, then we can perform scrolling and loading operations. Using this method, you can get the current DOM node of the component. If the current node is empty, it means that the component has been loaded into the DOM tree. Another advantage of using this method is that it can hide elements, because if our component is a button, then there is only one element in the component, but in v-ui we can hide multiple elements, this is because the handler method needs to be Initialize the operation. In addition, when we get the element from the DOM tree, if it does not exist, then this method will fail. The above two methods are specific methods to realize scrolling loading in v-ui. However, there is another feature of v-ui, that is, when the component gets the data from the DOM tree, it will update immediately. This is why we can't keep component data in v-ui all the time. How to implement scrolling loading of components? Very simple, we can achieve it by introducing v-ui framework. The Vue framework not only supports scrolling loading operations, but also supports updating component data. When we use the Vue framework, it is very convenient to get component data from the DOM tree into v-ui to achieve scrolling loading. The specific method of use is: Next, we will explain this method in detail through a practical example.

    • 3. How to set the life cycle of component data

      We can set the life cycle in the DOM of the component and pass it to the handler through request-modifying. If the data of the component has expired, then onload() will be returned. In v-ui, the life cycle of component data can be realized by setting the template attribute. We can use the template attribute to set the life cycle of the component, which will add a template attribute to the data attribute, which will represent the life cycle of the component data with a value. For example: when we need to add a button to v-ui, first we need to put the button into data. Then, when we set the data attribute to template, we can do this: At this point, the data of this component will be loaded into v-ui from the DOM tree. For other types of component data, the life cycle can be set in a similar way.

    • 4. Update component data

      In v-ui, there are many ways to update component data, such as onload (), request-modifying, dialog-by-value (), handler (), etc. We know that the onload() method gets component data from the browser. But what if v-ui doesn't support this method? So how to use onload () to update component data in v-ui? In fact, it is very simple, just call the onload () method directly, but it should be noted here that v-ui only supports one update, and cannot support multiple data updates like webpack. At this time, someone may ask, why not support multiple data? That's because the onload() method in v-ui has two modes, one update and multiple updates. If we only update the data once, there will be problems. Because if you only update the data of one component, and the data of other components has not been modified, then when you update it again, it will compare the data of the current component with the previous one, if the previous data has changed , then this change will be notified to other components.

    • V. Summary

      In v-ui, the function of the handler is to obtain the data of the component according to the properties of the component, and the onload () method is to find the next button that needs to be updated from the DOM tree. However, if we want to ensure that the data obtained by the handler is unique, then we need to add a keyword to the onload () method to ensure that there is only one, without causing the data of the component to be changed. This keyword is direction. In v-ui, direction is a keyword that compares the data obtained by the handler with the current component. If there is a data difference, this difference will be passed to the onload () method, if there is no difference, this difference will be passed to the onload () method, and then the DOM tree of the component will be updated.

The following are several commonly used Vue scroll loading codes:

1. 使用vue-infinite-loading插件实现滚动加载
```
<template>
<div>
<infinite-loading @infinite="infiniteHandler"></infinite-loading>
<ul>
<li v-for="(item, index) in list" :key="index">{ { item }}</li>
</ul>
</div>
</template>
<script>
import InfiniteLoading from 'vue-infinite-loading';
export default { components: { InfiniteLoading, }, data() { return { list: [], page: 1, limit: 10, }; }, methods:{ axios.get('/api/list', { // Send request to get datainfiniteHandler($state) {














params: {
page: this.page,
limit: this.limit,
},
}).then((res) => {
if (res.data.length) {
this.list = this.list.concat(res.data);
this.page++;
$state.loaded();
} else {
$state.complete();
}
}).catch(() => {
$state.error();
});
},
},
};
</script>
```
2. 使用Intersection Observer实现滚动加载
```
<template>
<div>
<ul>
<li v-for="(item, index) in list" :key="index">{ { item }}</li>
</ul>
<div ref="scrollTarget" style="height: 1px;"></div>
</div>
</template>
<script>
export default { data() { return { list: [], page: 1, limit: 10, observer: null, }; }, mounted() { // create Intersection Observer instance this.observer = new IntersectionObserver(this.handleObserver, { root: null, rootMargin: '0px', threshold: 1.0, }); // Observe the scroll target this.observer.observe(this.$refs.scrollTarget); }, methods: { handleObserver(entries) { const entry = entries[0]; if (entry.isIntersecting) { // Send a request to get data axios.get('/api/list', { params: {

























page: this.page,
limit: this.limit,
},
}).then((res) => {
if (res.data.length) {
this.list = this.list.concat(res.data);
this.page++;
} else {
// 取消监听
this.observer.unobserve(this.$refs.scrollTarget);
}
});
}
},
},
};
</script>
```
3. 使用scroll事件实现滚动加载
```
<template>
<div ref="scrollContainer" style="height: 300px; overflow-y: scroll;">
<ul>
<li v-for="(item, index) in list" :key="index">{ { item }}</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
list: [],
page: 1,
limit: 10,
};
},
mounted() {
// 监听scroll事件
this.$refs.scrollContainer.addEventListener('scroll', this.handleScroll);
},
methods: {
handleScroll() {
const container = this.$refs.scrollContainer;
if (container.scrollTop + container.clientHeight >= container.scrollHeight) {
// 发送请求获取数据
axios.get('/api/list', {
params: {
page: this.page,
limit: this.limit,
},
}).then((res) => {
if (res.data.length) {
this.list = this.list.concat(res.data);
this.page++;
}
});
}
},
},
beforeDestroy() {
// 取消监听
this.$refs.scrollContainer.removeEventListener('scroll', this.handleScroll);
},
};
</script>
```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/131004451