vue 中js方法封装
1.在src下创建utils文件里面创建tool.js文件显示如下
2.tool.js封装对应方法如下:
export default { fun(num) { num++; return num; }, hello(obj) { obj--; return obj; } }
3.在main.js引入封装的方法
import tool from "./utils/tool"; // 1.先引入 Vue.prototype.$tool = tool; // 2.直接定义在vue的原型上面
4.在页面中使用即可
let result = this.$tool.hello(7); console.log(result); // 8