Vue单文件学习项目综合案例Demo,黑马vue教程

文章目录

  • 前言
  • 一、小黑记事本
  • 二、购物车
  • 三、小黑记账清单

前言

  • bilibili视频地址

一、小黑记事本

  • 效果图
    在这里插入图片描述

  • 主代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link rel="stylesheet" href="./css/index.css"/>
    <title>记事本</title>
</head>
<body>

<!-- 主体区域 -->
<section id="app">
    <!-- 输入框 -->
    <header class="header">
        <h1>小黑记事本</h1>
        <input placeholder="请输入任务" v-model="inputTask" class="new-todo"/>
        <button class="add" @click="addTask()">添加任务</button>
    </header>
    <!-- 列表区域 -->
    <section class="main">
        <ul class="todo-list" v-for="(item,index) in list" :key="item.id">
            <li class="todo">
                <div class="view">
                    <span class="index">{{ index + 1 }}</span> <label>{{ item.name }}</label>
                    <button class="destroy" @click="delTask(item.id)"></button>
                </div>
            </li>
        </ul>
    </section>
    <!-- 统计和清空 -->
    <footer class="footer" v-show="list.length > 0">
        <!-- 统计 -->
        <span class="todo-count">合 计:<strong> {{ list.length }} </strong></span>
        <!-- 清空 -->
        <button class="clear-completed" @click="clearAll()">
            清空任务
        </button>
    </footer>
</section>

<!-- 底部 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>

    const app = new Vue({
        el: '#app',
        data: {
            list: [
                {id: 2, name: "我要吃饭"},
                {id: 1, name: "我要睡觉"}
            ],
            inputTask: ""
        },
        methods: {
            addTask() {
                let tempId;
                //生成一个不重复的id
                if (this.list[0] != null) {
                    tempId = this.list[0].id + 1
                } else {
                    tempId = 1
                }
                this.list.unshift({
                    id: tempId,
                    name: this.inputTask
                });
                this.inputTask = ""
            },
            delTask(id) {
                this.list = this.list.filter(item => item.id != id);
            },
            clearAll() {
                this.list = [];
            }
        }
    })

</script>
</body>
</html>

二、购物车

  • 效果图
    在这里插入图片描述

  • 主代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./css/inputnumber.css" />
    <link rel="stylesheet" href="./css/index.css" />
    <title>购物车</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div class="app-container" id="app">
      <!-- 顶部banner -->
      <div class="banner-box"><img src="img/fruit.jpg" alt="" /></div>
      <!-- 面包屑 -->
      <div class="breadcrumb">
        <span>🏠</span>
        /
        <span>购物车</span>
      </div>
      <!-- 购物车主体 -->
      <div class="main" v-if="fruitList.length > 0">
        <div class="table">
          <!-- 头部 -->
          <div class="thead">
            <div class="tr">
              <div class="th">选中</div>
              <div class="th th-pic">图片</div>
              <div class="th">单价</div>
              <div class="th num-th">个数</div>
              <div class="th">小计</div>
              <div class="th">操作</div>
            </div>
          </div>
          <!-- 身体 -->
          <div class="tbody">
            <div class="tr" :class="{ active:item.isChecked}" v-for="(item,index) in fruitList" :key="item.id" >
              <div class="td"><input type="checkbox" v-model="item.isChecked" /></div>
              <div class="td"><img :src="item.icon" alt="" /></div>
              <div class="td">{{ item.price }}</div>
              <div class="td">
                <div class="my-input-number">
                  <button class="decrease" @click="sub(item.id)"> - </button>
                  <span class="my-input__inner">{{ item.num }}</span>
                  <button class="increase" @click="add(item.id)"> + </button>
                </div>
              </div>
              <div class="td">{{ item.price*item.num }}</div>
              <div class="td"><button @click="del(item.id)">删除</button></div>
            </div>
          </div>
        </div>
        <!-- 底部 -->
        <div class="bottom">
          <!-- 全选 -->
          <label class="check-all">
            <input type="checkbox" v-model="isAll" />
            全选
          </label>
          <div class="right-box">
            <!-- 所有商品总价 -->
            <span class="price-box">总价&nbsp;&nbsp;:&nbsp;&nbsp;¥&nbsp;<span class="price">{{ totalPrice }}</span></span>
            <!-- 结算按钮 -->
            <button class="pay">结算( {{ totalCount }} )</button>
          </div>
        </div>
      </div>
      <!-- 空车 -->
      <div class="empty" v-else>🛒空空如也</div>
    </div>
    <script>
      const defaultFruitList = [
        {
          id: 1,
          icon: 'img/火龙果.png',
          isChecked: true,
          num: 2,
          price: 6,
        },
        {
          id: 2,
          icon: 'img/荔枝.png',
          isChecked: false,
          num: 7,
          price: 20,
        },
        {
          id: 3,
          icon: 'img/榴莲.png',
          isChecked: false,
          num: 3,
          price: 40,
        },
        {
          id: 4,
          icon: 'img/鸭梨.png',
          isChecked: true,
          num: 10,
          price: 3,
        },
        {
          id: 5,
          icon: 'img/樱桃.png',
          isChecked: false,
          num: 20,
          price: 34,
        },
      ]
      const app = new Vue({
        el: '#app',
        data: {
          // 水果列表
          fruitList: JSON.parse(localStorage.getItem("list")) || defaultFruitList
          // fruitList: defaultFruitList
        },
        computed: {
          isAll: {
            get() {
              return this.fruitList.every(item => item.isChecked);
            },
            set(value) {
              this.fruitList.forEach(item => item.isChecked = value);
            }
          },
          //计算选中的总数
          totalCount(){
            let count=this.fruitList.reduce((sum,item)=>{
              if (item.isChecked){
                sum+=item.num
              }
              return sum;
            },0)
            return count;
          },
          //计算选中的价格
          totalPrice(){
            let count=this.fruitList.reduce((sum,item)=>{
              if (item.isChecked){
                sum+=item.num*item.price
              }
              return sum;
            },0)
            return count;
          }
        },
        methods: {
          del(id) {
            this.fruitList = this.fruitList.filter(item => item.id != id);
          },
          sub(id) {
            const fruit = this.fruitList.find(item => item.id == id);
            fruit.num--;
            //数量为0,删除商品
            if (fruit.num == 0) {
              this.del(id)
            }
          },
          add(id) {
            const fruit = this.fruitList.find(item => item.id == id);
            fruit.num++
          }
        },
        watch: {
          fruitList:{
            deep:true,
            handler (newValue) {
              //当被删完的时候置空,方便初始化数据
              if (newValue.length == 0) {
                newValue=null
              }
              //需要将变化后的数据存入本地(转JSON)
              localStorage.setItem("list", JSON.stringify(newValue))
            }
          }
        }
      })
    </script>
  </body>
</html>

三、小黑记账清单

  • 效果图
    在这里插入图片描述
  • 主代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- CSS only -->
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
    />
    <style>
      .red {
        color: red!important;
      }
      .search {
        width: 300px;
        margin: 20px 0;
      }
      .my-form {
        display: flex;
        margin: 20px 0;
      }
      .my-form input {
        flex: 1;
        margin-right: 20px;
      }
      .table > :not(:first-child) {
        border-top: none;
      }
      .contain {
        display: flex;
        padding: 10px;
      }
      .list-box {
        flex: 1;
        padding: 0 30px;
      }
      .list-box  a {
        text-decoration: none;
      }
      .echarts-box {
        width: 600px;
        height: 400px;
        padding: 30px;
        margin: 0 auto;
        border: 1px solid #ccc;
      }
      tfoot {
        font-weight: bold;
      }
      @media screen and (max-width: 1000px) {
        .contain {
          flex-wrap: wrap;
        }
        .list-box {
          width: 100%;
        }
        .echarts-box {
          margin-top: 30px;
        }
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="contain">
        <!-- 左侧列表 -->
        <div class="list-box">

          <!-- 添加资产 -->
          <form class="my-form">
            <input type="text" class="form-control" placeholder="消费名称" v-model.trim="name" />
            <input type="text" class="form-control" placeholder="消费价格" v-model.number="price"/>
            <button type="button" class="btn btn-primary" @click="add()">添加账单</button>
          </form>

          <table class="table table-hover">
            <thead>
              <tr>
                <th>编号</th>
                <th>消费名称</th>
                <th>消费价格</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item,index) in list" :key="item.id">
                <td>{{index+1}}</td>
                <td>{{item.name}}</td>
                <td :class="{ red: item.price>500}">{{item.price.toFixed(2)}}</td>
                <td><a href="javascript:;" @click="del(item.id)">删除</a></td>
              </tr>
            </tbody>
            <tfoot>
              <tr>
                <td colspan="4">消费总计: {{ totalPrice.toFixed(2) }}</td>
              </tr>
            </tfoot>
          </table>
        </div>
        
        <!-- 右侧图表 -->
        <div class="echarts-box" id="main"></div>
      </div>
    </div>
    <script src="../common/js/echarts.min.js"></script>
    <script src="../common/js/vue.js"></script>
    <script src="../common/js/axios.js"></script>
    <script>
      /**
       * 接口文档地址:
       * https://www.apifox.cn/apidoc/shared-24459455-ebb1-4fdc-8df8-0aff8dc317a8/api-53371058
       * 
       * 功能需求:
       * 1. 基本渲染
       * 2. 添加功能
       * 3. 删除功能
       * 4. 饼图渲染
       */
      const app = new Vue({
        el: '#app',
        data: {
          list:[],
          name:'',
          price:'',
        },
        methods:{
          async getList(){
            const res = await axios.get("https://applet-base-api-t.itheima.net/bill", {
              params: {
                creator: '小黑'
              }
            })
            this.list=res.data.data;
            this.myChart.setOption({
              series: [
                {
                  //更新饼图数据项
                  data: this.list.map(item=>({value:item.price,name:item.name}))
                }
              ]
            });
          },
          async add(){
            if (!this.name){
              alert("请输入消费名称")
              return
            }
            if (typeof this.price !== 'number'){
              alert("请输入正确的消费价格")
              return
            }
            let param={
              creator: '小黑',
              name:this.name,
              price:this.price
            };
            const res = await axios.post("https://applet-base-api-t.itheima.net/bill",param);
            console.log(res)
            this.getList();
            this.name=''
            this.price=''
          },
          async del(id){
            const res=await axios.delete("https://applet-base-api-t.itheima.net/bill/"+id);
            this.getList();
          }
        },
        created() {
          this.getList()
        },
        mounted(){
          option = {
            title: {
              text: '消费账单列表',
              subtext: '',
              left: 'center'
            },
            tooltip: {
              trigger: 'item'
            },
            legend: {
              orient: 'vertical',
              left: 'left'
            },
            series: [
              {
                name: '消费账单',
                type: 'pie',
                radius: '50%',
                data: [
                  { value: 1048, name: 'Search Engine' },
                  { value: 735, name: 'Direct' },
                  { value: 580, name: 'Email' },
                  { value: 484, name: 'Union Ads' },
                  { value: 300, name: 'Video Ads' }
                ],
                emphasis: {
                  itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              }
            ]
          };
          // 基于准备好的dom,初始化echarts实例
          this.myChart = echarts.init(document.getElementById('main'));
          // 绘制图表
          this.myChart.setOption(option);
        },
        computed:{
          totalPrice(){return this.list.reduce((sum,item)=>sum+=item.price,0)}
        }
      })
    </script>
  </body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/404684.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C# CAD2016 cass10宗地Xdata数据写入

一、 查看cass10写入信息 C# Cad2016二次开发获取XData信息&#xff08;二&#xff09; 一共有81条数据 XData value: QHDM XData value: 121321 XData value: SOUTH XData value: 300000 XData value: 141121JC10720 XData value: 权利人 XData value: 0702 XData value: YB…

卫星地面站监测系统仿真

当今世界&#xff0c;大国竞争日趋激烈&#xff0c;国际关系愈发紧张&#xff0c;信息与通信已经是当下高度信息化社会的“命脉”&#xff0c;信息只有经过有效且广泛地传播&#xff0c;才能成为一种有利用价值的资源&#xff0c;产生经济效益、推动社会发展。通信技术在发展的…

【必备清单】开学运动好物清单,迎接新学期,打造健康体魄!

随着新学期的开始&#xff0c;校园里的氛围渐渐热络起来。作为一名学生&#xff0c;除了学习之外&#xff0c;参与体育运动也是非常重要的。不仅可以锻炼身体&#xff0c;提高身体素质&#xff0c;还能增加社交机会&#xff0c;丰富学校生活。然而&#xff0c;想要成为一名校园…

software framwork

software framwork软件架构 软件架构&#xff0c;之前图没找到&#xff0c;随手画了一个啦&#xff0c;了解架构细分职能和工作任务&#xff1a; 下图&#xff0c;第一是客户端架构包项目&#xff0c;第二是服务端架构包项目 -----------------------------------------------…

数字化转型解锁企业高效协作与管理优化的新篇章!

一、客户介绍 某服饰股份有限公司是一家集服装设计、生产、销售及品牌建设于一体的企业。该公司的产品线涵盖男装、女装、童装等多个领域&#xff0c;设计风格时尚、简约、大方&#xff0c;深受消费者喜爱。公司注重产品研发&#xff0c;不断推陈出新&#xff0c;紧跟时尚潮流…

洗选中心智能化运维工是做什么的?智能化运维工程师是干什么的

洗选中心智能化运维工程师的职责和工作内容&#xff1f;同时&#xff0c;描述智能化运维工程师在信息技术行业中的具体角色和他们的主要任务。  洗选中心智能运维工程师的职责和工作内容主要包括&#xff1a;  设备监控管理&#xff1a;重点对洗涤中心机器进行实时监控管理…

C#与VisionPro联合开发——INI存储和CSV存储

1、INI存储 INI 文件是一种简单的文本文件格式&#xff0c;通常用于在 Windows 环境中存储配置数据。INI 文件格式由一系列节&#xff08;section&#xff09;和键值对&#xff08;key-value pairs&#xff09;组成&#xff0c;用于表示应用程序的配置信息。一个典型的 INI 文…

面试官:你知道Comparable 和 Comparator 的区别吗?我:巴拉巴拉

写在开头 面试官&#xff1a;“我们在Java的集合和数据结构中都离不开比较器&#xff0c;请你聊一聊Comparable 和 Comparator 这两种的区别吧” 内心活动&#xff1a;“上来就这么直接吗&#xff0c;那些ArrayList&#xff0c;HashMap都不问呀&#xff0c;好&#xff0c;既然…

船舶制造5G智能工厂数字孪生可视化平台,推进船舶行业数字化转型

船舶制造5G智能工厂数字孪生可视化平台&#xff0c;推进船舶行业数字化转型。随着数字化时代的到来&#xff0c;船舶行业正面临着前所未有的机遇与挑战。为了适应这一变革&#xff0c;船舶制造企业需要加快数字化转型的步伐&#xff0c;提高生产效率、降低成本并增强市场竞争力…

“职”想有你!庭田科技2024招聘开始啦!

关于|庭田科技 庭田科技有限公司&#xff08;简称&#xff1a;庭田科技&#xff09;是一家专注于计算机辅助工程(CAE)软件和高科技仪器设备的系统集成商和方案咨询服务供应商&#xff08;下设“上海庭田信息科技有限公司”与“西安庭田信息科技有限公司”&#xff09;。致力于…

Linux调试器——gdb的基础使用

目录 1.背景 2.指令的使用 2.1gdb的使用和退出 2.2显示源代码 2.3运行程序 2.4调试 1.打断点 2.查断点 3.去断点 4.运行 5.关闭断点 6.启用断点 7.逐过程 8.进入函数 9.显示变量的值 1.背景 众所周知&#xff0c;我们的程序发布有两种&#xff0c;分别是debug模式和release模式…

cocos creator3.x项目打包成aar 加入到已有的Android工程

Cocos crearor版本&#xff1a; 3.4.2 Android Studio Flamingo | 2022.2.1 Patch 2 1、配置构建安卓项目 2、 运行编译无报错 出现问题可尝试修改Gradle版本 修改jdk版本 3、对libservice打包成aar 打包完后 再build/outputs找到aar 如果看不到Tasks模块&#xff0c;在Fil…

uniapp_微信小程序自定义顶部导航栏和右侧胶囊对齐(不对齐来打我)

一、想要的效果 思路首先开启自定义导航栏&#xff0c;取消自带的导航栏&#xff0c;然后计算胶囊的高度和标题对齐 二、成品代码 1、首先再你需要居中的代码添加以下style <view class"header":style"{paddingTop:navBarTop px,height:navBarHeight px,…

Node.js安装及环境配置

1. 前言 Node.js简介 Node.js 是一个开源的、跨平台的 JavaScript 运行环境&#xff0c;它允许开发者使用 JavaScript 编写服务器端代码。Node.js 基于 Google 的 V8 JavaScript 引擎构建&#xff0c;该引擎是 Chrome 浏览器中用于解析和执行 JavaScript 的核心组件。因此&am…

Ubuntu20.04 查看系统版本号

目录 uname -auname -vlsb_release -acat /etc/issuecat /proc/version uname -a 查看系统发行版本号和操作系统版本 uname -v 查看版本号 lsb_release -a 查看发行版本信息 cat /etc/issue 查看系统版本 cat /proc/version 查看内核的版本号

Graphpad Prism10.2.0(329) 安装教程 (含Win/Mac版)

GraphPad Prism GraphPad Prism是一款非常专业强大的科研医学生物数据处理绘图软件&#xff0c;它可以将科学图形、综合曲线拟合&#xff08;非线性回归&#xff09;、可理解的统计数据、数据组织结合在一起&#xff0c;除了最基本的数据统计分析外&#xff0c;还能自动生成统…

来分析两道小题

一、源码 二、分析 首先它会接两个参数一个是id一个是ps&#xff0c;传递的话会包含一个flag.php&#xff0c;然后数据库连接&#xff0c;之后传递过滤&#xff0c;然后查询&#xff0c;如果查到了就会取id&#xff0c;取出来看是不是跟adog一样&#xff0c;如果是它告诉你账号…

在springboot中调用openai Api并实现流式响应

之前在《在springboot项目中调用openai API及我遇到的问题》这篇博客中&#xff0c;我实现了在springboot中调用openai接口&#xff0c;但是在这里的返回的信息是一次性全部返回的&#xff0c;如果返回的文字比较多&#xff0c;我们可能需要等很久。 所以需要考虑将请求接口响应…

c++服务器开源项目Tinywebserver运行

c服务器开源项目Tinywebserver运行 一、Tinywebserver介绍二、环境搭建三、构建数据库四、编译Tinywebserver五、查看效果 Tinywebserver是github上一个十分优秀的开源项目&#xff0c;帮助初学者学习如何搭建一个服务器。 本文讲述如何在使用mysql跟该项目进行连接并将项目运行…

集合、List、Set、Map、Collections、queue、deque

概述 相同类型的数据进行统一管理操作&#xff0c;使用数据结构、链表结构&#xff0c;二叉树 分类&#xff1a;Collection、Map、Iterator 集合框架 List接口 有序的Collection接口&#xff0c;可以对列表中的每一个元u尿素的插入位置进行精确的控制&#xff0c;用户可以根…