Laravel Bootstrap-Vue不适用于NAV崩溃

时间:2020-05-01 01:54:45

标签: javascript laravel twitter-bootstrap vue.js bootstrap-vue

我正在尝试让我可折叠的导航栏与Laravel 7中的Vue + Bootstrap一起使用,但是没有运气。导航栏显示没有问题,但是当我调整大小并尝试使其折叠时,该按钮不执行任何操作,也不会引发任何错误。

我已将bootstrap-vue导入到我的app.ts文件中,应该启用它,但似乎没有。

app.ts

import "./bootstrap";
import Vue from "vue";
import BootstrapVue from "bootstrap-vue"; //Importing

Vue.use(BootstrapVue); // Telling Vue to use this in whole application

// Require everything withing the /components directory as a Vue component
const files = require.context("./", true, /\.vue$/i);
files.keys().map(key =>
    Vue.component(
        key
            .split("/")
            .pop()
            ?.split(".")[0] ?? "",
        files(key).default
    )
);

new Vue({
    el: "#app"
});

navbar文件(main.blade.php)

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="csrf-token"
          content="{{ csrf_token() }}">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>nCoV Stats</title>
    <link href="{{ asset('css/app.css') }}"
          rel="stylesheet">
</head>

<body class="d-flex flex-column min-vh-100">
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <a class="navbar-brand"
           href="#">Navbar</a>
        <button class="navbar-toggler"
                type="button"
                data-toggle="collapse"
                data-target="#navbarNav"
                aria-controls="navbarNav"
                aria-expanded="false"
                aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse"
             id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item active">
                    <a class="nav-link"
                       href="#">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link"
                       href="#">Features</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link"
                       href="#">Pricing</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link disabled"
                       href="#"
                       tabindex="-1"
                       aria-disabled="true">Disabled</a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- Content -->
    <main id="app"
          class="wrapper flex-grow-1">
        @yield('content')
    </main>

    <!-- Footer -->
    <footer>
        <div class="container">
            <div class="row">
                <div class="col-8">
                    A Joe Scotto product.
                </div>
                <div class="col-4"></div>
            </div>
        </div>
    </footer>

    <script src="{{ asset('js/app.js') }}"></script>
</body>

</html>

据我了解,这应该可以正常工作,因为我正在将其导入app.ts

1 个答案:

答案 0 :(得分:3)

我认为这是因为您没有在标记中使用任何Bootstrap-Vue组件。在您的情况下,导航栏必须类似于:

<b-navbar toggleable="lg" type="light" variant="light">
    <b-navbar-brand href="#">NavBar</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 href="#">Home</b-nav-item>
        <b-nav-item href="#">Features</b-nav-item>
        <b-nav-item href="#">Pricing</b-nav-item>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>

您可以找到有关导航栏组件here

的更多信息

此外,就像提到的porloscerros一样,您需要在id=app元素上设置body或使用div创建一个id=app,其中包含您的(Bootstrap-Vue)组件。