【问题标题】:BootstrapVue - Bootstrap CSS is being added to my Vue app multiple timesBootstrapVue - Bootstrap CSS 被多次添加到我的 Vue 应用程序中
【发布时间】:2020-10-17 04:38:56
【问题描述】:

我正在使用 BootstrapVue 并已按照文档进行设置。我看到的是,对于我的 Vue 应用程序中使用 BootstrapVue 组件的每个组件,我都将 Bootstrap 嵌入到我呈现的 HTML 中。在本例中,我添加了 27 个相同样式表的实例。

我看不出问题出在哪里。

这是我的 main.js 文件

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { BRow, BCol, BContainer  } from 'bootstrap-vue'
import {InlineSvgPlugin} from "vue-inline-svg/dist/vue-inline-svg";
import VueSvgIconPolyFill from "@yzfe/vue-svgicon-polyfill";

Vue.config.productionTip = false

Vue.use(VueSvgIconPolyFill);
Vue.use(BCol, BRow, BContainer)
Vue.use(require('vue-moment'))
Vue.use(InlineSvgPlugin)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

这是一个使用 BootstrapVue 的 Vue 组件示例

<template>
    <section class="latest-news">
        <b-container>
            <b-row class="latest-news__title-bar">
                <b-col class="d-flex align-items-center" :sm="12" :md="10">
                    <h2>
                        {{blockTitle}}
                        <a
                                href="#"
                                title="Collapse this section"
                                :class="`collapse-control ` + (isCollapsed ? 'collapse-control--collapsed' : '')"
                                @click.prevent="isCollapsed = !isCollapsed"
                                v-html="(isCollapsed) ? 'Show' : 'Hide'"
                        ></a>
                    </h2>
                </b-col>
                <b-col class="d-flex justify-space-around" v-if="showSearchBar">
                    <ContentSearchBar title="Search news"/>
                </b-col>
                <b-col class="d-flex justify-content-end">
                    <Toolbar controls="news" :display-sorter="false" v-if="!isCollapsed && news.length"/>
                </b-col>
            </b-row>
            <b-row v-if="!isCollapsed">
                <b-col
                        cols="12"
                        :sm="smCols"
                        :md="mdCols"
                        v-for="(item, index) in news"
                        :key="`news` + index"
                       :class="((index < numberNewsItems) || !numberNewsItems) ? `latest-news__col` : ``"
                >
                    <NewsItem
                            v-if="(index < numberNewsItems) || numberNewsItems === null"
                            :show-excerpt="showExcerpt"
                            :news="item"
                            :item-index="index"
                            :format="newsListFormat"
                    />
                </b-col>
            </b-row>
            <b-row v-if="news && news.length === 0 && isSearch && !isCollapsed">
                <b-col cols="12">There are no news item results for your search term "{{$route.params.query}}"</b-col>
            </b-row>
        </b-container>
    </section>
</template>

<script>
  import NewsItem from "./NewsItem";
  import ContentSearchBar from "./ContentSearchBar";
  import Toolbar from "./Toolbar";
  import store from '../store';
  import {mapGetters} from 'vuex';
  import NewsService from '../services/NewsService'

  export default {
    name: "LatestNews",
    store: store,
    props: {
      showExcerpt: {
        default: true,
      },
      showSearchBar: {
        default: false,
      },
      numberNewsItems: {
        default: null
      },
      'isSearch': {
        default: false
      },
    },
    data() {
      return {
        news: [],
        isCollapsed: false
      }
    },
    mounted() {
      if (this.isSearch) {
        this.searchNews()
      } else {
        this.getNews()
      }
    },
    methods: {
      async getNews() {
        const response = await NewsService.all()
        this.news = response.data
      },
      async searchNews() {
        let query = this.$route.params.query;
        const response = await NewsService.search(query);
        this.news = response.data.results
      }
    },
    components: {Toolbar, ContentSearchBar, NewsItem},
    computed: {
      blockTitle() {
        if (this.isSearch) {
          return 'News search results for "' + this.$route.params.query + '"'
        } else {
          return 'Latest News'
        }

      },
      ...mapGetters([
        'newsListFormat'
      ]),
      smCols() {
        if (this.newsListFormat === 'list') {
          return '12'
        } else {
          return '6'
        }
      },
      mdCols() {
        if (this.newsListFormat === 'list') {
          return '12'
        } else {
          return '3'
        }
      },
    }

  }
</script>

<style lang="scss">
    .latest-news {
        &__col {
            margin-bottom: 24px;
        }

        &__title-bar {
            margin-bottom: 20px;

            h2 {
                margin-bottom: 0;
            }
        }
    }
</style>

这是 Chrome 开发工具在 yarn serveed 时显示 HTML 的方式

【问题讨论】:

  • 您不应该添加从bootstrap-vue 导出的模块的Vue.use(BootstrapVue); 吗?我不知道它是否解决了问题
  • 同样的事情发生在我身上。不知道为什么,因为我按照文档中的说明进行操作。一件奇怪的事情是,每个 Bootstrap 实例都有一个与之关联的不同数据标签,即 [data-v-d66bcac4]
  • 我认为这可能与在我的样式中使用作用域有关,但删除它并没有改变任何东西。 @霍克斯
  • 我最终对 Bootstrap 和 Bootstrap Vue 进行了自定义导入,然后使用 PurifyCSS 从导入的模块中修剪任何未使用的样式。它有所帮助,但纱线构建产生的 CSS 仍然是 1.2MB,这似乎太大了。

标签: vue.js bootstrap-vue


【解决方案1】:

这个问题已经解决了:)

问题是由于将引导程序 scss 文件包含在一个 scss 文件中,该文件是 @imported 到 Vue 的 main.js 中,以便引导程序的混合可以用于单个文件组件的样式

删除 Bootstrap scss 导入并在 main.js 文件中导入 bootstrap css 文件修复了该问题。

【讨论】:

  • 太棒了 - 我刚回到这里更新这个帖子,在得到相同的解决方案后!哈哈很高兴你能确定发生了什么。
猜你喜欢
  • 2017-12-29
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多