vue按需加载组件,异步组件

叁歲伎倆 2021-11-16 11:18 563阅读 0赞

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

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

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

template中的代码:

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

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

script中的代码:

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

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

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

然后注册组件

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

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

定义的方法

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

转载于:https://www.cnblogs.com/jun-qi/p/10931205.html

发表评论

表情:
评论列表 (有 0 条评论,563人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue.js之组件异步

    当项目中组件特别多的时候,通过webpack打包的组件非常多,如果在访问其中某一个路由对应的组件时,加载了所有组件的文件,对于性能的消耗是非常浪费的。 此时,我们就需要使用

    相关 react之组件异步

    我们在用react的时候,希望组件异步加载,提高性能,那么应该怎么实现呢? 往下看。。。 解释一波: react异步加载的方法很多,这里只说一种,因为这一种直接使用r