如题所示,vue项目中想提供类,该怎么写?

js其实并没有类,它只能用function来模拟类。如果用原生的js,要写一个类,可以这样写:

//类 
function Hi(){
let hi = "hello world!";

this.say = function(){
console.log(hi);
}
}

//使用类
let hi = new Hi();
hi.say();//hello world!

那在vue中怎么写呢?原生js的写法,我已经十分习惯了,用得很溜。但vue里面的写法,有点古怪:

1)定义

/src/utils/index.js

export class Hi {
#hi;//#代表私有,不写就是公有,问你怕未
constructor() {
this.#hi = "hello world!";
}

say = () => {
return this.#hi;
};
}

2)调用

//类定义位于文件 /src/utils/index.js
import * as tools from "@/utils";

const hi = new tools.Hi();
hi.say();