十五春会
十五春会
  • 发布:2024-11-21 09:57
  • 更新:2024-11-21 09:57
  • 阅读:22

【uniapp x】Expression 'xx' of type 'Any?' cannot be invoked as a function

分类:uni-app x

定义了一个方法,但是编译报错,应该怎么调整?试了很多种方法都不行

 function dateFormat(  
    fmt : string,  
    date : any  
  ) : string {  
    if (typeof date === 'string' && (date as string).length > 0) date = date.replace(/\-/g, '/')  
    const formatDate = typeof date === 'string' && (date as string).length > 0 ? new Date(date) : new Date();  
    const formatMap = {  
      'Y': (len : number) : string => formatDate.getFullYear().toString().slice(-len),  
      'M': (len : number) : string => (formatDate.getMonth() + 1).toString().padStart(len, '0'),  
      'D': (len : number) : string => formatDate.getDate().toString().padStart(len, '0'),  
      'H': (len : number) : string => formatDate.getHours().toString().padStart(len, '0'),  
      'n': (len : number) : string => formatDate.getMinutes().toString().padStart(len, '0'),  
      't': (len : number) : string => formatDate.getSeconds().toString().padStart(len, '0'),  
      'S': (len : number) : string => formatDate.getMilliseconds().toString().padStart(len, '0'),  

      'q': (len : number) : string => Math.ceil((formatDate.getMonth() + 1) / 3).toString().padStart(len, '0'),  
      'i': (len : number) : string => (formatDate.getHours() - 12).toString().padStart(len, '0'),  
      'd': (len : number) : string => len == 1 ? formatDate.getDay().toString() : len === 2 ?  
        `周${['日', '一', '二', '三', '四', '五', '六'][formatDate.getDay()]}` :  
        `星期${['天', '一', '二', '三', '四', '五', '六'][formatDate.getDay()]}`,  
    };  
    return fmt.replace(new RegExp(`[${UTSJSONObject.keys(formatMap).join('')}]+`, 'g'), (match, offset, string) : string => formatMap[match.substring(0, 1)](match.length));  
  };

方法说明:

const date = dateFormat('YYYY-MM-DD') // 2024-11-21  
const date1 = dateFormat('YY/MM/DD') // 24/11/21

报错
[plugin:uni:app-uts] 编译失败
09:35:04.829 ‌error: Expression 'formatMap[match.substring(0, 1)]' of type 'Any?' cannot be invoked as a function. The function 'invoke()' is not found‌
09:35:04.829 at components/ev-date-picker/ev-date-picker.uvue:68:122
09:35:04.829 66 | };
09:35:04.829 67 | console.log(typeof formatMap['d'])
09:35:04.829 68 | return fmt.replace(new RegExp([${UTSJSONObject.keys(formatMap).join('')}]+, 'g'), (match, offset, string):string => formatMapmatch.substring(0,1));
09:35:04.829 | ^
09:35:04.829 69 | };

2024-11-21 09:57 负责人:无 分享
已邀请:

要回复问题请先登录注册