Nuxt.js run code on page load

Asked By: Anonymous

How do I run code when a page loads with Nuxt.js?

I’d like to add a class if the browser supports touch events using !!window.ontouchstart.

I tried this, but window is undefined:

```
<section v-bind:class="{ touch: !!window.ontouchstart }">
   ...
</section>
```

Solution

Answered By: Anonymous

You need to first add an eventlistener to listen to touchstart.

created: function(){
    document.addEventListener('touchstart',this.touchStart);
 }, 
destroyed: function() { 
    document.removeEventListener('touchstart', this.touchStart); 
 }

Now you can use touch touchStart method
inside Vue methods.

methods:{
  touchStart(e){
    this.touched=true;
  }

}

In html

<section v-bind:class="{ touch: touched}"> ... </section>

Also ontouchstart is a window event which is not yet standardized,
So replace document with window.


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen Windows 10 Lisans Office 2019 Mac Satın Al follower kaufen instagram follower kaufen porno