I found a problem during the project iteration today.
Because the back end returned the update time this field is a timestamp, thinking that there will be a lot of places to use, directly encapsulated a public method out.
The timestamp is converted to time in the format of year-month-day hour: minute: second
// SEC turn 2021-03-05 12:11:30 export const transFormTime = (timestamp) => {if (timestamp) {const time = new Date(timestamp); const Y = time.getFullYear(); const M = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1; const D = time.getDate() < 10 ? '0' + time.getDate() : time.getDate(); const h = time.getHours() < 10 ? '0' + time.getHours() : time.getHours(); const m = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(); const s = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds(); return `${Y}-${M}-${D} ${h}:${m}:${s}`; } else { return ''; }};Copy the code
Introduce this method on pages that require time conversion as follows:
Use this method where the conversion is required, as follows:
The result is an error that does not find the transFormTime method
Then according to the previous experience to check the introduction of the path, method name is determined to have no problem. I then tried calling this method at page initialization and found no errors and returned the expected results. Note that methods introduced by JS cannot be used directly in HTML.
Finally, we try to rewrite a method in methods, call the transFormTime method introduced by JS in the method, and call the method in HTML, and find that it is effective.
Through Baidu to find an explanation, said very good, also put the link here: the original link