SoFunction
Updated on 2025-04-10

How to use classes in vue project

Classes in vue project

As shown in the title, if you want to provide a class in the vue project, how should you write it?

js actually does not have classes, it can only use function to simulate classes. If you use native js, you want to write a class, you can write it like this:

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

//User classlet hi = new Hi();
();//hello world!

So how do you write it in vue?

I am already very used to writing native js and use them very well.

But the writing method in vue is a bit weird:

1) Definition

/src/utils/

export class Hi {
  #hi;//# means private ownership. If you don’t write, it means public ownership. If you are afraid of not writing, you are afraid of  constructor() {
    this.#hi = "hello world!";
  }

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

2) Call

//The class definition is located in the file /src/utils/import * as tools from "@/utils";

const hi = new ();
();

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.