二十一、computed Option 计算选项

本文深入探讨Vue中的计算属性(computed)功能,展示如何利用计算属性对原始数据进行格式化、大小写转换、顺序调整等操作,提高代码复用性和效率。通过具体示例,如价格格式化显示和新闻列表逆序呈现,阐述计算属性在实际项目中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                    computed Option 计算选项

     computed 的作用主要是对原数据进行改造输出。改造输出:包括格式的编辑,大小写转换,顺序重排,添加符号……

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="../assets/js/vue.js"></script>
    <title>Computed Option 计算选项</title>
</head>
<body>
    <h1>Computed Option 计算选项</h1>
    <hr>
    <div id="app">
        {{newPrice}}
        <hr>
        <br>
        <ul>
            <li v-for="(news,index) in reverseNews">
                {{index+1}}、{{ news.title}}:{{ news.date  }}
            </li>
        </ul>
    </div>


    <script type="text/javascript">

        var app=new Vue({
            el:'#app',
            data:{
                price:100,
                newsList:[
                    {title:'香港或就“装甲车被扣”事件追责 起诉涉事运输公司',date:'2017/3/10'},
                    {title:'日本第二大准航母服役 外媒:针对中国潜艇',date:'2017/3/12'},
                    {title:'中国北方将有明显雨雪降温天气 南方阴雨持续',date:'2017/3/13'},
                    {title:'起底“最短命副市长”:不到40天落马,全家被查',date:'2017/3/23'},
                ]
            },
            computed:{
                newPrice:function(){
                    return this.price='¥' + this.price + '元';
                },
                reverseNews:function(){
                    return this.newsList.reverse();
                }
            }
        })
    </script>
</body>
</html>
<template> <div class="checkbox-container"> <h2>Vue Checkbox 全选/单选联动</h2> <!-- 全选控制 --> <div class="select-group"> <input type="checkbox" id="selectAll" v-model="selectAll" :indeterminate="indeterminate" > <label for="selectAll">全选</label> <span class="indicator"> ({{ selectedCount }} / {{ options.length }}) </span> </div> <hr> <!-- 子选项 --> <div class="option-group" v-for="(option, index) in options" :key="option.id"> <input type="checkbox" :id="'option' + index" v-model="option.selected" @change="updateIndeterminate" > <label :for="'option' + index"> {{ option.label }} <span class="option-id">(ID:{{ option.id}})</span> </label> </div> <!-- 操作统计 --> <div class="results"> <h3>当前选中:</h3> <ul> <li v-for="option in selectedOptions" :key="option.id"> {{ option.label }} (ID:{{ option.id }}) </li> <li v-if="selectedOptions.length === 0">没有选中任何选项</li> </ul> </div> </div> </template> <script setup> import { ref, computed, reactive } from 'vue' // 选项列表 const options = reactive([ { id: 1, label: '选项A', selected: false }, { id: 2, label: '选项B', selected: false }, { id: 3, label: '选项C', selected: false }, { id: 4, label: '选项D', selected: false }, ]) // 计算属性:选中的子选项 const selectedOptions = computed(() => options.filter(option => option.selected) ) // 计算属性:选中的数量 const selectedCount = computed(() => options.reduce((count, option) => count + (option.selected ? 1 : 0), 0) ) // 中间态(当部分选中时) const indeterminate = ref(false) // 更新中间态状态 const updateIndeterminate = () => { indeterminate.value = selectedCount.value > 0 && selectedCount.value < options.length } // 计算属性:全选状态 const selectAll = computed({ get: () => selectedCount.value === options.length, set: (newValue) => { // 当全选状态变化时,更新所有子选项 options.forEach(option => { option.selected = newValue }) indeterminate.value = false // 重置中间态 } }) </script> <style> .checkbox-container { max-width: 500px; margin: 2rem auto; padding: 1.5rem; border-radius: 10px; background: #f8f9fa; box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1); font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } h2 { color: #2c3e50; text-align: center; margin-bottom: 1.5rem; } .select-group { padding: 0.8rem; background: #e1f5fe; border-radius: 6px; margin-bottom: 1.5rem; font-weight: bold; display: flex; align-items: center; gap: 10px; } .option-group { padding: 0.8rem; background: #fff; border-radius: 6px; margin: 0.5rem 0; display: flex; align-items: center; gap: 10px; transition: background 0.2s; } .option-group:hover { background: #e8f4ff; } .indicator { margin-left: auto; color: #546e7a; font-size: 0.9rem; } .option-id { font-size: 0.8rem; color: #78909c; } .results { margin-top: 2rem; padding: 1rem; border-top: 1px dashed #b0bec5; } .results h3 { margin-top: 0; color: #37474f; } .results ul { padding-left: 20px; list-style-type: square; } .results li { padding: 0.3rem 0; color: #455a64; } hr { border: 0; border-top: 1px solid #e0e0e0; margin: 1rem 0; } </style>逐行中文解释每行代码含义
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值