apertureless / vue-cookie-law

🍪 👮 Hackable EU Cookie Law Plugin for Vue.js
MIT License
377 stars 43 forks source link

How to check isAccepted() on mounted()? #73

Open guiltyvictim opened 3 years ago

guiltyvictim commented 3 years ago

vue & vue-cookie-law version

2.6.12, 1.13.3

Sorry I'm still really new to Vue.js and JS apps, but I'm not understanding how I'm supposed to check if cookie consent has been given when a visitor visits the page.

I wanted to use this to enable / disable vue-analytics. I assume I want to do a check on the isAccepted() method inside mounted(), but how? What is the function tied to? this.isAccepted() doesn't work, nor does CookieLaw.isAccepted()

I'm not using scoped version of the code, just literally just:

<cookie-law v-on:accept="enableTracking()" />

alamo42 commented 3 years ago

Hi, you need to create what on Vue.js is called a ref (Vue v3). Take this as a basic example:

<template>
  <vue-cookie-law ref="cookieBanner">
    Accept cookies, please!!
  </vue-cookie-law>
</template>

<script>
export default {
  computed: {
    areCookiesAccepted()  {
      return this.$refs.cookieBanner.isAccepted()
    }
  },
  mounted() {
    console.log(this.areCookiesAccepted) // true or false
  }
}
</script>

Hope it had helped :)