0

how can i use constants in the template of the vuejs?

this is my code, I'm declaring this constants import TIPS from '../../constants/tipsConstants' and i want to use in the template like this onWhiteListedMint(TIPS.TIP_A)

<template>
  <el-row class="row" justify="center">
    {{ACTION_CARD.ACTION_A}}
    <CustomCard
      @onClick="isListed(TIPS.TIP_A)"
    />
import TIPS from '../../constants/tipsConstants'
  
  export default {
    name: 'Main',
    props: {

the content of tipsConstants is:

export const TIPS = { TIP_A: 'TIP_A', TIP_B: 'TIP_B', TIP_C: 'TIP_C'}

i am getting this error

35:10  error    'TIPS' is defined but never used  
0

1 Answer 1

2

tipsConstants.js has a named export for TIPS, so the component would have to use a named import:

import { TIPS } from '../../constants/tipsConstants'

You could expose TIPS to the template through a data property. Since you likely don't need reactivity on it, use Object.freeze() on the imported object as shown below:

import { TIPS } from '../../constants/tipsConstants'

export default {
  data() {
    return {
      TIPS: Object.freeze(TIPS),
    }
  }
}

demo

Sign up to request clarification or add additional context in comments.

1 Comment

You technically can expose TIPS directly without Object.freeze(), but then the object will be made reactive (i.e., Vue will track changes to the object) unecessarily because the object is not going to change (they're all constants). A data property is reactive by default, but you have the option of making it not-reactive with Object.freeze().

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.