vue模块化组件的使用(全局组件和局部组件)

vue模块化的组件开发

首先定义三个vue组件,注意:<template></template>之间最大子集div只能有一个

三个vue组件代码如下所示:

(内部的<selfButton>组件为全局组件,下文会提到)

elementView:

<template>
   <div class="elementView">
      i am header
      <selfButton></selfButton>
   </div>
</template>

<script>

</script>

<style scoped>
.elementView{
   float: left;
   line-height: 100px;
   text-align: center;
   margin: 5px;
   width: 280px;
   height: 100px;
   background-color: bisque;
}
</style>

bodyView

<template>
   <div class="bodyView">
    i am body
    <selfButton></selfButton>
   </div>
</template>

<script>
</script>

<style scoped>
.bodyView{
    float: left;
    line-height: 250px;
    text-align: center;
    margin: 5px;
    width: 280px;
    height: 250px;
    background-color: blue;
    color: chocolate;
}
</style>

foodView

<template>
   <div class="foodView">
    i am food
    <selfButton></selfButton>
   </div>
</template>

<script>
</script>

<style scoped>
.foodView{
    float: left;
    line-height: 100px;
    text-align: center;
    width: 280px;
    margin: 5px;
    height: 100px;
    background-color: bisque;
    color:violet;
}
</style>

如上组建均要在app.vue中注册才能使用

该方法将这三个组件注册成为了局部组件

要想注册全局组件,则除了写对应的.vue文件外,还需要在main.js中进行导入和配置,完成这两步操作即可在所有的组件内使用定义好的全局组件

(此处使用了element插件库的样式)

<template>
   <div class="">
      <el-button type="danger" icon="el-icon-delete" circle></el-button>
   </div>
</template>

<script>

</script>

<style scoped>

</style>

最终结果如图所示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值