javascript - 在导航栏组件中组合 bootstrap-vue 和 vue-router

标签 javascript vue.js vuejs2 vue-router bootstrap-vue

我正在尝试使用与 vue-router 一起使用的 bootstrap-vue 制作一个导航栏。导航栏运行正常,但当我单击导航栏项目时,它不会将我引导至该页面。在我加入 bootstrap-vue 之前,路由器就在工作,所以我猜我错过了一些与 bootstrap 相关的东西?有任何想法吗?

导航栏.vue:

<template>
    <div>
        <b-navbar 
            toggleable="lg"
            type="light"
            variant="light"
        >
            <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>
            <b-collapse id="nav-collapse" is-nav>
                <b-navbar-nav>
                    <b-link
                        active-class="active"
                        class="nav-link"
                        v-for="routes in links"
                        :key="routes.id"
                        :to="routes.path"
                    >
                        <b-nav-item>
                            {{ routes.name }}
                        </b-nav-item>
                    </b-link>
                </b-navbar-nav>
            </b-collapse>
        </b-navbar>

        <router-view></router-view>
    </div>
</template>

<script>

export default {
    name: 'Navbar',
    data() {
        return {
            links: [
                {
                    id: 0,
                    name: 'Home',
                    path: '/',
                },
                {
                    id: 1,
                    name: 'RSVP',
                    path: '/RSVP',
                },
                {
                    id: 2,
                    name: 'Pictures',
                    path: '/pictures',
                },
                {
                    id: 3,
                    name: 'Contact',
                    path: '/contact',
                },
            ],
        }
    },
}

</script>

router/index.js:

import Vue from 'vue';
import router from 'vue-router';
import Home from '../components/Home';
import RSVP from '../components/RSVP';
import Pictures from '../components/Pictures';
import Contact from '../components/Contact';

Vue.use(router);

export default new router({
    routes: [
        {
            path: '/',
            name: 'Home',
            component: Home
        },
        {
            path: '/RSVP',
            name: 'RSVP',
            component: RSVP
        },
        {
            path: '/pictures',
            name: 'Pictures',
            component: Pictures
        },
        {
            path: '/contact',
            name: 'Contact',
            component: Contact
        },
    ],
    mode: 'history',
});

主要.js:

import Vue from 'vue';
import App from './App';
import router from './router';
import BootstrapVue from 'bootstrap-vue'

Vue.use(router);
Vue.use(BootstrapVue);
Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: { App },
    template: '<App/>'
});

App.vue:

<template>
    <div id="app">
        <Navbar />
</div>
</template>

<script>

import Home from './components/Home';
import Navbar from './components/Navbar';
import RSVP from './components/RSVP';
import Contact from './components/Contact';
import Pictures from './components/Pictures';

export default {
    name: 'app',
    created () {
        document.title = "title";
    },
    components: {
        Home,
        Navbar,
        RSVP,
        Contact,
        Pictures,
    },
}

</script>

<style lang="css">

@import '../node_modules/bootstrap/dist/css/bootstrap.min.css';

</style>

最佳答案

根据 b-navbar-nav 文档:

Navbar navigation links build on the <b-navbar-nav> parent component and requires the use of and <b-nav-toggle> toggler for proper responsive styling. Navigation in navbars will also grow to occupy as much horizontal space as possible to keep your navbar contents securely aligned.

<b-navbar-nav> supports the following components:

  • <b-nav-item> for link (and router-link) action items

  • ...

所以你的代码应该是这样的:

  <b-nav-item active-class="active" class="nav-link" v-for="routes in links" 
    :key="routes.id" :to="routes.path" >
   {{ routes.name }} 
 </b-nav-item>      

关于javascript - 在导航栏组件中组合 bootstrap-vue 和 vue-router,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57732203/

相关文章:

javascript - 在 redux-observable 中使用 rxjs 示例来监听其他操作

vue.js - 使用 vue.js 更新值时如何添加 CSS 动画?

javascript - 使用vuejs加载页面时如何从firebase获取数据?

javascript - 按列表特定项目的字段对多个列表中的 d'n'd 列表项目进行排序

javascript - 如何在 Angular 2/4 中将当前日期转换为时间戳

javascript - 向具有不同域和基本身份验证的服务器发出 ajax POST 请求是不可能的吗?

javascript - 类型错误 : Cannot read property '$on' of undefined in q-ajax-bar

vue.js - 未捕获的类型错误 : routes. forEach 不是函数

javascript - 识别vuejs组件的多个容器

vuejs2 - Vue2 + Vuex 提交不提交(没有 Vue devtools)