utils文件夹是统一管理函数工具
在src目录下创建一个utils文件夹来统一存放函数工具
可以创建一个index.js文件,在index.js文件里写js函数
// index.js
// 方法一
// 验证是否为blob格式
export async function blobValidate(data) {
try {
const text = await data.text();
JSON.parse(text);
return false;
} catch (error) {
return true;
}
}
// 方法二
function test(){
alert("你好");
}
export default test;
//还可以写class类函数
class Person{
// 构造函数
constructor(x,y){
this.x = x;
this.y = y;
}
todoSome(){
alert(this.x,this.y);
}
}
export default Person;
在main.js文件,引入,并挂载在Vue实例上
//main.js
//引入
import {blobValidate test Person } from "@/utils/index";
// 全局方法挂载
Vue.prototype.blobValidate = blobValidate
Vue.prototype.test = test
Vue.prototype.Person = Person
页面如何使用
//页面
this.blobValidate(); //调用函数
this.test();
const test= new this.Person('haha','33'); //调用类js,先new,注意传参
test.todoSome(); //调用class类下面的方法
还可以把多个js函数文件,注册到一个js文件里方便统一管理调用,最后将这个index.js在main.js里面注册到vue实例中