Home >Web Front-end >Vue.js >Optimization skills and practical experience of list rendering in Vue
Optimization skills and practical experience of list rendering in Vue
Preface
In the process of developing web applications using Vue, list rendering is a common requirement. However, when there is a large amount of data in the list, frequent DOM operations may cause page performance to degrade. In order to solve this problem, this article will introduce some list rendering optimization techniques in Vue, and provide practical experience and code examples.
1. Avoid using index as the key value
When using v-for loop to render a list in Vue, you need to provide a key value to uniquely identify each item. In some cases, developers may prefer to use a circular index as the key value. However, this approach is not recommended.
There are some problems with using index as the key value. When the order in the list changes, Vue will re-render the DOM through the diff algorithm. If you use index as the key value, it may cause some unnecessary DOM operations, thereby reducing performance. Therefore, we should use the unique identifier of each item as the key value.
For example:
d477f9ce7bf77f53fbcf36bec1b69b7a
ff6d136ddc5fdfeffaf53ff6ee95f185
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
929d1f5ca49e04fdcb27f9465b944689
21c97d3a051048b8e55e3c8f199a54b2
2. Use v-show instead of v-if
In Vue, both v-if and v-show can control the display and hiding of DOM elements. However, v-if has some additional overhead, as it completely recreates or destroys the DOM element. And v-show only hides or shows DOM elements by modifying CSS properties. Therefore, when we need to frequently switch the display and hiding of list items, we should use v-show instead of v-if.
For example:
d477f9ce7bf77f53fbcf36bec1b69b7a
ff6d136ddc5fdfeffaf53ff6ee95f185
<li v-for="item in items" :key="item.id" v-show="item.visible">{{ item.name }}</li>
929d1f5ca49e04fdcb27f9465b944689
21c97d3a051048b8e55e3c8f199a54b2
3. Use local component cache
When each item in a list corresponds to a more complex component, we can use the local component cache provided by Vue to improve performance. By placing the component definition within the 7c9485ff8c3cba5ae9343ed63c2dc3f7 tag, Vue will cache already rendered components when switching between components, thereby avoiding repeated DOM operations.
For example:
d477f9ce7bf77f53fbcf36bec1b69b7a
ff6d136ddc5fdfeffaf53ff6ee95f185
<keep-alive> <item :key="item.id" v-for="item in items"></item> </keep-alive>
929d1f5ca49e04fdcb27f9465b944689
21c97d3a051048b8e55e3c8f199a54b2
4. Use virtual lists
When the amount of data in the list is very large, we can use virtual lists to optimize rendering performance. A virtual list will only render the currently visible list items, rather than the entire list. Virtual lists can be implemented by dynamically calculating the position and size of each list item.
The following is an example of using vue-virtual-scroll-list, a Vue-based virtual list plug-in:
d477f9ce7bf77f53fbcf36bec1b69b7a
ff6d136ddc5fdfeffaf53ff6ee95f185
<virtual-list :size="items.length" :remain="10"> <template v-slot="{ range }"> <li v-for="index in range" :key="items[index].id">{{ items[index].name }}</li> </template> </virtual-list>
929d1f5ca49e04fdcb27f9465b944689
21c97d3a051048b8e55e3c8f199a54b2
5. Use paging loading
When the amount of list data is too large, you can use paging loading to reduce one-time loading The impact of large amounts of data on page performance. By dividing the list into multiple pages, only loading the data of the current page each time, and providing the function of paging switching, the rendering pressure of the page can be effectively reduced.
The following is an example of using vue-infinite-scroll, a Vue-based paging loading plug-in:
d477f9ce7bf77f53fbcf36bec1b69b7a
b3b4c2870ca397b75d2e0b1c03303414
ff6d136ddc5fdfeffaf53ff6ee95f185
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
929d1f5ca49e04fdcb27f9465b944689
182fe49a82b81b8dde1059717c296d45
eee507863231767bdcc207556a71d96d
<button @click="loadMore">Load More</button>
16b28748ea4df4d9c2150843fecfba68
21c97d3a051048b8e55e3c8f199a54b2
3f1c4e4b6b16bbbd69b2ee476dc4f83a
import InfiniteScroll from 'vue-infinite-scroll'
export default {
data() {
return { items: [], page: 1 }
},
methods: {
loadMore() { // AJAX请求获取下一页数据 // this.items = [...this.items, ...newData] this.page++ }
},
mounted() {
// 初始化分页加载插件 // this.$refs.infiniteScroll.addEventListener('scrolled', this.loadMore)
},
beforeDestroy() {
// 销毁分页加载插件 // this.$refs.infiniteScroll.removeEventListener('scrolled', this.loadMore)
}
}
2cacc6d41bbb37262a98f745aa00fbf0
Conclusion
Through the above optimization skills and practical experience, we can handle list rendering more efficiently in Vue , optimize page performance. Reasonable selection of key values, use of v-show, local component caching, virtual lists, and paging loading can improve page rendering speed and user experience. I hope this article will be helpful to you in the process of using list rendering in Vue.
The above is the detailed content of Optimization skills and practical experience of list rendering in Vue. For more information, please follow other related articles on the PHP Chinese website!