javascript - 为什么使用 div 不使用 bootstrap 4 堆叠 div

标签 javascript vue.js bootstrap-4

<分区>

我正在研究 VueJs 和 bootstrap4 中的两个基本组件,问题是我的 div 我分配了类 .col-md-4 以便为每一行添加 3 个元素,但这并没有发生,实际上只是每行添加 1 个元素

Main.vue

<template>
    <div class="col-md-12">
        <categoriacard  v-for="cate in categorias"  
                    :categoria=cate :key="cate.id">
        </categoriacard>
    </div>
</template>
<script>
    import categoriacard from './categoria-card';
    export default {
        name : 'MainBarber',
        data(){
            return {
                categorias : []
            }
        },
        components :{
            categoriacard
        },
        mounted(){
            this.getCategorias();
        },
        methods : {
            getCategorias(){
                axios.get('/api/categorias')
                        .then((res)=>{
                    this.categorias = res.data;
                })
            }
        }
    }
</script>

categoria-card.vue

<template>
    <div class="col-md-4 mb-md-0 mb-4 mt-4 ">
        <div class="card card-image"  >
            <div class="text-white text-center justify-content-center rgba-black-strong py-5 px-4 rounded">
                <div>
                    <h6 class="purple-text">
                        <i class="fa fa-pie-chart"></i>
                        <strong>Categoria</strong>
                    </h6>
                    <h3 class="py-3 font-weight-bold">
                        <strong>{{ categoria.nombre }}</strong>
                    </h3>
                    <p class="pb-3 text-center">{{ categoria.descripcion }} </p>
                    <a class="btn btn-success btn-rounded btn-md waves-effect waves-light"><i class="fa fa-clone left"></i>Ver Servicios</a>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
    export default {
        name : 'categoria-card',
        props : ['categoria']
    }
</script>

我目前有这个结果

enter image description here

我想要的是

enter image description here

这可能会发生吗?

完整的 HTML 是

<div class="container">
   <div class="row">
      <MainBarber></MainBarber>
   </div>
</div>

最佳答案

在以下情况下似乎工作正常:

  • 在 categoria-card.vue 中用 col-4 替换 col-md4
  • 在 Main.vue 中用 row 替换 col-md-12

查看代码片段:

<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"/> 
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>

</head>
<body>

<div id="app">
  <main2></main2>
</div>

<script>


Vue.component('main2', {
  name : 'MainBarber',
        data(){
            return {
                categorias : []
            }
        },
        mounted(){
            this.getCategorias();
        },
        methods : {
            getCategorias(){
				console.log("categorias");
                this.categorias = [
					{id : 1, nombre : 'Esportivo', descripcion : 'desc1'}, 
					{id : 2, nombre : 'Infos', descripcion : 'desc2'}, 
					{id : 3, nombre : 'Politica', descripcion : 'desc3'},
					{id : 4, nombre : 'Moda', descripcion : 'desc4'}, 
					{id : 5, nombre : 'Cocina', descripcion : 'desc5'}, 
					{id : 6, nombre : 'Casa', descripcion : 'desc6'}					
					];
            }
        },
  template: `<div class="row">
				<categoria-card  v-for="cate in categorias"  
                    :categoria=cate :key="cate.id">{{cate.id}}
				</categoria-card>
			</div>`

})
Vue.component('categoria-card', {
		name : 'categoria-card',
        props : ['categoria'],
		template: `<div class="col-4">
        <div class="card card-image"  >
            <div class="text-center justify-content-center rgba-black-strong py-5 px-4 rounded">
                <div>
                    <h6 class="purple-text">
                        <i class="fa fa-pie-chart"></i>
                        <strong>Categoria</strong>
                    </h6>
                    <h3 class="py-3 font-weight-bold">
                        <strong>{{ categoria.nombre }}</strong>
                    </h3>
                    <p class="pb-3 text-center">{{ categoria.descripcion }} </p>
                    <a class="btn btn-success btn-rounded btn-md waves-effect waves-light"><i class="fa fa-clone left"></i>Ver Servicios</a>
                </div>
            </div>
        </div>
    </div>`

})

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!'
  }
})

</script>




</body>
</html>

关于javascript - 为什么使用 div 不使用 bootstrap 4 堆叠 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50989920/

相关文章:

vue.js - axios已定义但从未使用

javascript - 我可以使用 HTML/CSS 和 javascript 创建一个登录页面,然后再向其中添加 VUE 吗?

vue.js - vuetify 图标未显示

html - 我有一张背景图片,我必须在该图片上放置 3 x 3 图片,但图片重叠

validation - Google INVISIBLE reCaptcha + Bootstrap 4 验证器

javascript - 动态 url 作为 css 和 D3JS 中伪元素的数据属性

javascript - 使用javascript在括号中的负数

html - 将一个 div 拉伸(stretch)到它的全宽

javascript - 如何在 redux 中使用 setTimeout 和调度程序触发周期性操作

javascript - 防止 esc 退出全屏模式