ts+vue3实现页面的添加数据,删除数据,没有更多数据的显示

 页面布局

<template>
    <div class="warrp">
        <!-- 头部 -->
        <div class="header">
            <h2>拾忆记事本</h2>
            <h6>记录生活点点滴滴,写下生活每一个美好</h6>
            <div class="flex input">
                <div>
                    <select class="sele" v-model="selected" @click="uptype">
                        <option  v-for="(item,index) in seleArr" :value="item.id" :key="index">{{item.title}}</option>
                    </select>
                </div>
                <div>
                    <input type="text" class="inp" v-model="todotext">
                </div>
                <div class="que" @click="addTodo">
                    确认
                </div>
            </div>
        </div>
        <!-- 内容 -->
        <div class="max red"  v-if="todoList.length > 0 && selected === 1">
            <div class="flex item" v-for="(item,index) in todoList" :key="index">
                <div class="flex"> 
                    <div class="index">{{index+1}}</div>
                    <div class="fu">|</div>
                    <div>
                        {{item.conset}}
                    </div>
                </div>
                <div @click="del(index)">❌</div>
            </div>
        </div>
        <!-- 内容 -->
        <div class="max" v-if="todoTile.length > 0 && selected === 2">
            <div class="flex item" v-for="(item,index) in todoTile" :key="index">
                <div class="flex"> 
                    <div class="index">{{index+1}}</div>
                    <div class="fu">|</div>
                    <div>
                        {{item.conset}}
                    </div>
                </div>
                <div @click="del(index)">❌</div>
            </div>
        </div>
        <!-- 底部 -->
        <div v-if="(selected === 1 && todoList.length > 0) || (selected === 2 && todoTile.length > 0)">
            <!-- 总数 -->
            <div class="flex footer">
                <div>总数:{{all}}</div>
                <div @click="delArr">清楚</div>
            </div>
            <!-- 没有更多数据 -->
        </div>
        <!-- 没有更多数据 -->
        <div v-else>
            <img src="../../../../assets/image/ZnoSHOW.png" class="img">
        </div>
    </div>
</template>

TS代码

<script setup lang="ts">
    import {reactive, ref,watchEffect} from 'vue'
    let todotext=ref('')//拿到input框的值
    let id=ref(3)//声名id
    let selected=ref(1)//类型的值
    let all=ref(0)//显示总条数
    //类型数组
    const seleArr=reactive([
        {
            id:1,
            title:"内容"
        },{
            id:2,
            title:"标题"
        }
    ])
    //接口声名
    interface ITodo{
        id:number,
        conset:string
    }
    //内容的数组
    let todoList:ITodo[]=reactive([
        {
            id:1,
            conset:"内容"
        },{
            id:2,
            conset:"内容1"
        },{
            id:3,
            conset:"内容2"
        }
    ])
     //标题数组
    let todoTile:ITodo[]=reactive([
        {
            id:1,
            conset:"内容"
        },
    ])
    //添加
    const addTodo=()=>{
        if(!todotext.value){
            alert('请填写要做的事情')
            return false
        }
        let obj={
            id:++id.value,
            conset:todotext.value
        }
        // console.log(obj)
        //判断我们需要填写的类型
        if (selected.value === 1) {
            // 内容
            todoList.push(obj)
            console.log('todosTitle',)
        } else {
            // 标题
            todoTile.push(obj)
            console.log('todosTitle',)
        }
        todotext.value=""
    }
    //删除
    const del=(index:number)=>{
        selected.value === 1 && todoList.splice(index, 1)
        selected.value === 2 && todoTile.splice(index, 1)
    }
    //批量删除
    const delArr=()=>{
        selected.value === 1 && (todoList.length = 0)
        selected.value === 2 && (todoTile.length = 0)
    }
    //切换类型
    const uptype=(e:any)=>{
        todotext.value=""
        selected.value=parseInt(e.target.value)
    }
    // 监听数据变化
    watchEffect(() => {
        selected.value === 1 &&  (all.value = todoList.length)
        selected.value === 2 &&  (all.value = todoTile.length)
    })
</script>

 CSS样式

<style scoped>
    /* .warrp{
        border: 1px solid red;
    } */
    .img{
        width: 600px;
        height: 100%;
    }
    .flex{
        display: flex;
    }
    .header{
        background-color: skyblue;
        padding: 0 20px;
        box-sizing: border-box;
        height: 150px;
        width: 600px;
    }
    .max{
        padding:20px;
        background-color: #fff;
    }
    .item{
        height: 50px;
        line-height: 50px;
        border-bottom:1px dashed black ;
        display: flex;
        justify-content: space-between;
        margin-bottom: 20px;
    }
    .fu{
        margin: 0 10px;
    }
    .footer{
        padding: 0 20px;
        box-sizing: border-box;
        justify-content: space-between;
    }
    .input{
        width: 80%;
        height: 40px;
        border-radius: 8px;
        border: 1px solid red;
    }
    .sele{
        height: 100%;
        border:none;
        width: 70px;
        line-height: 100%;
        border-radius: 8px 0 0 8px;
        outline:none;
    }
    .inp{
        height: 38px;
        border:none;
        width: 300px;
        outline:none;
        line-height: 100%;
    }
    .que{
        width: 74px;
        height: 100%;
        text-align: center;
        line-height: 40px;
        border-radius: 0 8px 8px 0;
        background-color: #ccc;
    }
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中使用TypeScript管理后台的数据添加非常简单。首先,我们需要创建一个组件,用于接收用户输入的数据。我们可以定义一个表单,包含输入框、下拉框、多选框等表单元素,让用户填写相关数据。我们可以使用Vue的双向绑定来更新组件的数据模型。 接下来,我们需要定义一个数据模型,用于保存用户的输入数据。我们可以使用TypeScript的接口或类来定义数据模型,其中包含与表单元素对应的属性。例如,如果表单中有一个名称输入框和一个描述输入框,则可以定义一个接口或类,包含name和description属性。在组件中,我们可以使用v-model指令将输入框的值与数据模型中的属性进行绑定。 当用户填写完表单并点击提交按钮时,我们可以在组件中定义一个方法,用于将数据模型发送到后台。我们可以使用Axios等库来进行网络请求,并将数据模型作为参数传递给后台的API接口。在这个方法中,我们可以处理请求的返回结果,例如显示成功的消息或错误信息。 在Vue 3中,我们可以使用Composition API编写组件。在组件中,我们可以使用setup函数来定义数据模型、方法和其他逻辑。我们可以使用ref来创建响应式数据,使用toRefs将数据模型转换为响应式对象并在模板中使用。我们也可以使用watchEffect来监听数据的变化,并在数据变化时触发特定的逻辑。 总之,使用Vue 3和TypeScript管理后台的数据添加非常方便。我们可以通过定义组件、数据模型和方法来实现数据的双向绑定和请求发送。在这个过程中,我们可以利用Vue 3的新特性和TypeScript的类型检查来提高开发效率和代码质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值