this piece of code is designed for a component that allows users to jump back to the top of the page. However, after refreshing the page, it stops working and throws an error. The project uses the Nuxt and Vue framework. Can anyone identify the reason behind this issue or suggest any changes needed to fix it?
Interestingly, this code works perfectly fine outside the Nuxt framework. But within the framework, it fails to function properly after the first refresh, resulting in an error message!
Error message :
TypeError : Cannot create property 'display' on string 'bottom:30px;right:30px;'
Vue.component('back-to-top', {
template: '#backToTop',
name: 'BackToTop',
props: {
text: {
type: String,
default: 'text'
},
visibleoffset: {
type: [String, Number],
default: 600
},
right: {
type: String,
default: '30px',
},
bottom: {
type: String,
default: '40px',
},
},
data() {
return {
visible: false
}
},
mounted() {
window.smoothscroll = () => {
let currentScroll = document.documentElement.scrollTop || document.body.scrollTop
if (currentScroll > 0) {
window.requestAnimationFrame(window.smoothscroll)
window.scrollTo(0, Math.floor(currentScroll - (currentScroll / 5)))
}
}
window.addEventListener('scroll', this.catchScroll)
},
destroyed() {
window.removeEventListener('scroll', this.catchScroll)
},
methods: {
catchScroll() {
this.visible = (window.pageYOffset > parseInt(this.visibleoffset))
},
backToTop() {
window.smoothscroll()
this.$emit('scrolled');
}
}
})
new Vue({
}).$mount('#app')
.back-to-top-fade-enter-active,
.back-to-top-fade-leave-active {
transition: opacity .7s;
}
.back-to-top-fade-enter,
.back-to-top-fade-leave-to {
opacity: 0;
}
.vue-back-to-top {
position: fixed;
z-index: 1000;
cursor: pointer;
}
.vue-back-to-top .default {
width: 160px;
color: #ffffff;
text-align: center;
line-height: 30px;
background-color: #f5c85c;
border-radius: 3px;
}
.vue-back-to-top .default span {
color: #ffffff;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>"
<title>CodePen - Backtotop Component</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<template id="backToTop">
<div>
<transition name="back-to-top-fade">
<div class="vue-back-to-top" :style="`bottom:${this.bottom};right:${this.right};`" v-show="visible" @click="backToTop">
<slot>
<div class="default">
<span>
{{ text }}
</span>
</div>
</slot>
</div>
</transition>
</div>
</template>
<div id="app">
<back-to-top text="Back to top"></back-to-top>
Lorem ipsum dolor sit amet consectetur adipisicing elit. In id libero fugit atque repudiandae cumque officiis(...) library/2.5.17/vue.min.js'></script>
<script src="./script.js"></script>
</body>
</html>