Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums in Vue and javascript

I want to make enums work in in vue file.

I first define my enums in a js file

const Status= Object.freeze({
    Normal: "normal",
    Loading: "loading",
    Error: "error",
    Done: "done",
});

export default Status;

My main.vue file can't compile:

<template>
    <div v-if="status == AudioCardStatus.Normal">
</template>

import Status from "./../enums/status"

Error is

Property or method "Status" 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.

I have already looked at another similar SO question but the solution seems to be to use Typescript.

like image 709
joesph nguyen Avatar asked Sep 11 '25 12:09

joesph nguyen


1 Answers

You should add that imported object to the data option in order to be available for the template :

import Status from "./../enums/status"

export default{
   data(){
     return{
        status:Status
     }
   }
}
like image 151
Boussadjra Brahim Avatar answered Sep 13 '25 02:09

Boussadjra Brahim