我是VueJS和BootstrapVue的新手,我已经使用导航栏组件中的事件滚动替换了该方法
router-link-exact-active
使用
精确链接更改
尽管可以,但是还是有问题
未捕获的TypeError:无法读取null的属性'classList'
问题出在这两个代码之内:
link.classList.add("exact-link-change")
cLink.classList.add("router-link-exact-active")
这是组件中的脚本文件:
<script type="text/javascript">
export default{
name:'Navbar',
data(){
return{
}
},
methods:{
handleScroll (event) {
let header = document.querySelector(".nav");
let link = document.querySelector(".router-link-exact-active");
let cLink = document.querySelector(".exact-link-change");
if (window.scrollY > 100) {
header.classList.add("nav--bgscroll")
link.classList.add("exact-link-change")
link.classList.remove("router-link-exact-active")
}
else{
header.classList.remove("nav--bgscroll")
cLink.classList.add("router-link-exact-active")
cLink.classList.remove("exact-link-change")
}
},
},
created () {
window.addEventListener('scroll', this.handleScroll);
},
destroyed () {
window.removeEventListener('scroll', this.handleScroll);
}
}
</script>
这是HTML:
<template>
<b-navbar toggleable= "lg" type="light" class = "nav" @scroll = "handleScroll">
<b-navbar-brand class = "title"> Aquafarm Beach Resort</b-navbar-brand>
<b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
<b-collapse id="nav-collapse" is-nav>
<b-navbar-nav>
<b-nav-item><b-link to ="/" active >Home</b-link></b-nav-item>
<b-nav-item><b-link :to="{name: 'Rooms'}">Rooms</b-link></b-nav-item>
<b-nav-item><b-link :to="{name: 'About' }">About</b-link></b-nav-item>
<b-nav-item><b-link :to="{name: 'ContactUs'}">Contact Us</b-link></b-nav-item>
</b-navbar-nav>
<b-navbar-nav class="ml-auto" >
<b-button variant= "outline-dark" :to="{name: 'Booking'}">Book Now</b-button>
</b-navbar-nav>
</b-collapse>
</b-navbar>
</template>
答案 0 :(得分:0)
BootstrapVue中大多数接受to
道具的组件还具有active-class
和exact-active-class
道具,它们使您可以指示组件应用与默认名称不同的类名。您也不需要在<b-link>
内放置<b-nav-item>
,因为<b-nav-item>
支持to
道具:
<b-navbar-nav>
<b-nav-item to ="/" active exact-active-class="exact-link-change">Home</b-nav-item>
<b-nav-item :to="{name: 'Rooms'}" exact-active-class="exact-link-change">Rooms</b-nav-item>
<b-nav-item :to="{name: 'About' }" exact-active-class="exact-link-change">About</b-nav-item>
<b-nav-item :to="{name: 'ContactUs'}" exact-active-class="exact-link-change">Contact Us</b-nav-item>
</b-navbar-nav>
exact-active-class
和active-class
道具是反应性的,因此您可以将它们v绑定到计算的道具,该道具根据某些条件返回特定的类名。
我建议阅读以下内容:
<template>
<b-navbar-nav>
<b-nav-item to ="/" :exact-active-class="activeClass">Home</b-nav-item>
<b-nav-item :to="{name: 'Rooms'}" :exact-active-class="activeClass">Rooms</b-nav-item>
<b-nav-item :to="{name: 'About' }" :exact-active-class="activeClass">About</b-nav-item>
<b-nav-item :to="{name: 'ContactUs'}" :exact-active-class="activeClass">Contact Us</b-nav-item>
</b-navbar-nav>
</template>
<script>
export default {
data(){
return{
isPast100Px: false
}
},
computed: {
activeClass() {
return this.isPast100px ? 'exact-link-change' : ''
}
}
methods:{
handleScroll (event) {
this.isPast100Px = window.scrollY > 100
},
},
beforeMount () {
window.addEventListener('scroll', this.handleScroll);
},
destroyed () {
window.removeEventListener('scroll', this.handleScroll);
}
}
</script>