6

I have a Vue.js v2.6 project with TypeScript v.4.3 "under the hood".
My problem: I cannot find any documentation that clears up, if it makes sense to use access modifiers in Vue components and how to use them correctly.
I searched through articles, Vue.js docs, and StackOverflow, without any findings. (The Vue.js docs ignore their existence completely!)

This is how I am currently using access modifiers in my Vue components:

Template part of myComponent.vue:

<template>
  <div>
    <!-- accessing some of the fields of the class here e.g. the getter -->
  </div>
</template>

Script part of myComponent.vue:

<script lang="ts">
  import { Vue, Component, Prop, Emit } from "vue-property-decorator";

  @Component()
  export default class MyComponent extends Vue {
    @Prop() public readonly myPropertyVariable!: boolean;

    public myVariable!: string; // accessed in template

    private myInternalVariable!: boolean; // not accessed in template

    public get myGetterVariable(): string {
       // obviously accessed in template (getter)
    }

    @Emit()
    public change(): void {}

    @Action
    public doAction(): void {}

    public mounted(): void {}

    public myMethod(): boolean {
      // accessed in template
    }

    private myInternalMethod(): boolean {
      // not accessed in template
    }
  }
</script>

I hope, this is not an opinion-based question. I am assuming there a facts that confirm the meaningfulness of access modifiers in Vue components. I have a strong, possibly irrational resistance to omit them all.

Btw: I am coding in Rider, the access modifiers might be useful for IDE code analysis.

tony19
  • 125,647
  • 18
  • 229
  • 307
jasie
  • 2,192
  • 10
  • 39
  • 54
  • related question: https://stackoverflow.com/questions/55339564/what-access-modifiers-should-i-use-for-the-class-component-in-vue (no answers) – jasie Aug 04 '21 at 12:59

4 Answers4

1

If it works like Angular, a public modifier as you commented in your code, will make sure that your template can call those public functions.

Perhaps as the other fellow devs say, in the use case of @refs. (Although if you need them, there's is a good chance you are spaghetti coding)

Marking a method private is just a way to say "this doesn't belong to the template".

In these "frontend scenarios" where classes are kind of forced, the use of accessors just increases readability (and confusion :D)

This question wasn't answered in that other thread, mostly because it's common sense and style, and my 2 cents are, use private when you don't want to target the template, just to increase your code readability.

As a side note, I've been working with vue and typescript for 3 years now, and the one thing I can tell you is, I always found vue class components bloatware, especially in vue3. Vue2 and 3 support brilliantly typescript without any need for class components.

Cristiano Soleti
  • 805
  • 7
  • 12
  • Thanks Christiano, but the template does not seem to care about it if the properties and method it uses are being declared public or private. I can access them either way.. – jasie Aug 16 '21 at 05:41
  • Well, I assumed that was the truth considering your commented code AND the fact that angular actually works like that. If it doesn't then it's just about visibility really, but usually communication through components happens in different ways. I'd move regardless away from that. – Cristiano Soleti Aug 16 '21 at 11:55
  • 1
    I mean, I would move away from class components in favor of just plain vue + typescript. – Cristiano Soleti Aug 16 '21 at 12:05
0

I found in my projects that the public/private access modifiers are only useful when your components are referenced from other components (e.g. using the @Ref property decorator)

gbalduzzi
  • 9,356
  • 28
  • 58
  • Thanks. But would you please elaborate on the why? – jasie Aug 04 '21 at 13:21
  • Because I have not found another use for them, I don't really know how to expand on that – gbalduzzi Aug 04 '21 at 15:26
  • 1
    I also want to add that in every language the access modifiers are ony useful when using an object outside the class declaration. In a similar way, the vue access modifiers are not useful inside the component itself but only when the component is `Ref`erenced from the outside – gbalduzzi Aug 17 '21 at 15:52
0

TypeScript's public/private access modifiers do not affect the transpiled output and thus have no effect within Vue's ecosystem. You can still use them but they will only help during development by providing property access errors.

See: TypeScript Support for Vue.js

This extension is specific to VSCode, but seems to align with what you're looking for: https://github.com/prograhammer/vscode-tslint-vue

tony19
  • 125,647
  • 18
  • 229
  • 307
matpie
  • 17,033
  • 9
  • 61
  • 82
  • I did not get a property access error a single time, no matter what access modifier I used wherever (e.g. private on props that are used in template)... – jasie Aug 18 '21 at 06:45
  • I had also read the doc you shared but no access modifiers are even mentioned there. They are frankly just omitted. I really think they should make the effort to write a few words about it, especially in the class components section. – jasie Aug 18 '21 at 06:48
  • 1
    @jasie, I agree it's strangely omitted. I linked there mostly to show Vue.js *doesn't* do anything with it. As for getting errors, I added a second link for you. – matpie Aug 18 '21 at 23:52
-2

Public/Private modifiers are used when you want a logical separation of what can and can't access something. For instance, in Object Oriented Programming, we declare the attributes of an object private, because you never directly access those attributes. Instead you perform an action to modify or access those attributes. This is just an old programming paradigm.

The example I give my students is to consider for instance, a real world object. Take for instance a Pencil. We define its attributes and say okay, the pencil has a length, a sharpness, and a color.

Each of these attributes, length, sharpness, and color, would all be prefaced with the modifier private because we would never be able to magically change these attributes. If we made them public attributes, we could just do:

pencil.color = "blue";

And suddenly our pencil is blue. But we have no idea how we got here, or why. It's spontaneous pencil color changing. We have ignored the process or function or method required to get to this point.

So instead, at a very basic level, we get or set the color of the pencil. This is another old object oriented paradigm, read: getters and setters (which are a whole different discussion). The functions would then be declared public because they are representative of the action that the object can perform or have performed on it. That way if you wanted to paint all of your pencils blue, somewhere else in your code, you could do this:

List<Pencil> my_pencils = pencilFairy.producePencils();
    for(Pencil p : my_pencils){
        p.paint("Blue");    
    }

And this way, for each pencil, which we will call p, in our pencils (that we acquired from the pencilFairy's recent production) we want to paint the pencil blue. And within the Pencil class, the logic required to define how painting a pencil, would then be defined.

TLDR; You use private to define something's attributes because things outside of that object should not be able to mystically and magically modify those attributes, and you use public to define the functions or methods that can be performed on/by/to that object, and those actions can modify those attributes or return some other info based on those attributes, etc.

EDIT: Rereading the question, I'm sure you know most of this already. Just providing my hot take on how I decide when and where something should be public/private.

TheFunk
  • 981
  • 11
  • 39
  • Thanks for the effort, but this question was NOT about the meaning of public/private but about the benefits and the correct application of them in vue.js class components. – jasie Aug 18 '21 at 06:42