1.check-type-plugin.js
function getPrototype(data) {
return Object.prototype.toString.call(data).toLowerCase();
}
const Types = {
/**
* 返回传入数据的类型
*
* @param {*} data
* @returns
*/
isArray(data) {
return getPrototype(data) === '[object array]';
},
isJSON(data) {
return getPrototype(data) === '[object object]';
},
isFunction(data) {
return getPrototype(data) === '[object function]';
},
isString(data) {
return getPrototype(data) === '[object string]';
},
isNumber(data) {
return getPrototype(data) === '[object number]';
},
isUndefined(data) {
return getPrototype(data) === '[object undefined]';
},
isNull(data) {
return getPrototype(data) === '[object null]';
},
};
const CheckType = {
install(Vue) {
// 为 Vue 绑定私有的 $_isArray 等方法
Object.keys(Types).forEach((key) => {
// eslint-disable-next-line no-param-reassign
Vue.prototype[`$_${key}`] = Types[key];
});
},
};
export default CheckType;
2.main.js
import CheckType from './check-type-plugin.js'
Vue.use(CheckType)