第三天课程上午

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




  
  
  
  Document



  

{{ title }}

{{ count }}

2.生命周期案例

(1)created初始化渲染

第三天课程上午_第1张图片

 重点是用了钩子函数

 async created () {
        // 1. 发送请求获取数据
        const res = await axios.get('http://hmajax.itheima.net/api/news')
        // 2. 更新到 list 中,用于页面渲染 v-for
        this.list = res.data.data
      }



  
  • {{ item.title }}
    {{ item.source }} {{ item.time }}

(2)mounted获取焦点

第三天课程上午_第2张图片

 

    // 核心思路:

    // 1. 等input框渲染出来 mounted 钩子

    // 2. 让input框获取焦点 inp.focus()

    mounted () {

      document.querySelector('#inp').focus()

    }

3.综合案例-小黑记账清单

第三天课程上午_第3张图片 (1)基本渲染

* 1. 基本渲染
       *    (1) 立刻发送请求获取数据 created
       *    (2) 拿到数据,存到data的响应式数据中
       *    (3) 结合数据,进行渲染 v-for
       *    (4) 消费统计 => 计算属性
       * 2. 添加功能
       * 3. 删除功能
       * 4. 饼图渲染
       */
      const app = new Vue({
        el: '#app',
        data: {
          list: []
        },
        computed: {
          totalPrice () {
            return this.list.reduce((sum, item) => sum + item.price, 0)
          }
        },
        async created () {
          const res = await axios.get('https://applet-base-api-t.itheima.net/bill', {
            params: {
              creator: '小黑'
            }
          })
          this.list = res.data.data
        }
      })

 (2)添加功能

          
        methods: {
          async getList () {
            const res = await axios.get('https://applet-base-api-t.itheima.net/bill', {
              params: {
                creator: '小黑'
              }
            })
            this.list = res.data.data
          },
          async add () {
            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
            })
            // 重新渲染一次
            this.getList()

            this.name = ''
            this.price = ''
          }
        }

(3)删除功能

       * 3. 删除功能

       *    (1) 注册点击事件,传参传 id

       *    (2) 根据 id 发送删除请求

       *    (3) 需要重新渲染

              
                {{ index + 1 }}
                {{ item.name }}
                {{ item.price.toFixed(2) }}
                删除
              
          async del (id) {
            // 根据 id 发送删除请求
            const res = await axios.delete(`https://applet-base-api-t.itheima.net/bill/${id}`)
            // 重新渲染
            this.getList()
          }

(4)饼图渲染

       * 4. 饼图渲染

       *    (1) 初始化一个饼图 echarts.init(dom)  mounted钩子实现

       *    (2) 根据数据实时更新饼图 echarts.setOption({ ... })

 要用到插件

        mounted () {
          this.myChart = echarts.init(document.querySelector('#main'))
          this.myChart.setOption({
            // 大标题
            title: {
              text: '消费账单列表',
              left: 'center'
            },
            // 提示框
            tooltip: {
              trigger: 'item'
            },
            // 图例
            legend: {
              orient: 'vertical',
              left: 'left'
            },
            // 数据项
            series: [
              {
                name: '消费账单',
                type: 'pie',
                radius: '50%', // 半径
                data: [
                  // { value: 1048, name: '球鞋' },
                  // { value: 735, name: '防晒霜' }
                ],
                emphasis: {
                  itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              }
            ]
          })
        },

你可能感兴趣的:(黑马VUE,vue)