"Vue.js" code optimization: Add a line v-if in the DOM to write less a cyclic class method

Source: Internet
Author: User

"Problem description"

The items in the current user's shopping cart (cartlist), Commodity (good) selected field checked = True are displayed on the order page.

"General Practice" (two cycles)

First take the current user's shopping cart list, loop the item inside, if Item=true, add the current item to the OrderList array, and then display the orderlist array through v-if by displaying the DOM of the order list.

This type of hair is clearly used in two loops, a loop that loops through the method, and a loop that uses the v-for to loop through the DOM.

<template>    <li v-for= "item in OrderList" >        ...    </li></template><script>     //First step: Get the current user shopping cart method return cartlist     //Second step: Traverse cartlist and then pass item = True to store item in array orderlist </script>

"Code Optimization" (One-time loop)

First take the current user's shopping cart list cartlist array, and then directly to the DOM through V-for to cycle, and then add a condition, v-if= "Item.checked===true", meet the display, the code is as follows:

<template>       <li v-for= "item in Cartlist" v-if= "item.checked = = ' 1 '" >        ...    </li></template><script>    //First step: Get the current user shopping cart method return cartlist</script>

See, it only uses a cycle to make the "general approach" effect!

"Vue.js" code optimization: Add a line v-if in the DOM to write less a cyclic class method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.