Class based vue component property definition: constructor vs. getter / setter vs. mounted lifecycle The Next CEO of Stack OverflowWhat is the tag name of class based vue componentWebpack fails to mount Vue componentsOnEvent vue class component decoratorDefining user-defined getters in a Vue componentVue Class Based Component Warning: Property is not defined on the instance but referenced during renderHow to build a es6 vue-component library using vue-class-componentHow can both a base class and mixins be used with vue-class-component?Vue&TypeScript: 'el' property for vue-property-decorator/vue-class-component?Use plugin given Vue constructor for component inheritance with vue-component-classHow to use vue-i18n with Vue class components?

What is the difference between "hamstring tendon" and "common hamstring tendon"?

Physiological effects of huge anime eyes

It is correct to match light sources with the same color temperature?

From jafe to El-Guest

Won the lottery - how do I keep the money?

Can this note be analyzed as a non-chord tone?

Why is information "lost" when it got into a black hole?

Film where the government was corrupt with aliens, people sent to kill aliens are given rigged visors not showing the right aliens

How did Beeri the Hittite come up with naming his daughter Yehudit?

Computationally populating tables with probability data

Do scriptures give a method to recognize a truly self-realized person/jivanmukta?

Reshaping json / reparing json inside shell script (remove trailing comma)

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?

What would be the main consequences for a country leaving the WTO?

Is it OK to decorate a log book cover?

Redefining symbol midway through a document

Can Sneak Attack be used when hitting with an improvised weapon?

What is the process for cleansing a very negative action

Airplane gently rocking its wings during whole flight

How do I fit a non linear curve?

"Eavesdropping" vs "Listen in on"

Is there such a thing as a proper verb, like a proper noun?

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

What day is it again?



Class based vue component property definition: constructor vs. getter / setter vs. mounted lifecycle



The Next CEO of Stack OverflowWhat is the tag name of class based vue componentWebpack fails to mount Vue componentsOnEvent vue class component decoratorDefining user-defined getters in a Vue componentVue Class Based Component Warning: Property is not defined on the instance but referenced during renderHow to build a es6 vue-component library using vue-class-componentHow can both a base class and mixins be used with vue-class-component?Vue&TypeScript: 'el' property for vue-property-decorator/vue-class-component?Use plugin given Vue constructor for component inheritance with vue-component-classHow to use vue-i18n with Vue class components?










1















I'm just wondering which way is the most reliable to define properties, which should generate an output in the template.



Define property in constructor:



Template reference:



<h1>msg</h1>


Property definition:



<script lang="ts">
import Component, Vue from "vue-property-decorator";
@Component
export default class Test extends Vue
protected msg: string;
public constructor()
super();
this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');


</script>


Output in Browser:



<h1>Today's date 2019/03/07</h1>



Define property in mounted lifecycle:



Template reference:



<h1>msg</h1>


Property definition:



export default class Test extends Vue 
protected msg: string = '';
mounted()
this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




Output in Browser:



<h1>Today's date 2019/03/07</h1>



Define property by get and set, set value in constructor:



Template reference:



<h1>msgText</h1>


Property definition:



export default class Test extends Vue 
protected msg: string = '';
public constructor()
super();
this.msgText = 'Today's date ' + moment().format('YYYY/MM/DD');

get msgText(): string
return this.msg;

set msgText(msg:string)
this.msg = msg;




Output in Browser:



<h1>Today's date 2019/03/07</h1>



Questions:



  • All three ways results in the same output. Is there a golden rule / best practice, how properties should be defined and in which lifecycle?

  • Is there a difference, if properties are defined in constructor or in mounted lifecycle?









share|improve this question


























    1















    I'm just wondering which way is the most reliable to define properties, which should generate an output in the template.



    Define property in constructor:



    Template reference:



    <h1>msg</h1>


    Property definition:



    <script lang="ts">
    import Component, Vue from "vue-property-decorator";
    @Component
    export default class Test extends Vue
    protected msg: string;
    public constructor()
    super();
    this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');


    </script>


    Output in Browser:



    <h1>Today's date 2019/03/07</h1>



    Define property in mounted lifecycle:



    Template reference:



    <h1>msg</h1>


    Property definition:



    export default class Test extends Vue 
    protected msg: string = '';
    mounted()
    this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




    Output in Browser:



    <h1>Today's date 2019/03/07</h1>



    Define property by get and set, set value in constructor:



    Template reference:



    <h1>msgText</h1>


    Property definition:



    export default class Test extends Vue 
    protected msg: string = '';
    public constructor()
    super();
    this.msgText = 'Today's date ' + moment().format('YYYY/MM/DD');

    get msgText(): string
    return this.msg;

    set msgText(msg:string)
    this.msg = msg;




    Output in Browser:



    <h1>Today's date 2019/03/07</h1>



    Questions:



    • All three ways results in the same output. Is there a golden rule / best practice, how properties should be defined and in which lifecycle?

    • Is there a difference, if properties are defined in constructor or in mounted lifecycle?









    share|improve this question
























      1












      1








      1








      I'm just wondering which way is the most reliable to define properties, which should generate an output in the template.



      Define property in constructor:



      Template reference:



      <h1>msg</h1>


      Property definition:



      <script lang="ts">
      import Component, Vue from "vue-property-decorator";
      @Component
      export default class Test extends Vue
      protected msg: string;
      public constructor()
      super();
      this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');


      </script>


      Output in Browser:



      <h1>Today's date 2019/03/07</h1>



      Define property in mounted lifecycle:



      Template reference:



      <h1>msg</h1>


      Property definition:



      export default class Test extends Vue 
      protected msg: string = '';
      mounted()
      this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




      Output in Browser:



      <h1>Today's date 2019/03/07</h1>



      Define property by get and set, set value in constructor:



      Template reference:



      <h1>msgText</h1>


      Property definition:



      export default class Test extends Vue 
      protected msg: string = '';
      public constructor()
      super();
      this.msgText = 'Today's date ' + moment().format('YYYY/MM/DD');

      get msgText(): string
      return this.msg;

      set msgText(msg:string)
      this.msg = msg;




      Output in Browser:



      <h1>Today's date 2019/03/07</h1>



      Questions:



      • All three ways results in the same output. Is there a golden rule / best practice, how properties should be defined and in which lifecycle?

      • Is there a difference, if properties are defined in constructor or in mounted lifecycle?









      share|improve this question














      I'm just wondering which way is the most reliable to define properties, which should generate an output in the template.



      Define property in constructor:



      Template reference:



      <h1>msg</h1>


      Property definition:



      <script lang="ts">
      import Component, Vue from "vue-property-decorator";
      @Component
      export default class Test extends Vue
      protected msg: string;
      public constructor()
      super();
      this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');


      </script>


      Output in Browser:



      <h1>Today's date 2019/03/07</h1>



      Define property in mounted lifecycle:



      Template reference:



      <h1>msg</h1>


      Property definition:



      export default class Test extends Vue 
      protected msg: string = '';
      mounted()
      this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




      Output in Browser:



      <h1>Today's date 2019/03/07</h1>



      Define property by get and set, set value in constructor:



      Template reference:



      <h1>msgText</h1>


      Property definition:



      export default class Test extends Vue 
      protected msg: string = '';
      public constructor()
      super();
      this.msgText = 'Today's date ' + moment().format('YYYY/MM/DD');

      get msgText(): string
      return this.msg;

      set msgText(msg:string)
      this.msg = msg;




      Output in Browser:



      <h1>Today's date 2019/03/07</h1>



      Questions:



      • All three ways results in the same output. Is there a golden rule / best practice, how properties should be defined and in which lifecycle?

      • Is there a difference, if properties are defined in constructor or in mounted lifecycle?






      typescript vue.js vue-component vue-class-components






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 18:13









      Mikel WohlschlegelMikel Wohlschlegel

      356311




      356311






















          1 Answer
          1






          active

          oldest

          votes


















          1














          The second approach of using mounted is preferred over the rest of the approaches. The only change I would suggest is the use of created hook instead of mounted:



          export default class Test extends Vue 
          protected msg: string = '';

          created()
          this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




          Generally, for simple properties, you can directly assign a value at a time of declaration. Use created when your assignment is not simple.



          Also, we don't really use constructors when writing the class-based component. The reason behind is that essentially Vue.js components are object-based. The @Component decorator is eventually making the component behave like object-based.



          Further, if you look at Vue.js component lifecycle methods, then there is no place for a constructor. The initial methods are beforeCreate -> data -> created -> mounted and so on. How can a beforeCreate execute without an actual call to the constructor? That make is really weird to reason about.



          Note 1: For version 3 of Vue.js, official class-based components are
          proposed. Thus, this might change in the near future.



          Note 2: TypeScript will move msg declaration to the constructor after compilation and Vue.js seems to work well with it. But it is still unspecified and better be avoided.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050333%2fclass-based-vue-component-property-definition-constructor-vs-getter-setter-v%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The second approach of using mounted is preferred over the rest of the approaches. The only change I would suggest is the use of created hook instead of mounted:



            export default class Test extends Vue 
            protected msg: string = '';

            created()
            this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




            Generally, for simple properties, you can directly assign a value at a time of declaration. Use created when your assignment is not simple.



            Also, we don't really use constructors when writing the class-based component. The reason behind is that essentially Vue.js components are object-based. The @Component decorator is eventually making the component behave like object-based.



            Further, if you look at Vue.js component lifecycle methods, then there is no place for a constructor. The initial methods are beforeCreate -> data -> created -> mounted and so on. How can a beforeCreate execute without an actual call to the constructor? That make is really weird to reason about.



            Note 1: For version 3 of Vue.js, official class-based components are
            proposed. Thus, this might change in the near future.



            Note 2: TypeScript will move msg declaration to the constructor after compilation and Vue.js seems to work well with it. But it is still unspecified and better be avoided.






            share|improve this answer





























              1














              The second approach of using mounted is preferred over the rest of the approaches. The only change I would suggest is the use of created hook instead of mounted:



              export default class Test extends Vue 
              protected msg: string = '';

              created()
              this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




              Generally, for simple properties, you can directly assign a value at a time of declaration. Use created when your assignment is not simple.



              Also, we don't really use constructors when writing the class-based component. The reason behind is that essentially Vue.js components are object-based. The @Component decorator is eventually making the component behave like object-based.



              Further, if you look at Vue.js component lifecycle methods, then there is no place for a constructor. The initial methods are beforeCreate -> data -> created -> mounted and so on. How can a beforeCreate execute without an actual call to the constructor? That make is really weird to reason about.



              Note 1: For version 3 of Vue.js, official class-based components are
              proposed. Thus, this might change in the near future.



              Note 2: TypeScript will move msg declaration to the constructor after compilation and Vue.js seems to work well with it. But it is still unspecified and better be avoided.






              share|improve this answer



























                1












                1








                1







                The second approach of using mounted is preferred over the rest of the approaches. The only change I would suggest is the use of created hook instead of mounted:



                export default class Test extends Vue 
                protected msg: string = '';

                created()
                this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




                Generally, for simple properties, you can directly assign a value at a time of declaration. Use created when your assignment is not simple.



                Also, we don't really use constructors when writing the class-based component. The reason behind is that essentially Vue.js components are object-based. The @Component decorator is eventually making the component behave like object-based.



                Further, if you look at Vue.js component lifecycle methods, then there is no place for a constructor. The initial methods are beforeCreate -> data -> created -> mounted and so on. How can a beforeCreate execute without an actual call to the constructor? That make is really weird to reason about.



                Note 1: For version 3 of Vue.js, official class-based components are
                proposed. Thus, this might change in the near future.



                Note 2: TypeScript will move msg declaration to the constructor after compilation and Vue.js seems to work well with it. But it is still unspecified and better be avoided.






                share|improve this answer















                The second approach of using mounted is preferred over the rest of the approaches. The only change I would suggest is the use of created hook instead of mounted:



                export default class Test extends Vue 
                protected msg: string = '';

                created()
                this.msg = 'Today's date ' + moment().format('YYYY/MM/DD');




                Generally, for simple properties, you can directly assign a value at a time of declaration. Use created when your assignment is not simple.



                Also, we don't really use constructors when writing the class-based component. The reason behind is that essentially Vue.js components are object-based. The @Component decorator is eventually making the component behave like object-based.



                Further, if you look at Vue.js component lifecycle methods, then there is no place for a constructor. The initial methods are beforeCreate -> data -> created -> mounted and so on. How can a beforeCreate execute without an actual call to the constructor? That make is really weird to reason about.



                Note 1: For version 3 of Vue.js, official class-based components are
                proposed. Thus, this might change in the near future.



                Note 2: TypeScript will move msg declaration to the constructor after compilation and Vue.js seems to work well with it. But it is still unspecified and better be avoided.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 7 at 18:47

























                answered Mar 7 at 18:41









                Harshal PatilHarshal Patil

                3,30621250




                3,30621250





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55050333%2fclass-based-vue-component-property-definition-constructor-vs-getter-setter-v%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко