【发布时间】: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