0

I'm getting the following error:

Property or method "showNotification" 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. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <MsgcNotification> at src/layout/Header/msgc/msgc.vue

Inside a very simple component. I've seen this error a hundred times and I know it's normally because I am trying to use a variable in the template that wasn't defined in my JS code.

I tried adding it as a prop, as part of the data and even as a computed property but for some reason I cannot get the error to disappear.

My component looks like this:

<template>
  <span>
    <span v-if="showNotification" class="msgc-notification" :style="{ top: topPos, left: leftPos, right: rightPos }">
    {{ amountToDisplay }}
    </span>
  </span>
</template>

<script src="./msgc.js" />
<script src="./msgc.scss" scoped lang="scss" />

Its javascript looks like:

import * as SessionValidator from 'session-validator'

export default {
  props: {
    isLoggedIn: {
      type: Boolean,
      default: SessionValidator.isValidSession()
    },
    unreadMessages: {
      type: Number,
      default: 0
    },
    amountToDisplay: {
      type: String,
      default: '0'
    },
    topPos: {
      type: String,
      default: 'auto'
    },
    leftPos: {
      type: String,
      default: 'auto'
    },
    rightPos: {
      type: String,
      default: 'auto'
    }
  },
  data() {
    return {
      showNotification: false
    }
  },
  watch: {
    unreadMessages: {
      handler: function handler(value) {
        this.showNotification = (this.isLoggedIn && value > 0)
        if (this.showNotification) this.amountToDisplay = value > 99 ? '+99' : value.toString()
      }
    }
  }
}

I can't believe I'm stuck in such a simple error but I just can't see my mistake. I need some fresh eyes to take a look.

EDIT: I'm an idiot, I was loading my styles with a script tag instead of a style tag.

tony19
  • 125,647
  • 18
  • 229
  • 307
RichyST
  • 243
  • 1
  • 3
  • 11
  • 2
    Shouldn't your Sass file be in a ` – Phil Jun 12 '19 at 22:54
  • See also [Why don't self-closing script elements work?](https://stackoverflow.com/questions/69913/why-dont-self-closing-script-elements-work). Not sure exactly if this restriction applies to the vue-loader but `` just doesn't look right to me :) – Phil Jun 12 '19 at 22:55
  • @Phil you were right with the first comment, the missing ` – RichyST Jun 12 '19 at 22:58

1 Answers1

1

As a Single File Component in Vue.js, for styling with CSS/SCSS/LESS, you must use style tag. The script tag is used for JS/TS code.

tony19
  • 125,647
  • 18
  • 229
  • 307
Harshal Patil
  • 17,838
  • 14
  • 60
  • 126