VUE之旅—day2

news2024/9/23 3:17:13

文章目录

  • Vue生命周期和生命周期的四个阶段
        • created应用—新闻列表渲染
        • mounted应用—进入页面搜索框就获得焦点
        • 账单统计(Echarts可视化图表渲染)

Vue生命周期和生命周期的四个阶段

思考:

什么时候可以发送初始化渲染请求?(越早越好)

什么时候可以开始操作dom?(至少dom得渲染出来)

Vue生命周期: 一个Vue实例从创建到销毁的整个过程

生命周期四个阶段: ①创建②挂载③更新④销毁

在这里插入图片描述

在这里插入图片描述

Vue生命周期函数(钩子函数)

Vue生命周期过程中,会自动运行一些函数,被称为【生命周期钩子】—>让开发者可以在【特定阶段】运行自己的代码。

在这里插入图片描述

代码说明

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>生命周期的四个阶段</title>
</head>

<body>
  <div id="app">
    <h1>{{title}}</h1>
    <button @click="sub">-</button>
    <span>{{count}}</span>
    <button @click="add">+</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        count: 100,
        title: '计数器'
      },
      // 1.创建阶段(准备数据)
      beforeCreate() {
        console.log('beforeCreate 响应式数据准备好之前', this.count);//undefined
      },
      created() {
        // created,这一阶段开始,就能发送初始化渲染请求
        console.log('Created 响应式数据准备好之后', this.count);// 100
      },

      // 2.挂载阶段(渲染模板)
      beforeMount() {
        console.log('beforeMount 模板渲染之前', document.querySelector('h1').innerHTML);// {{title}}
      },
      mounted() {
        // created,这一阶段开始,就能操作dom了
        console.log('mounted 模板渲染之后', document.querySelector('h1').innerHTML);// 计数器
      },

      // 3.更新阶段(修改数据 → 更新视图)
      beforeUpdate() {
        console.log('beforeUpdate 数据修改了,视图还没更新', document.querySelector('span').innerHTML);
      },
      updated() {
        console.log('Updated 数据修改了,视图已经更新', document.querySelector('span').innerHTML);
      },
      //4.卸载阶段
      //Vue提供了一个语法 Vue对象名.$destroy()  用来查看卸载状态
      beforeDestroy() {
        console.log('beforeDestory 卸载前');
        console.log('清除掉一些Vue以外的资源占用,定时器,延时器...');
      },
      destroyed() {
        console.log('destroyed 卸载后');
      },
      methods: {
        add() {
          this.count++
        },
        sub() {
          this.count--
        }
      }
    })
  </script>
</body>

</html>
created应用—新闻列表渲染
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>新闻列表渲染</title>
  <style>
    #app {
      width: 500px;
      margin: 0 auto;
    }

    #app ul {
      width: 100%;
      margin: 0;
      padding: 0;
      list-style: none;
    }

    #app ul li.news {
      width: 100%;
      height: 120px;
      display: flex;
      background-color: rgb(252, 252, 252);
      margin: 20px 0;
      border: 1px solid #eee;
      border-left: none;
      border-right: none;
    }

    #app ul li.news .left {
      width: 70%;
      height: 100%;

    }

    #app ul li.news .left .title {
      width: 90%;
      height: 70%;
      font-size: 18px;
      font-weight: bold;
      margin: 5px 0;
      color: #292929;
    }

    #app ul li.news .left span {
      font-size: 14px;
      color: #454545;
      margin-right: 20px;
    }

    #app ul li.news .right {
      width: 30%;
      height: 100%;
    }

    #app ul li.news .right img {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="(item,index) in list" :key="item.id" class="news">
        <div class="left">
          <div class="title">{{item.title}}</div>
          <div class="info">
            <span>{{item.source}}</span>
            <span>{{item.time}}</span>
          </div>
        </div>
        <div class="right">
          <img :src="item.img" alt="">
        </div>
      </li>
    </ul>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
  <script>
    // 接口地址:http://hmajax.itheima.net/api/news
    // 请求方式 get 
    const app = new Vue({
      el: '#app',
      data: {
        list: []
      },
      async created() {
        // 1.发送请求,获取数据
        const res = await axios.get('http://hmajax.itheima.net/api/news')
        console.log(res);//查看获取到的数据
        // 2.将数据更新给data中的list
        this.list = res.data.data
      }
    })
  </script>
</body>

</html>
mounted应用—进入页面搜索框就获得焦点
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>搜索框一进入就获得焦点</title>
  <style>
    .container {
      width: 600px;
      height: auto;
      margin: 0 auto;
    }

    .container .seacher-container {
      width: 100%;
      height: 150px;
      text-align: center;
      background-color: aliceblue;
    }

    .container .seacher-container .search-box {
      width: 80%;
      height: 35px;
      margin: 20px auto;
      border: 1px solid #8b8b8b;
      display: flex;
      border-radius: 5px;
      justify-content: flex-end;
      align-items: center;
      background-color: #ffffff;
      border-right: none;
    }

    .container .seacher-container .search-box input {
      width: 78%;
      height: 90%;
      border: none;
      outline: none;
      background-color: #ffffff;

    }

    .container .seacher-container .search-box button {
      width: 20%;
      height: 106%;
      border: none;
      outline: none;
      background-color: #ea2704;
      color: #f5f5f5;
      border-radius: 5px;
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
      cursor: pointer;
    }
  </style>
</head>

<body>
  <div class="container" id="app">
    <div class="seacher-container">
      <img src="http://www.itheima.com/images/logo.png" alt="">
      <div class="search-box">
        <input type="text" v-model="words" id="inp" autocomplete="off">
        <button>搜索一下</button>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        words: ''
      },
      // 核心思路:
      // 1.等输入框渲染出来
      // 2.让输入框获取焦点
      mounted() {
        document.querySelector('#inp').focus()
      }
    })
  </script>
</body>

</html>
账单统计(Echarts可视化图表渲染)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #app {
      margin: 0 auto;
      width: 1100px;
      height: auto;
    }

    h3 {
      color: #8d5252;
    }

    /* https://www.apifox.cn/apidoc/shared-24459455-ebb1-4fdc- */
    .container {
      width: 1100px;
      display: flex;
      justify-content: space-between;
    }

    .container #tableArea {
      width: 50%;
      height: auto;
    }

    .container #tableArea .iptArea {
      width: 100%;
      height: 30px;
      margin-bottom: 10px;
    }

    .container #tableArea .iptArea input {
      width: 40%;
      height: 100%;
      border: 1px solid #e2e1e1;
      border-radius: 4px;
      text-indent: 5px;
      outline: none;
    }

    .container #tableArea .iptArea button {
      width: 15%;
      height: 110%;
      border: none;
      outline: none;
      background-color: rgb(9, 114, 206);
      color: #fff;
      border-radius: 4px;
      cursor: pointer;
    }

    .container #tableArea table {
      width: 100%;
      height: auto;
      text-align: left;
      border-collapse: collapse;
      font-size: 14px;
    }

    .container #tableArea table tr {
      height: 40px;
      border-bottom: 1px solid #eee;
    }

    .container #tableArea table tr .red {
      color: red;
    }

    .container #tableArea table tr td a {
      color: rgb(42, 97, 238);
      text-decoration: none;
    }

    .container #chartArea {
      width: 45%;
      height: 330px;
      border: 1px solid #eee;
      padding: 10px;
    }

    @media (max-width: 768px) {
      .container {
        width: 600px;
        flex-wrap: wrap;
        justify-content: center;
      }

      .container #tableArea {
        width: 90%;
      }

      .container #chartArea {
        width: 90%;
      }
    }

    @media (min-width: 1200px) {
      .container {
        width: 1100px;
        flex-wrap: wrap;
      }

      .container #tableArea {
        width: 50%;
      }

      .container #chartArea {
        width: 45%;
      }
    }
  </style>
</head>

<body>
  <div id="app">
    <h3>小黑记账清单</h3>
    <div class="container">
      <div id="tableArea">
        <div class="iptArea">
          <input type="text" placeholder="消费名称" v-model.trim="name">
          <input type="text" placeholder="消费价格" v-model.number="price">
          <button @click="addData">添加账单</button>
        </div>
        <table>
          <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}}</td>
              <td><a @click="delData(item.id)" href="javascript:;">删除</a></td>
            </tr>
          </tbody>
          <tfoot>
            <tr>
              <th colspan="4">消费总计:<span>{{totalPrice}}</span></th>
            </tr>
          </tfoot>
        </table>
      </div>
      <div id="chartArea">
        <div id="main" style="width:550px;height:330px;"></div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/echarts@5.5.0/dist/echarts.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
  <!-- 
  //接口地址
  // 查询我的账单列表 https://applet-base-api-t.itheima.net/bill get请求方式,请求参数creator
  // 删除账单明细 https://applet-base-api-t.itheima.net/bill/{id} delete请求方式,请求参数id
  // 添加账单 https://applet-base-api-t.itheima.net/bill post请求方式,请求参数creator、name、 price
  // 转换 https://applet-base-api-t.itheima.net/bill
   -->
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        list: [],
        name: '',
        price: ''
      },
      computed: {
        totalPrice() {
          return this.list.reduce((sum, item) => sum + item.price, 0).toFixed(2)
        }
      },
      created() {
        this.getData()
      },
      mounted() {
        this.myChart = echarts.init(document.getElementById('main'));
        this.myChart.setOption({
          title: {
            text: '消费账单列表',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'left'
          },
          series: [
            {
              name: '消费账单',
              type: 'pie',
              radius: '50%',
              data: [

              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        });

      },
      methods: {
        async getData() {
          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 addData() {
          // 优化
          if (!this.name) {
            alert("请输入消费名称")
            return
          }
          if (typeof this.price !== 'number') {
            alert("请输入正确的消费价格")
            return
          }
          //发送添加请求
          const res = await axios.post('https://applet-base-api-t.itheima.net/bill', {
            creator: '小黑',
            name: this.name,
            price: this.price
          })
          console.log(res);
          // 重新再渲染一次
          this.getData()
          // 清空输入框
          this.name = ''
          this.price = ''
        },
        async delData(id) {
          const res = await axios.delete(`https://applet-base-api-t.itheima.net/bill/${id}`)
          console.log(res);
          // 重新再渲染一次
          this.getData()
        }
      }

    })
  </script>
</body>

</html>

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

经验分享智能产品从0到1全流程

大家好&#xff0c;今天继续分享文章&#xff0c;这篇文章在网络上搜索资料时&#xff0c;有感而发&#xff0c;分享一个智能产品从0到1的整个生命周期中需要经历哪些阶段&#xff0c;我这里以开发一个mini补光灯为例&#xff0c;深入探索各个阶段可能涉及的具体活动和考虑事项…

记录计全支付切换到RabbitMQ时启动报错的问题

记录计全支付切换到RabbitMQ时启动报错的问题 首先在application.yml中切换到RabbitMQ配置安装RabbitMQ、Erlang、延时插件 rabbitmq_delayed_message_exchange&#xff0c;延迟插件必装 首先在application.yml中切换到RabbitMQ配置 # 第一处rabbitmq:addresses: 127.0.0.1:56…

TRL校准和De-embedding的区别以及如何操作?

Fiture的性能可以在测试前利用TRL校准件移除掉&#xff0c;但是TRL的步骤比较繁琐或者说TRL校准件&#xff08;包含直通、反射、多条Line&#xff09;很难设计(如果做到很高的频率对设计和加工制造的要求都很高)&#xff0c;此时可以选择只做一根2x Through&#xff08;直通件&…

电脑常用的PDF阅读器-嗨动PDF编辑器!带你详细了解它

电脑常用的PDF阅读器-嗨动PDF编辑器&#xff01;在数字化信息爆炸的时代&#xff0c;PDF格式的文件因其易于打印和保留原始格式等优点&#xff0c;成为了人们日常工作和学习的常用格式。而对于PDF文件的处理&#xff0c;一款功能强大、操作简便的PDF阅读器是必不可少的。今天&a…

世界500强企业建设软件开发安全体系,打造DevSecOps示范标杆

某世界500强企业旗下拥有众多知名汽车品牌&#xff0c;业务涵盖出行服务、科技创新、金融服务、教育等。该集团一直专注于技术创新和人才培养&#xff0c;同时也是国内第一批倡议并践行“数字化转型”的汽车行业企业。 数智化建设转型&#xff0c;研发安全体系建设势在必行 在…

C#【进阶】委托和事件

委托和事件 文章目录 1、委托1、委托概念2、基本语法3、定义自定义委托4、使用自定义委托5、委托变量可以存储多个函数6、系统定义好的委托思考 怪物死亡数据更新 2、事件1、事件概念2、事件的使用3、为什么有事件思考 热水器 3、匿名函数1、匿名函数概念2、基本语法3、使用4、…

27_Scala功能函数

文章目录 功能函数1.功能函数处理集合数据2.扁平化操作3.按照指定条件将数据集中的数据进行过滤4.集合通过 自定义函数进行分组5.mapValues6.sortBy函数 功能函数 1.功能函数处理集合数据 –集合的功能函数 map List --> map( logical ) --> newList–实现一个不确定的…

Blender雕刻建模流程

1.构形 先构造一个大致相像的外形 可使用的方法包含 -多边形&#xff1a;表面细分&#xff0c;布尔 -曲线&#xff1a;曲线倒角 -融球&#xff08;使用较少&#xff09; -曲面&#xff08;使用较少&#xff09; 构形之后的准备 -应用缩放 -应用修改器 -曲线转网格 1.1…

8个迹象表明你需要一台新笔记本电脑,看一下你的笔记本是否有其中一个

序言 当你第一次打开你的笔记本电脑的盒子时,它会以最高性能运行,电池寿命更长,过热最小,资源使用效率高。然而,随着笔记本电脑的老化,它将不能满足预期用途。以下几个迹象表明,可能是时候寻找并投资一款新设备了。 你的设备不再具有预期用途 如果你的笔记本电脑不再…

战网国际服注册教程 暴雪战网国际服账号注册一站式教程分享

战网国际版&#xff0c;也即Battle.net环球版&#xff0c;是由暴雪娱乐操刀的全球化游戏交流枢纽&#xff0c;它突破地理限制&#xff0c;拥抱全世界的游戏玩家。与仅限特定地区的版本不同&#xff0c;国际版为玩家开辟了无障碍通道&#xff0c;让他们得以自由探索暴雪庞大游戏…

MacOS docker 安装与配置

orbstack 安装 官网&#xff1a; https://orbstack.dev 下载链接&#xff1a;Download OrbStack Fast, light, simple Docker Desktop alternative 选择是Apple M系列处理器&#xff0c; 或 Intel系列处理器 到这里就安装好了Orbstack软件&#xff0c;下面开始配置docker 下…

C语言/数据结构——栈的实现

一.前言 今天我们讲解新的领域——栈。 二.正文 1.栈 1.1栈的概念及结构 栈&#xff1a;一种特殊的线性表&#xff0c;其允许在固定的一段进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶&#xff0c;另一端称为栈底。栈中的数据元素遵守后进先出LIFO&#…

css笔记总结2

找到所有的 h1 标签。 选择器&#xff08;选对人&#xff09; 设置这些标签的样式&#xff0c;比如颜色为红色&#xff08;做对事&#xff09;。 ##css基础选择器 基础选择器又包括&#xff1a;标签选择器、类选择器、id 选择器和通配符选择器 ###标签选择器&#xff1a; 标签…

PostgreSQL(十二)报错:Tried to send an out-of-range integer as a 2-byte value: 51000

目录 一、报错场景二、源码分析三、实际原因&#xff08;更加复杂&#xff09;四、解决思路 一、报错场景 今天写了一个历史数据处理程序&#xff0c;在开发环境、测试环境都可以正常执行&#xff0c;但是放到生产环境上就不行&#xff0c;报了一个这样的错误&#xff1a; or…

【SpringBoot】SpringBoot整合jasypt进行重要数据加密

&#x1f4dd;个人主页&#xff1a;哈__ 期待您的关注 目录 &#x1f4d5;jasypt简介 &#x1f525;SpringBoot使用jasypt &#x1f4c2;创建我需要的数据库文件 &#x1f4d5;引入依赖 &#x1f513;配置数据库文件&#xff08;先不进行加密&#xff09; &#x1f319;创…

十二.吊打面试官系列-JVM优化-深入JVM内存模型

JVM内存模型 1.JVM的组成 整个JVM组成由 &#xff1a;运行时数据区 &#xff0c; 类加载子系统 &#xff0c; 执行引擎 &#xff0c; 本地方法库 几部分组成 上面是Java7的内存模型&#xff0c;Java8以后做了一些调整&#xff0c;把方法区变成了元空间&#xff0c;元空间不在…

Linux 第三十三章

&#x1f436;博主主页&#xff1a;ᰔᩚ. 一怀明月ꦿ ❤️‍&#x1f525;专栏系列&#xff1a;线性代数&#xff0c;C初学者入门训练&#xff0c;题解C&#xff0c;C的使用文章&#xff0c;「初学」C&#xff0c;linux &#x1f525;座右铭&#xff1a;“不要等到什么都没有了…

Golang | Leetcode Golang题解之第80题删除有序数组中的重复项II

题目&#xff1a; 题解&#xff1a; func removeDuplicates(nums []int) int {n : len(nums)if n < 2 {return n}slow, fast : 2, 2for fast < n {if nums[slow-2] ! nums[fast] {nums[slow] nums[fast]slow}fast}return slow }

1、sql server数据库进行sql注入

靶机取自&#xff1a;墨者sql server 1、判断数据库类型 抓包知sql server&#xff0c;所以注入语句跟MySQL有些区别 2、判断注入点 “http://219.153.49.228:42514/new_list.asp?id2 ”&#xff0c;当id2 and 11时显示正确&#xff0c;id2 and 12时页面报错。 3、确定列…

Vue.js的发展史(一)

Vue.js的发展史&#xff08;一&#xff09; 什么是Vue? Vue (发音为 /vjuː/&#xff0c;类似 view) 是一款用于构建用户界面的 JavaScript 框架。它基于标准 HTML、CSS 和 JavaScript 构建&#xff0c;并提供了一套声明式的、组件化的编程模型&#xff0c;帮助你高效地开发…