从安
2019-06-21
来源 :
阅读 1673
评论 0
摘要:本篇文章主要讲述JavaScript之轻松掌握vue按需加载组件、异步组件,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。
本篇文章主要讲述JavaScript之轻松掌握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 },本文由职坐标整理发布,学习更多的相关知识,请关注职坐标IT知识库!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号