4

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.

2
  • which vue version are you using? Commented Jul 3, 2022 at 7:18
  • All JS should be located in script tag. Is this your exact code in main.vue ? If it's not then there's a problem with the question. "the solution seems to be to use Typescript" - answers aren't limited to the first post, you can read them all. Enums are TS constructs, that's why TS appeared in search results. It doesn't matter what's the role of the constant is and whether you use Object.freeze, it's treated the same way in your case, this is basically a var from another module Commented Jul 3, 2022 at 7:36

2 Answers 2

3

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
     }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

First, You import it as Status but use it as AudioCardStatus, Also you need to asign it to property in your data.

Second, you should import it inside a script tag.

Third, you don't need the extra ./ this enough ../enums/status

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.