💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
>[success] # 组件分割 在组件分割这里,类似下面淘宝这种列表为例 ![](https://img.kancloud.cn/c9/9a/c99a31a18299e83fac4ce2ab735a7763_1367x842.png) 之前开发时候可能就是直接将后台返回的数据进行直接的数据循环,但这其中问题是,有些数据需要特殊处理,由于整个数据都是循环出来的针对单独某一条基本是失去了计算属性带来的便利,即使说代码好看点也是用定义方法 **但如果将代码更加颗粒度拆分将其 item 变为组件可以发现 计算属性功能有可以了** ![](https://img.kancloud.cn/b8/28/b82821d43bb1c54b0e15bfff75b7cea3_1475x873.png) >[danger] ##### 之前的代码 [参考](https://sfc.vuejs.org/#eNqNUctK5EAU/ZWaYsAZMIkwMIsm3TDfMSUYOxWNdCpFVXWrhAJ1oYjPjYLSiBtFXClu2kbxZ5KoK3/BW5VINy2IgcB9nHtu3XMy/I9zt9eluIF9RRPeCRRtEUaUH8Y9CBB8JkQ9J0pFk+AYUChmKAxUQHCNAMwPx0EvTyevW7vF+mk+3Cx3Hsr9i9f+2svl+tvDbnG5UZ7188fzon9VXDw+b9yXB4fF1jAfrOWD6/L4vrw7gsFqynFGvJIHrJVlcz8zs9mdFwELdZ2oVU71nNa+Z1GTMxbDRdymExDf+zhuFBHme2MC+LItYq6QpKrLLSBOeCoU6kgUiTRBU65nJHCX5NRYNxM00jUAVLUt87dTJpXVDDURYH515O+PXrW72tfC07iicpKAA3nKwJnMQuuGJLiBbMXUYInJCV5UisuG58mobfxckm4qFjyIXNFlKk6oS2XizIt0WVIBxARPj3F4UOxR4QjKQiqo+IpzAvqJ19BqwjScUisEJ1QCgHhN9N8AMmtkg+Bibz8fDOE1xkzIy+2dsm9yaxwU/vydmSFY29eOpm4Onm/vvjU1SxhdseaENAq6HfMKrN8Bbd0hAw==) * data.js ~~~ const ls = [ {brand:"华为",type:"手机",price:"3600"}, {brand:"小米",type:"手机",price:"3600"}, ] export default ls ~~~ ~~~html <template> <div> <div v-for="item in data"> <!-- 这里做了拼接需要,如果你在好点提出一个方法做拼接--> <span>{{`${item.brand}${item.type}`}}</span> <span>{{item.price}}</span> </div> </div> </template> <script setup> import ls from './data.js' import {ref} from 'vue' const data = ref(ls) </script> ~~~ >[danger] ##### 较好的处理 [案例](https://sfc.vuejs.org/#eNqNU82K1EAQfpWiETYLO8mC4CFmRkQvnvRuC5tJOm6GpNN0d0YlNHgSRNSL4EUET3paXLzIgvgyszM+htWd9EycBdlAQv18qf6q6uuO3BUiXLaMxCTRrBZVqtmMcqqTvFyiAfhYE5aTopFTSkpEQckhT3VKiUfcOy2rXDIOcSpZeh9zAxQhSeSzHh352juL8iQanZ+oTJZCg2K6FQ5Q1qKRGioFhWxqOAgjyyBcqINRdkvDY3zAtjgGdpIVZkD5lH2zhivteoMpICao1KHP9SR7YjNyRPpSkzoVyKLhOMHOQYeEoiQGF7ExPMT6lJxqLVQcRarILKmFChv5NEIrlC3XZc1CpurJXDbPFJNYmJKjUY0Ig0smJ9hSziST/6u5B71S15Y1lBtsZRglttAPAKc8hccW0M1lyvOYksu371Y/L5CNfiEY+uvXb9afrC9kmdnAzVvHx5QYx3b31/f3m/Mf1/rrCeXsuVtOzoq0rSwLpDbe4J5Ie2UOokJHiZTPuk6XumIPeNEYg+uysX2E12joWPwD84q8nho7FEwtWs1yGKvpNl6grZiEbIQdJ3ZVcvbIesEgC89jqxMANyd4OF+wTA+LBz+QGIJDmM4g6IwTpVsffpwTRbD69Xvz4dvq4tWfsy+bs4+X55/XL7/uiGzngmQ87aCv2B8vsT3J4eRG5ziHnt6d0K3TXI1btuZkR8LfZH9JzF8sG4aG) ~~~ <template> <div> <div v-for="item in data"> <Children :areaData="item"></Children> </div> </div> </template> <script setup> import ls from './data.js' import Children from './Children.vue' import {ref} from 'vue' const data = ref(ls) </script> ~~~ * 拆分 ~~~ <template> <div> <span>{{titleInfo}}</span> <span>{{areaData.price}}</span> </div> </template> <script setup> import { computed } from 'vue'; const props = defineProps({ areaData: { type: Object, default: () => ({}) } }) // 使用了计算属性 const titleInfo = computed(() => { return `${props.areaData?.brand}${props.areaData?.type}` }) </script> ~~~