clear intervals and timeouts on nuxt.js build - vue.js

On my website I have a few intervals and timeouts set, for things such as a carousel. Now I do clear them on the components destroyed() lifecycle hook. But I am still getting this build warning.
This is an example of one of my components
<script>
data() {
return {
carouselInterval: null
}
},
created() {
this.startInterval();
},
methods: {
startInterval() {
this.carouselInterval = setInterval(() => {
...
}, 5000);
}
},
destroyed() {
clearInterval(this.carouselInterval);
}
</script>
I also have a timeOut like so
<script>
data() {
return {
testTimeout: null
}
},
created() {
this.startTimeout();
},
methods: {
startTimeout() {
this.testTimeout = setTimeout(() => {
...
}, 5000);
}
},
destroyed() {
clearTimeout(this.testTimeout);
}
</script>
Now I would assume this would clear these timing functions but, I am still getting this warning.
Is there something I am doing wrong? Is there another way to clear all timing functions on build?
EDIT
I am 100% sure I have cleaned up all timing functions on the destroyed() lifecycle hook

The destroyed() lifecycle hook does not get called whilst using SSR. You can read more about this here SSR component lifecycle hooks
This means you can not use it to clear timeouts on the server.
You can solve this issue by moving your interval functions to either be called in the mounted() or beforeMount() lifecycle hooks since they are only called on the client side.
In your example you can change your code to be
<script>
data() {
return {
carouselInterval: null
}
},
mounted() {
this.startInterval();
},
methods: {
startInterval() {
this.carouselInterval = setInterval(() => {
...
}, 5000);
}
}
</script>
This will prevent the nuxt.js build from timing out.

Related

watcher in vuejs not working for props value

I am working on vue app. The issue I am facing here is that I want to run a method if props has value and in my case manager value. So I am trying something like this but the debugger is not called in the watcher. Please help me find where I am going wrong.
<script>
export default {
props: ['manager'],
watch: {
manager: function (value) {
debugger
if(value) {
this.validationManager();
}
}
},
methods: {
validationManager(){
console.log("Hello")
}
}
};
</script>
We can definetely watch the props, please try this:
watch: {
manager: {
// the callback will be called immediately after the start of the observation
immediate: true,
handler (val, oldVal) {
//do your stuff here
this.validationManager();
}
}
}
You forget the deep attribute for watcher
watch: {
manager: {
handler(value){
if(value) {
this.validationManager();
}
},
immediate: true,
deep: true,
}
}

Action is not defined as a method in the component vue.js

I'm trying to access an action as a method in component, But got error of
this.delete_notifaction is not a function
notifaction.js
export const actions = {
add_notifaction({ commit }, notifaction) {
commit("ADD_NOTIFACTION", notifaction);
},
delete_notifaction({ commit }, notificationToRemove) {
commit('DELETE_NOTIFACTION', notificationToRemove)
}
};
store/index.js
modules : {
notifaction
},
Component.vue
methods: mapActions('notifaction',["delete_notifaction"]),
mounted() {
this.delete_notifaction(this.notification);
}
Any Help?
Try this
methods: {
...mapActions(['delete_notifaction']),
}
See here for spread syntax
https://stackoverflow.com/a/48137766/10118668

Vue - Data not computed in time before mount

I'm learning Vue and I've run into a problem where my data returns undefined from a computed method. It seems that the data is not computed by the time the component is mounted, probably due to the get request - wrapping my this.render() in a setTimeout returns the data correctly. Setting a timeout is clearly not sensible so how should I be doing this for best practice?
Home.vue
export default {
created() {
this.$store.dispatch('retrievePost')
},
computed: {
posts() {
return this.$store.getters.getPosts
}
},
methods: {
render() {
console.log(this.comments)
}
},
mounted() {
setTimeout(() => {
this.render()
}, 2000);
},
}
store.js
export const store = new Vuex.Store({
state: {
posts: []
},
getters: {
getPosts (state) {
return state.posts
}
},
mutations: {
retrievePosts (state, comments) {
state.posts = posts
}
},
actions: {
retrievePosts (context) {
axios.defaults.headers.common['Authorization'] = 'Bearer ' + context.state.token
axios.get('/posts')
.then(response => {
context.commit('retrievePosts', response.data)
})
.catch(error => {
console.log(error)
})
}
}
})
It is because axios request is still processing when Vue invokes mounted hook(these actions are independent of each other), so state.posts are undefined as expected.
If you want to do something when posts loaded use watch or better computed if it's possible:
export default {
created() {
this.$store.dispatch('retrievePost')
},
computed: {
posts() {
return this.$store.getters.getPosts
}
},
methods: {
render() {
console.log(this.comments)
}
},
watch: {
posts() { // or comments I dont see comments definition in vue object
this.render();
}
}
}
P.S. And don't use render word as methods name or something because Vue instance has render function and it can be a bit confusing.

How to listen to scroll events in vue nuxtjs?

I've search for a solution and came up with this code
methods: {
handleScroll () {
console.log(window.scrollY)
}
},
created () {
window.addEventListener('scroll', this.handleScroll);
},
destroyed () {
window.removeEventListener('scroll', this.handleScroll);
}
Unfortunately, this doesn't work for me. I also tried to change window to document.body.
The error message was Window is not defined
Using window or any other browser-specific API in created or beforeCreate will lead to problems because platform-specific APIs like document or window are not available on the server (where SSR happens). Instead, move the logic from created into beforeMount. Leaving it in created and checking it via process.browser would work as well but is not as clean and can lead to confusion easily.
export default {
methods: {
handleScroll () {
// Your scroll handling here
console.log(window.scrollY)
}
},
beforeMount () {
window.addEventListener('scroll', this.handleScroll)
},
beforeDestroy () {
window.removeEventListener('scroll', this.handleScroll)
}
}
Only created and beforeCreate are executed on both sides, server and client. Therefore you don't need guarding ifs in beforeMount or beforeDestroy.
Further read about ssr-ready Vue components
window is undefined because nuxt JS is server side rendered.
So try it using process.client variable
methods: {
handleScroll () {
console.log(window.scrollY)
}
},
created () {
if (process.client) {
window.addEventListener('scroll', this.handleScroll);
}
},
destroyed () {
if (process.client) {
window.removeEventListener('scroll', this.handleScroll);
}
}
See link for more info
For me work only with 'wheel' not 'scroll'
beforeMount () {
window.addEventListener('wheel', this.handleScroll)
},
beforeDestroy() {
window.removeEventListener('wheel', this.handleScroll);
},

Vue2 window addEventListener scroll doesnt fire?

I have a component, and I simply cannot get
window.addEventListener("scroll", function(){
console.log('scrolling');
});
to fire - I've tried to attach it to the created and mounted lifecycle hooks, however, it doesn't print to the console when scrolling.
At the moment I have the following, but still no luck. It fires the console.log("My Method") but not the scroll :(
export default {
data() {
return {
}
},
components: {
},
methods: {
myMethod(){
console.log("my method");
window.addEventListener("scroll", function(){
console.log('scrolling');
});
}
},
created() {
console.log("created");
},
mounted(){
console.log("mounted");
this.myMethod();
}
}
Have you tried this:
window.addEventListener("scroll", function () {
console.log("scroll");
}, true);
?