使用
- 创建src\assets\js\common.js文件
export default {
install(Vue) {
Vue.prototype.formatDate = function (date, format) {}
}}
formatDate为方法名
- 在main.js文件中进行全局引入
import common from './assets/js/common'
Vue.use(common);
- 使用
this.formatDate()
方法
注意
公共方法中存在异步
- 方法
Vue.prototype.getTestMan = async (post) => {
let list
await testManList({
pageNum: 1,
pageSize: 1000,
post: post,
}).then((res) => {
list = res.data.page?.records || []
});
console.log(list);
return list;
}
- 调用
this.getTestMan("5").then((res) => {
this.testManList = res;
});
- 总结
公共方法中同步执行,调用时使用异步操作