zoukankan      html  css  js  c++  java
  • vue按需加载组件,异步组件

    说实话,我一开始也不知道什么叫按需加载组件,组件还可以按需加载???后来知道了

    学不完啊...没关系,看我的

    按需加载组件,或者异步组件,主要是应用了component的 is 属性

    template中的代码:

    这里的每一个按钮,都要显示不同的组件,所以我让他们使用了同一个方法名

     1 <template slot-scope="scope">
     2             <el-button
     3               type="text"
     4               size="mini"
     5               @click="handleSchedule('CustomerInfoSchedule', scope.row.customer_id)"
     6             >详情</el-button>
     7             <el-button
     8               type="text"
     9               size="mini"
    10               @click="handleSchedule('VisitRecordSchedule', scope.row.customer_id)"
    11             >回访</el-button>
    12             <el-button
    13               type="text"
    14               size="mini"
    15               @click="handleSchedule('AddCustomerSchedule',scope.row.customer_id)"
    16             >编辑</el-button>
    17             <el-button
    18               type="text"
    19               size="mini"
    20               @click="handleSchedule('AddPeopleSchedule', scope.row.customer_id)"
    21             >添加联系人</el-button>
    22           </template>
    1 <component 
    2  :is="currentComponent" 
    3  :customer_id="customer_id" 
    4  @componentResult="componentResult"
    5 >
    6 </component>

    script中的代码:

    这里的组件使用request按需引入,我使用的点击事件,当事件触发的时候,引入对应的组件

    首先在data中声明组件的属性

    1 data() {
    2   return {
    3       currentComponent: "",
    4       customer_id:'',
    5    }
    6 }

    然后注册组件

    这里的组件作为一个个方法,组件名是方法名,组件内容是方法体,有几个组件就写几个方法

     1 components: {
     2     AddCustomerSchedule(resolve) {
     3       require(["../components/AddCustomer"], resolve);
     4     },
     5     AddPeopleSchedule(resolve) {
     6       require(["../components/AddPeople"], resolve);
     7     },
     8     CustomerInfoSchedule(resolve) {
     9       require(["../components/CustomerInfo"], resolve);
    10     },
    11     VisitRecordSchedule(resolve) {
    12       require(["../components/VisitRecord"], resolve);
    13     },
    14   },

    定义的方法

     1 // 这里直接接收name,然后相对应的引入组件,同时传值
     2 handleSchedule(name, id) {
     3       this.customer_id = id;
     4       this.currentComponent = name;
     5     },
     6 // 这是子组件触发父组件返回回来的方法,因为我的组件都是弹出框
     7 // 所以在子组件关闭弹出框的时候,我让this.currentComponent为空
     8 // 同时可以选择性的刷新数据
     9     componentResult(type) {
    10       if (type == "upData") {
    11         this.getTableData();
    12       } else {
    13         this.currentComponent = "";
    14       }
    15     },
  • 相关阅读:
    Ubuntu 网络代理配置
    WSL2 环境配置
    两台笔记本电脑实现同一wifi下虚拟主机网络实现互通
    Linux /dev/loop0文件详解
    Excel两张表查重,返回True
    计算机网络基础/进制转换/企业级子网IP划分
    leetcode 2030. 含特定字母的最小子序列
    nginx https
    kubernetes(二十四)ingress https
    求两个向量的旋转矩阵 E
  • 原文地址:https://www.cnblogs.com/jun-qi/p/10931205.html
Copyright © 2011-2022 走看看