Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enums (or const) in VueJS?

I feel like an idiot for having to ask about something so seemingly simple, but I'm trying to figure out how to use "enums" in VueJS. Currently, in a file called LandingPage.js I have this bit of code:

const Form = {   LOGIN: 0,   SIGN_UP: 1,   FORGOT_PASSWORD: 2, };  function main() {   new Vue({     el: "#landing-page",     components: {       LoginForm,       WhoIsBehindSection,       WhatIsSection,       Form,     },     data () {       return {         form: Form.LOGIN,       };     },     template: `     <div>       <LoginForm v-if="form === Form.LOGIN"></LoginForm>       <WhatIsSection></WhatIsSection>       <WhoIsBehindSection></WhoIsBehindSection>     </div>     `   }); } 

It is the conditional v-if="form === Form.LOGIN" that is failing with the error messages:

  • Property or method "Form" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

  • Cannot read property 'LOGIN' of undefined

Just so you guys know without the conditional everything is working, and if I were to put this bit in the template

<p>{{ form }}</p> 

it will print 0 on the screen. Though, putting this in the template

<p>{{ Form.LOGIN }}</p> 

Will not result in it printing 0 on the screen. So I just cannot for the life of me figure out why it will not accept Form.LOGIN.

 

The Answer

I did add it to components, but never did I think of adding it to data. Happy that it's working now. :)

    data () {       return {         form: Form.LOGIN,         Form, // I had to add this bit       };     }, 

Thank you MarcRo 👍

like image 486
G. Thorsen Avatar asked Aug 17 '19 17:08

G. Thorsen


People also ask

What is a const enum?

const in an enum means the enum is fully erased during compilation. Const enum members are inlined at use sites. You can can't index it by an arbitrary value. In other words, the following TypeScript code const enum Snack { Apple = 0, Banana = 1, Orange = 2, Other = 3 } let snacks = [ Snack. Apple, Snack.

What is an enum TypeScript?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.

What is enum in Java?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


2 Answers

If you are using Vue in Typescript, then you can use:

import { TernaryStatus } from '../enum/MyEnums';    export default class MyClass extends Vue {        myVariable: TernaryStatus = TernaryStatus.Started;        TernaryStatus: any = TernaryStatus;     } 

and then in Template you can just use

<div>Status: {{ myVariable == TernaryStatus.Started ? "Started It" : "Stopped it" }}</div> 
like image 107
Mahesh Avatar answered Oct 08 '22 14:10

Mahesh


You can use https://stackoverflow.com/a/59714524/3706939.

const State= Object.freeze({ Active: 1, Inactive: 2 }); export default {   data() {     return {       State,       state: State.Active     };   },   methods: {     method() {       return state==State.Active;     }   } } 
like image 42
Roohi Ali Avatar answered Oct 08 '22 13:10

Roohi Ali