• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

app.vue组件如何按需加载?

vue 搞代码 4年前 (2021-12-26) 26次浏览 已收录 0个评论


app.vue组件如何按需加载?

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

1、template中的代码:

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

<template slot-scope="scope">
	<el-button
                type="text"
               size="mini"
              @click="handleSchedule('CustomerInfoSchedule', scope.row.customer_id)"
            >详情</el-button>
	<el-button
             type="text"
              size="mini"
              @click="handleSchedule('VisitRecordSchedule', scope.row.customer_id)"
             >回访</el-button>
	<el-button
               type="text"
          size="mini"
            @click="handleSchedule('AddCustomerSchedule',scope.row.customer_id)"
            >编辑</el-button>
	<el-button
              type="text"
           size="mini"
            @click="handleSchedule('AddPeopleSchedule', scope.row.customer_id)"
             >添加联系人</el-button>
</template>
<component 
  :is="currentComponent" 
  :customer_id="customer_id" 
  @componentResult="componentResult"
 ></component>

2、script中的代码:

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

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

data() {
  return {
      currentComponent: "",
      customer_id:'',
   }
}

3、然后注册组件

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

components: {
   AddCustomerSchedule(resolve) {
        require(["../components/AddCustomer"], resolve);
      },
      AddPeople<strong style="color:transparent">来源gaodai#ma#com搞@代~码网</strong>Schedule(resolve) {
        require(["../components/AddPeople"], resolve);
      },
      CustomerInfoSchedule(resolve) {
        require(["../components/CustomerInfo"], resolve);
     },
     VisitRecordSchedule(resolve) {
       require(["../components/VisitRecord"], resolve);
    },
   },

4、定义的方法

 // 这里直接接收name,然后相对应的引入组件,同时传值
 handleSchedule(name, id) {
       this.customer_id = id;
       this.currentComponent = name;
     },
// 这是子组件触发父组件返回回来的方法,因为我的组件都是弹出框
 // 所以在子组件关闭弹出框的时候,我让this.currentComponent为空
 // 同时可以选择性的刷新数据
     componentResult(type) {
       if (type == "upData") {
         this.getTableData();
       } else {
         this.currentComponent = "";
      }
     },

更多Vue.js相关技术文章,请访问Vue.js答疑栏目进行学习!

以上就是app.vue组件如何按需加载?的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:app.vue组件如何按需加载?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址