当前位置:   article > 正文

vue2 创建utils文件_vue2 utils

vue2 utils

utils文件夹是统一管理函数工具

在src目录下创建一个utils文件夹来统一存放函数工具

可以创建一个index.js文件,在index.js文件里写js函数 

  1. // index.js
  2. // 方法一
  3. // 验证是否为blob格式
  4. export async function blobValidate(data) {
  5. try {
  6. const text = await data.text();
  7. JSON.parse(text);
  8. return false;
  9. } catch (error) {
  10. return true;
  11. }
  12. }
  13. // 方法二
  14. function test(){
  15. alert("你好");
  16. }
  17. export default test;
  18. //还可以写class类函数
  19. class Person{
  20. // 构造函数
  21. constructor(x,y){
  22. this.x = x;
  23. this.y = y;
  24. }
  25. todoSome(){
  26. alert(this.xthis.y);
  27. }
  28. }
  29. export default Person;

在main.js文件,引入,并挂载在Vue实例上

  1. //main.js
  2. //引入
  3. import {blobValidate test Person } from "@/utils/index";
  4. // 全局方法挂载
  5. Vue.prototype.blobValidate = blobValidate
  6. Vue.prototype.test = test
  7. Vue.prototype.Person = Person

页面如何使用

  1. //页面
  2. this.blobValidate(); //调用函数
  3. this.test();
  4. const test= new this.Person('haha','33'); //调用类js,先new,注意传参
  5. test.todoSome(); //调用class类下面的方法

还可以把多个js函数文件,注册到一个js文件里方便统一管理调用,最后将这个index.js在main.js里面注册到vue实例中

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/753262
推荐阅读
相关标签
  

闽ICP备14008679号