兼容性複製功能/自定義mock數據/通用hook

*****自定義mock

const resourceList = computed(() =>
  Array.from({ length: 20 }, (_, index) => index).map((v, i) => {
    return 
       {
          id: i,
          joinList: Array.from({ length: i }, (_, index1) => index1).map(
            (v, index2) => `我真的想喫燒烤${index2}`
          ),
        } 
  })
);

*****通用複製功能

export const copyToClipboard = async (textToCopy) => {
  try {
    await navigator.clipboard.writeText(textToCopy);
    return Promise.resolve();
  } catch (err) {
    // 如果瀏覽器不支持Clipboard API,使用document.execCommand('copy')作爲備選方案
    const textArea = document.createElement("textarea");
    textArea.value = textToCopy;
    document.body.appendChild(textArea);
    textArea.select();
    document.execCommand("copy");
    document.body.removeChild(textArea);
    return Promise.resolve();
  }
};

使用

const handleCopy = async ( ) => {  
  copyToClipboard(`
  我真的想喫燒烤: \n
  hhhhh
  `)
    .then(() => {
      Message.success(`複製成功`);
    })
    .catch((error) => {
      console.error("複製到剪貼板時出錯:", error);
    });
};

當前時間
export const currentTime = () => {
  const currentDate = new Date();
  const currentYear = currentDate.getFullYear();
  const currentMonth = currentDate.getMonth() + 1; // 月份是從0開始的,所以要加1
  const currentDay = currentDate.getDate();
  const currentHour = currentDate.getHours();
  const currentMinute = currentDate.getMinutes();
  const currentSecond = currentDate.getSeconds();
  // 年月日時分秒
  const allTime = `${currentYear}-${currentMonth}-${currentDay} ${currentHour}:${currentMinute}:${currentSecond}`;
  const onlyToDay = `${currentYear}-${currentMonth}-${currentDay}`;
  const onlyToSecond = `${currentHour}:${currentMinute}:${currentSecond}`;
  return {
    currentYear,
    currentMonth,
    currentDay,
    currentHour,
    currentMinute,
    currentSecond,
    allTime,
    onlyToDay,
    onlyToSecond,
  };
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章