• vue快速入门(四十四)自定义组件


    注释很详细,直接上代码

    上一篇

    新增内容

    1. 全局注册自定义组件并应用
    2. 局部注册自定义组件并应用

    此篇使用了axios模块没有安装导入的先看这一篇

    axios模块下载与导入

    源码

    main.js

    import Vue from 'vue'
    import App from './App.vue'
    
    //全局引入axios
    // 引入axios
    import axios from 'axios';
    // 挂载到vue原型链上
    Vue.prototype.axios = axios;
    
    Vue.config.productionTip = false
    
    //全局注册指令(自动获取焦点)
    Vue.directive('focus', {
      // ele:绑定的元素(操作节点)
      // obj:指令的绑定对象(获取属性)
      
      bind(ele,obj){//只执行一次;DOM渲染之前执行,可以进行样式操作
      },
      
      inserted(ele,obj){//只执行一次,DOM渲染之后执行,可以进行行为操作
        ele.focus()// 聚焦元素
        console.log(obj)
      },
      
      update(ele,obj){//数据更新后执行
      },
    
      componentUpdated(ele,obj){//父子组件都更新后执行
      },
      
      unbind(ele,obj){//只执行一次,指令与元素解绑时执行
      }
    })
    
    new Vue({
      render: h => h(App),
    }).$mount('#app')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    App.vue

    <template>
      <div id="app">
        
        <input type="text" v-focus>
        <TestComponent/>
      div>
    template>
    <script>
    import TestComponent from "./components/TestComponent.vue";
    export default {
      name: "App",
      components: {
       TestComponent
      },
      data() {
        return {
        };
      },
      methods: {
      }
    };
    script>
    <style>style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    TestComponent.vue

    <template>
      <div class="main">
        <div class="box">
          <ul v-loading="list.length">
            <li v-for="item 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>
      div>
    template>
    
    <script>
    
    //局部引入axios
    //  import axios from 'axios'
    
    export default {
      data() {
        return {
          list: [],
        };
      },
      async created() {
        // 1. 发送请求获取数据
        const res = await this.axios.get("http://hmajax.itheima.net/api/news");
    
        setTimeout(() => {
          // 2. 更新到 list 中,用于页面渲染 v-for
          this.list = res.data.data;
        }, 1000);
      },
      // 自定义指令
      directives: {
        loading: {
          inserted(ele, obj) {
            //刷新后立即判断
            //如果数据长度不为零则表示加载完毕,可以去除loading的类名
            obj.value <= 0
              ? ele.classList.add("loading")
              : ele.classList.remove("loading");
          },
          update(ele, obj) {
            //数据改变后判断
            obj.value <= 0
              ? ele.classList.add("loading")
              : ele.classList.remove("loading");
          },
        },
      },
    };
    script>
    
    <style>
    /* 使用伪类覆盖的方法 */
    .loading:before {
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-size: cover;
      background: #fff url("../../imgs/loading.gif") no-repeat center;
    }
    
    /* 下面不是重点 */
    .box {
      width: 800px;
      min-height: 500px;
      border: 3px solid orange;
      border-radius: 5px;
      position: relative;
    }
    .news {
      display: flex;
      height: 120px;
      width: 600px;
      margin: 0 auto;
      padding: 20px 0;
      cursor: pointer;
    }
    .news .left {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding-right: 10px;
    }
    .news .left .title {
      font-size: 20px;
    }
    .news .left .info {
      color: #999999;
    }
    .news .left .info span {
      margin-right: 20px;
    }
    .news .right {
      width: 160px;
      height: 120px;
    }
    .news .right img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118

    效果演示:

    在这里插入图片描述

  • 相关阅读:
    VsFtpd的环境搭建,虚拟登录,Linux服务器
    Docker安装
    java计算机毕业设计开放式教学评价管理系统源码+mysql数据库+系统+lw文档+部署
    linux(rhel7)内核参数优化
    Docker ------compose概述与简单编排部署
    springboot2自动加载sql文件
    使用VMware 16 安装中标麒麟 7 --九五小庞
    线程池的使用及ThreadPoolExecutor源码分析
    【博客472】k8s中如何使用shared memory
    插入排序算法(Python版)
  • 原文地址:https://blog.csdn.net/m0_73756108/article/details/138036897