VUE2.0 global method registration

vue2.0 global method registration

The previous article briefly recorded the time format method. If it is registered as a global method, it can be called at any time in the project.

1. Create a new common.js file:
Create a new js file, define functions and expose them.

function formatDate(date, format) {
    let yFull = date.getFullYear();
    let y = date.getYear();
    let m = date.getMonth() + 1;
    let d = date.getDate();
    let time = '';
    if (format == 'YY/MM/DD') {
        time = y + '/' + m + '/' + d;
    } else if (format == 'YYYY/MM/DD') {
        time = yFull + '/' + m + '/' + d;
    } else if (format == 'YY-MM-DD') {
        time = y + '-' + m + '-' + d;
    } else if (format == 'YYYY-MM-DD') {
        time = yFull + '-' + m + '-' + d;
    } else {
        time = yFull + '-' + m + '-' + d;
    }
    return time
}

export default { formatDate }

2. Introduce and mount in the prototype in the min.js file:

import formatDate from './utils/common'

Vue.prototype.Common = formatDate;

3. Call in other components:

this.Common.formatDate(new Date(), 'YYYY-MM-DD');

If it is helpful to you, remember to like it (~~)

Guess you like

Origin blog.csdn.net/start_sea/article/details/122300322