|
[多端开发]
子组件内用了3层循环后70*12*365次之后,巨卡无比???
[复制链接]
本帖最后由 ColdKiller 于 2022-11-21 10:40 编辑
子组件内用了3层循环后,当引用该组件的stml页面打开之后,页面巨卡无比,有时候会导致APP闪退。该子组件calendar-date定义内容如下:
- <template name="calendar-date">
- <view class="select-date">
- <text style="display:none;">{{getDate}}</text>
- <a-cell :__last="__last" :title="title" class="a-field-center" :required="required" center
- placeholder={selectDate?'':placeholder} value={selectDate?selectDate:placeholder} @click="chooseDate()" />
- <text class="list-item-right" @click="clearDate()" v-if="selectDate && clearAble">清空</text>
- </view>
- </template>
-
- <script>
- import { syncModel } from "./avm-ui/utils";
- export default {
- name: 'calendar-date',
- install() {
- if (this.props["$value"]) {
- syncModel.call(this);
- }
- this.initCanlendarData();
- },
- computed: {
- getDate() {
- if (this.value) {
- this.data.selectDate = this.value;
- this.initDateIndex();
- } else {
- this.data.selectDate = "";
- }
- return this.value
- },
- },
- props: {
- value: {
- default: () => ""
- },
- title: {
- default: () => ""
- },
- required: {
- default: () => false
- },
- __last: {
- default: () => false
- },
- placeholder: {
- default: () => ""
- },
- clearAble: {
- default: () => false
- },
- readonly: {
- default: () => false
- },
- },
- data() {
- return {
- selectDate: "",
- calendarData: [],
- actives: [0, 0, 0]
- }
- },
- methods: {
- initDateIndex() {
- let myDate = new Date();
- let nowYear = myDate.getFullYear();
- let year_index = 0;
- let month_index = 0;
- let day_index = 0;
- if (this.data.selectDate) {
- let tmpDate = new Date(this.data.selectDate);
- let tmpYear = tmpDate.getFullYear();
- let tmpMonth = tmpDate.getMonth() + 1;
- let tmpDay = tmpDate.getDate();
- year_index = nowYear - tmpYear;
- month_index = tmpMonth - 1;
- day_index = tmpDay - 1;
- } else {
- let tmpDate = new Date(myDate);
- let tmpYear = tmpDate.getFullYear();
- let tmpMonth = tmpDate.getMonth() + 1;
- let tmpDay = tmpDate.getDate();
- year_index = nowYear - tmpYear;
- month_index = tmpMonth - 1;
- day_index = tmpDay - 1;
- }
- this.data.actives = [year_index, month_index, day_index];
- },
- initCanlendarData() {
- var dateArr = [];
- let myDate = new Date();
- let nowYear = myDate.getFullYear();
- for (let year = nowYear; year > 1950; year--) {
- let tmp_year = {
- "name": year + "年",
- "title": year,
- "sub": []
- };
- let dateMonth = 12;
- if (year == nowYear) {
- dateMonth = (myDate.getMonth() + 1);
- }
- for (let month = 1; month <= dateMonth; month++) {
- let tmp_month = {
- "name": month + "月",
- "title": month,
- "sub": []
- };
- let days = this.getDayNumByYearMoth(year, month);
- for (let day = 1; day <= days; day++) {
- let tmp_day = {
- "name": day + "日",
- "title": day,
- "sub": []
- };
- tmp_month.sub.push(tmp_day);
- }
- tmp_year.sub.push(tmp_month);
- }
- dateArr.push(tmp_year);
- }
- this.data.calendarData = dateArr;
- },
- //判断是否为闰年
- isLeapYear(year) {
- if ((year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0)) {
- return true
- } else {
- return false
- }
- },
- //计算每个月的天数
- getDayNumByYearMoth(year, month) {
- let day = 1;
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- day = 31;
- break
- case 4:
- case 6:
- case 9:
- case 11:
- day = 30;
- break;
- case 2:
- day = this.isLeapYear(year) ? 29 : 28;
- break;
- default:
- }
- return day
- },
- chooseDate() {
- if (this.readonly) {
- return
- }
- let that = this;
- var UIActionSelector = api.require('UIActionSelector');
- UIActionSelector.open({
- datas: that.data.calendarData,
- layout: {
- row: 5,
- col: 3,
- height: 30,
- size: 16,
- sizeActive: 18,
- rowSpacing: 5,
- colSpacing: 10,
- maskBg: 'rgba(0,0,0,0.2)',
- bg: '#fff',
- color: '#888',
- colorActive: '#333',
- colorSelected: '#333'
- },
- animation: true,
- actives: that.data.actives,
- cancel: {
- text: '取消',
- size: 18,
- w: 90,
- h: 35,
- bg: '#fff',
- bgActive: '#ccc',
- color: '#8B0000',
- colorActive: '#fff'
- },
- ok: {
- text: '确定',
- size: 18,
- w: 90,
- h: 35,
- bg: '#fff',
- bgActive: '#ccc',
- color: '#1E90FF',
- colorActive: '#fff'
- },
- title: {
- text: that.props.placeholder,
- size: 18,
- h: 44,
- bg: '#fff',
- color: '#888'
- },
- fixedOn: api.frameName
- }, function (ret, err) {
- if (ret && ret.eventType == "ok" && ret.selectedInfo) {
- let year = ret.selectedInfo[0].title;
- let month = ret.selectedInfo[1].title;
- if (month && month.toString().length == 1) {
- month = "0" + month;
- }
- let day = ret.selectedInfo[2].title;
- if (day && day.toString().length == 1) {
- day = "0" + day;
- }
- let date = year + "-" + month + "-" + day;
- that.data.selectDate = date;
- that.setValue(date);
- that.hideDate();
- that.fire('selectDate', {
- date: date
- });
- }
- });
- },
- showDate() {
- var UIActionSelector = api.require('UIActionSelector');
- UIActionSelector.show();
- },
- hideDate() {
- var UIActionSelector = api.require('UIActionSelector');
- UIActionSelector.hide();
- },
- setValue(value) {
- if (this.$model) {
- this.$model.value = value;
- }
- },
- clearDate() {
- this.data.selectDate = "";
- this.setValue("");
- this.fire('selectDate', {
- date: ""
- });
- }
- }
- }
- </script>
- <style scoped>
- .select-date {
- flex-flow: row;
- align-items: center;
- align-content: space-between;
- }
- .list-item-right {
- flex: 1;
- font-size: 14px;
- color: #3c77fe;
- margin-left: auto;
- padding-left: 5px;
- width: 100%;
- box-sizing: border-box;
- border: none;
- text-align: left;
- }
- </style>
复制代码
|
|