帖子
帖子
用户
博客
课程
123下一页
返回列表 发新帖
显示全部楼层
100
帖子
2
勋章
1960
Y币

[多端开发] 子组件内用了3层循环后70*12*365次之后,巨卡无比???

[复制链接]
发表于 2022-11-17 11:41:39
本帖最后由 ColdKiller 于 2022-11-21 10:40 编辑

子组件内用了3层循环后,当引用该组件的stml页面打开之后,页面巨卡无比,有时候会导致APP闪退。该子组件calendar-date定义内容如下:

  1. <template name="calendar-date">
  2.         <view class="select-date">
  3.                 <text style="display:none;">{{getDate}}</text>
  4.                 <a-cell :__last="__last" :title="title" class="a-field-center" :required="required" center
  5.                         placeholder={selectDate?'':placeholder} value={selectDate?selectDate:placeholder} @click="chooseDate()" />
  6.                 <text class="list-item-right" @click="clearDate()" v-if="selectDate && clearAble">清空</text>
  7.         </view>
  8. </template>

  9. <script>
  10. import { syncModel } from "./avm-ui/utils";
  11. export default {
  12.         name: 'calendar-date',
  13.         install() {
  14.                 if (this.props["$value"]) {
  15.                         syncModel.call(this);
  16.                 }
  17.                 this.initCanlendarData();
  18.         },
  19.         computed: {
  20.                 getDate() {
  21.                         if (this.value) {
  22.                                 this.data.selectDate = this.value;
  23.                                 this.initDateIndex();
  24.                         } else {
  25.                                 this.data.selectDate = "";
  26.                         }
  27.                         return this.value
  28.                 },
  29.         },
  30.         props: {
  31.                 value: {
  32.                         default: () => ""
  33.                 },
  34.                 title: {
  35.                         default: () => ""
  36.                 },
  37.                 required: {
  38.                         default: () => false
  39.                 },
  40.                 __last: {
  41.                         default: () => false
  42.                 },
  43.                 placeholder: {
  44.                         default: () => ""
  45.                 },
  46.                 clearAble: {
  47.                         default: () => false
  48.                 },
  49.                 readonly: {
  50.                         default: () => false
  51.                 },
  52.         },
  53.         data() {
  54.                 return {
  55.                         selectDate: "",
  56.                         calendarData: [],
  57.                         actives: [0, 0, 0]
  58.                 }
  59.         },
  60.         methods: {
  61.                 initDateIndex() {
  62.                         let myDate = new Date();
  63.                         let nowYear = myDate.getFullYear();
  64.                         let year_index = 0;
  65.                         let month_index = 0;
  66.                         let day_index = 0;
  67.                         if (this.data.selectDate) {
  68.                                 let tmpDate = new Date(this.data.selectDate);
  69.                                 let tmpYear = tmpDate.getFullYear();
  70.                                 let tmpMonth = tmpDate.getMonth() + 1;
  71.                                 let tmpDay = tmpDate.getDate();
  72.                                 year_index = nowYear - tmpYear;
  73.                                 month_index = tmpMonth - 1;
  74.                                 day_index = tmpDay - 1;
  75.                         } else {
  76.                                 let tmpDate = new Date(myDate);
  77.                                 let tmpYear = tmpDate.getFullYear();
  78.                                 let tmpMonth = tmpDate.getMonth() + 1;
  79.                                 let tmpDay = tmpDate.getDate();
  80.                                 year_index = nowYear - tmpYear;
  81.                                 month_index = tmpMonth - 1;
  82.                                 day_index = tmpDay - 1;
  83.                         }
  84.                         this.data.actives = [year_index, month_index, day_index];
  85.                 },
  86.                 initCanlendarData() {
  87.                         var dateArr = [];
  88.                         let myDate = new Date();
  89.                         let nowYear = myDate.getFullYear();
  90.                         for (let year = nowYear; year > 1950; year--) {
  91.                                 let tmp_year = {
  92.                                         "name": year + "年",
  93.                                         "title": year,
  94.                                         "sub": []
  95.                                 };
  96.                                 let dateMonth = 12;
  97.                                 if (year == nowYear) {
  98.                                         dateMonth = (myDate.getMonth() + 1);
  99.                                 }
  100.                                 for (let month = 1; month <= dateMonth; month++) {
  101.                                         let tmp_month = {
  102.                                                 "name": month + "月",
  103.                                                 "title": month,
  104.                                                 "sub": []
  105.                                         };
  106.                                         let days = this.getDayNumByYearMoth(year, month);
  107.                                         for (let day = 1; day <= days; day++) {
  108.                                                 let tmp_day = {
  109.                                                         "name": day + "日",
  110.                                                         "title": day,
  111.                                                         "sub": []
  112.                                                 };
  113.                                                 tmp_month.sub.push(tmp_day);
  114.                                         }
  115.                                         tmp_year.sub.push(tmp_month);
  116.                                 }
  117.                                 dateArr.push(tmp_year);
  118.                         }
  119.                         this.data.calendarData = dateArr;
  120.                 },
  121.                 //判断是否为闰年
  122.                 isLeapYear(year) {
  123.                         if ((year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0)) {
  124.                                 return true
  125.                         } else {
  126.                                 return false
  127.                         }
  128.                 },
  129.                 //计算每个月的天数
  130.                 getDayNumByYearMoth(year, month) {
  131.                         let day = 1;
  132.                         switch (month) {
  133.                                 case 1:
  134.                                 case 3:
  135.                                 case 5:
  136.                                 case 7:
  137.                                 case 8:
  138.                                 case 10:
  139.                                 case 12:
  140.                                         day = 31;
  141.                                         break
  142.                                 case 4:
  143.                                 case 6:
  144.                                 case 9:
  145.                                 case 11:
  146.                                         day = 30;
  147.                                         break;
  148.                                 case 2:
  149.                                         day = this.isLeapYear(year) ? 29 : 28;
  150.                                         break;
  151.                                 default:
  152.                         }
  153.                         return day
  154.                 },
  155.                 chooseDate() {
  156.                         if (this.readonly) {
  157.                                 return
  158.                         }
  159.                         let that = this;
  160.                         var UIActionSelector = api.require('UIActionSelector');
  161.                         UIActionSelector.open({
  162.                                 datas: that.data.calendarData,
  163.                                 layout: {
  164.                                         row: 5,
  165.                                         col: 3,
  166.                                         height: 30,
  167.                                         size: 16,
  168.                                         sizeActive: 18,
  169.                                         rowSpacing: 5,
  170.                                         colSpacing: 10,
  171.                                         maskBg: 'rgba(0,0,0,0.2)',
  172.                                         bg: '#fff',
  173.                                         color: '#888',
  174.                                         colorActive: '#333',
  175.                                         colorSelected: '#333'
  176.                                 },
  177.                                 animation: true,
  178.                                 actives: that.data.actives,
  179.                                 cancel: {
  180.                                         text: '取消',
  181.                                         size: 18,
  182.                                         w: 90,
  183.                                         h: 35,
  184.                                         bg: '#fff',
  185.                                         bgActive: '#ccc',
  186.                                         color: '#8B0000',
  187.                                         colorActive: '#fff'
  188.                                 },
  189.                                 ok: {
  190.                                         text: '确定',
  191.                                         size: 18,
  192.                                         w: 90,
  193.                                         h: 35,
  194.                                         bg: '#fff',
  195.                                         bgActive: '#ccc',
  196.                                         color: '#1E90FF',
  197.                                         colorActive: '#fff'
  198.                                 },
  199.                                 title: {
  200.                                         text: that.props.placeholder,
  201.                                         size: 18,
  202.                                         h: 44,
  203.                                         bg: '#fff',
  204.                                         color: '#888'
  205.                                 },
  206.                                 fixedOn: api.frameName
  207.                         }, function (ret, err) {
  208.                                 if (ret && ret.eventType == "ok" && ret.selectedInfo) {
  209.                                         let year = ret.selectedInfo[0].title;
  210.                                         let month = ret.selectedInfo[1].title;
  211.                                         if (month && month.toString().length == 1) {
  212.                                                 month = "0" + month;
  213.                                         }
  214.                                         let day = ret.selectedInfo[2].title;
  215.                                         if (day && day.toString().length == 1) {
  216.                                                 day = "0" + day;
  217.                                         }
  218.                                         let date = year + "-" + month + "-" + day;
  219.                                         that.data.selectDate = date;
  220.                                         that.setValue(date);
  221.                                         that.hideDate();
  222.                                         that.fire('selectDate', {
  223.                                                 date: date
  224.                                         });
  225.                                 }
  226.                         });
  227.                 },
  228.                 showDate() {
  229.                         var UIActionSelector = api.require('UIActionSelector');
  230.                         UIActionSelector.show();
  231.                 },
  232.                 hideDate() {
  233.                         var UIActionSelector = api.require('UIActionSelector');
  234.                         UIActionSelector.hide();
  235.                 },
  236.                 setValue(value) {
  237.                         if (this.$model) {
  238.                                 this.$model.value = value;
  239.                         }
  240.                 },
  241.                 clearDate() {
  242.                         this.data.selectDate = "";
  243.                         this.setValue("");
  244.                         this.fire('selectDate', {
  245.                                 date: ""
  246.                         });
  247.                 }
  248.         }
  249. }
  250. </script>
  251. <style scoped>
  252. .select-date {
  253.         flex-flow: row;
  254.         align-items: center;
  255.         align-content: space-between;
  256. }
  257. .list-item-right {
  258.         flex: 1;
  259.         font-size: 14px;
  260.         color: #3c77fe;
  261.         margin-left: auto;
  262.         padding-left: 5px;
  263.         width: 100%;
  264.         box-sizing: border-box;
  265.         border: none;
  266.         text-align: left;
  267. }
  268. </style>
复制代码


您好,是实际场景需要循环这么多次吗,这情况可能循环次数多,会造成卡顿
100
帖子
2
勋章
1960
Y币
技术咨询-特特 · 2022-11-17 12:45您好,是实际场景需要循环这么多次吗,这情况可能循环次数多,会造成卡顿

日期选择范围默认1950-2022,循环次数才306,600次,就卡顿的不行,请问下有什么好的解决方案么
380
帖子
4
勋章
6
Y币
ColdKiller · 2022-11-17 15:42日期选择范围默认1950-2022,循环次数才306,600次,就卡顿的不行,请问下有什么好的解决方案么

不引组件,单页面使用卡顿吗
0
帖子
2
勋章
1025
Y币
ColdKiller · 2022-11-17 15:42日期选择范围默认1950-2022,循环次数才306,600次,就卡顿的不行,请问下有什么好的解决方案么

你好,你现在提供的页面代码里引用了js文件和其他自定义组件,但看你这个页面无法做进一步的分析,请把你的完整项目压缩一个zip包发上来,我们这边好进行进一步的分析,帮你去解决本次反馈的问题。
100
帖子
2
勋章
1960
Y币
技术咨询-F · 2022-11-17 16:17不引组件,单页面使用卡顿吗

也卡顿的,请问下这是渲染机制的问题么
100
帖子
2
勋章
1960
Y币
至高吾·尚 · 2022-11-17 18:36你好,你现在提供的页面代码里引用了js文件和其他自定义组件,但看你这个页面无法做进一步的分析,请把你的完整项目压缩一个zip包发上来,我们这边好进行进一步的分析,帮你去解决本次反馈的问题。 ...

现在的问题是单页面内使用,不在组件内使用,也是卡顿的很
380
帖子
4
勋章
6
Y币
ColdKiller · 2022-11-21 09:20也卡顿的,请问下这是渲染机制的问题么

可能嵌套标签太多层了吧,文档示例代码也是三级选择器,就没卡顿
0
帖子
2
勋章
1025
Y币
ColdKiller · 2022-11-21 09:20现在的问题是单页面内使用,不在组件内使用,也是卡顿的很

你好,这样的虚对虚的沟通,很难帮你解决问题。建议你提供一下你的源码(如果项目敏感,可以写一个简单的demo),将整个demo的widget压缩成zip文件发出来就可以,我们技术这边可以运行一下demo,然后去进一步的定位帮你解决问题。
100
帖子
2
勋章
1960
Y币
至高吾·尚 · 2022-11-21 23:49你好,这样的虚对虚的沟通,很难帮你解决问题。建议你提供一下你的源码(如果项目敏感,可以写一个简单的demo),将整个demo的widget压缩成zip文件发出来就可以,我们技术这边可以运行一下demo,然后去进一步的定位 ...

已简化demo上传,请技术人员帮忙看下啥问题,万分感谢~~~~
AvmTest.doc 下载附件
2022-11-22 19:17上传7.01 MB下载次数: 46
123下一页
您需要登录后才可以回帖 登录

本版积分规则