应无所住,而生其心
排名
3
文章
317
粉丝
22
评论
14
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

vue,vue3组件封装,vue组件模板。简单组件模板。基础组件模板。vue引入自定义的组件。vue使用自定义的组件。插槽slot使用。vue封装格子效果,一块一块的grid布局效果

5263人阅读 2020/9/16 10:19 总访问:5416065 评论:0 收藏:0 手机
分类: 前端

vue封装组件的简单模板

贴一个简单模板方便自定义组件的时候直接复制

  1. <template>
  2. <div class="app-container">
  3. 组件模板
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. // 组件名字
  9. name: 'VideoItem',
  10. // 组件参数
  11. props: {
  12. percentage: {
  13. type: Number,
  14. default: 0
  15. },
  16. title: {
  17. type: String,
  18. default: ''
  19. },
  20. },
  21. data() {
  22. return {
  23. courseID: '',
  24. labId: '',
  25. chapter: {},
  26. }
  27. },
  28. mounted() {
  29. this.initPic()
  30. },
  31. // 组件方法
  32. methods: {
  33. initPic() {
  34. }
  35. }
  36. }
  37. </script>
  38. <style scoped="scoped" lang="scss">
  39. </style>

vue3简单的页面基础模板

  1. <template>
  2. <div class="app-container">
  3. <el-card>
  4. <div>
  5. {{ state.title }}
  6. </div>
  7. <div>
  8. <el-select v-model="state.schoolID" class="select-level" size="default" placeholder="请选择校区" clearable>
  9. <el-option v-for="item in schoolList" :key="item.id" :value="item.id" :label="item.name" />
  10. </el-select>
  11. </div>
  12. </el-card>
  13. </div>
  14. </template>
  15. <script setup lang="ts" name="tasks">
  16. import { defineAsyncComponent, reactive, onMounted, toRefs, ref } from 'vue';
  17. const state = reactive({
  18. title: '更新',
  19. schoolID:null,
  20. schoolList: [
  21. {
  22. id: 1,
  23. name: "小"
  24. },
  25. {
  26. id: 2,
  27. name: "大"
  28. }
  29. ],
  30. })
  31. // 这样暴露出来后页面可以直接使用schoolList,而不需要state.schoolList
  32. const { schoolList } = toRefs(state)
  33. onMounted(() => {
  34. })
  35. </script>
  36. <style scoped="scoped" lang="scss">
  37. .app-container {
  38. padding: 15px
  39. }
  40. </style>

vue引入自定义的组件:

引入组件,@表示src的路径,也可以使用..等相对路径路径来引入

  1. import FeedBack from '@/pages/feedback/feedback.vue'
  2. export default {
  3. components: {
  4. FeedBack
  5. }
  6. }

页面上使用即可

  1. <FeedBack/>

给组件里边传递参数这样就行

  1. <FeedBack title="学习活跃度" style="height:100%" />

完整一点的代码

  1. <template>
  2. <div class="lab-course">
  3. <!--引入自定义组件-->
  4. <FeedBack/>
  5. </div>
  6. </template>
  7. <script>
  8. //@表示src的路径,也可以使用..等相对路径路径来引入
  9. import FeedBack from '@/pages/feedback/feedback.vue'
  10. export default {
  11. components: {
  12. FeedBack
  13. },
  14. data() {
  15. return {
  16. courseID: ''
  17. }
  18. },
  19. mounted() {
  20. },
  21. methods: {
  22. }
  23. }
  24. </script>
  25. <style lang="scss">
  26. </style>

封装一个标题高亮格子的组件

效果如下,这样一块一块的格子效果,内容是做的插槽:

代码:

  1. <template>
  2. <div>
  3. <div class="zuxia_grid">
  4. <div class="title_com">
  5. <div class="title_com_tag">
  6. </div>
  7. <div class="title_com_text">
  8. {{title}}
  9. </div>
  10. </div>
  11. <div class="content">
  12. <slot name="content"></slot>
  13. </div>
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. // 组件名字
  20. name: 'ZuxiaGrid',
  21. // 组件参数
  22. props: {
  23. percentage: {
  24. type: Number,
  25. default: 0
  26. },
  27. title: {
  28. type: String,
  29. default: '默认标题'
  30. },
  31. },
  32. data() {
  33. return {
  34. }
  35. },
  36. components: {
  37. },
  38. mounted() {
  39. },
  40. beforeDestroy () {
  41. },
  42. methods: {
  43. }
  44. }
  45. </script>
  46. <style lang="scss" scoped>
  47. .zuxia_grid{
  48. height: 100%;
  49. width: 100%;
  50. background-image: url("../../assets/img/boxbox4.png");
  51. border: 1px solid #0E466C;
  52. border-radius: 4px;
  53. .title_com{
  54. display: flex;
  55. padding-top: 22px;
  56. margin-left: 9px;
  57. .title_com_tag
  58. {
  59. width: 8px;
  60. height: 19px;
  61. background: #23FFFC;
  62. margin-left:10px;
  63. margin-top: -1px;
  64. }
  65. .title_com_text
  66. {
  67. margin-left: 10px;
  68. font-size: 22px;
  69. // 字体设置成白色会更亮一点
  70. color:#fff;
  71. // text-shadow:0px -5px 5px #B4D3F6;
  72. text-shadow: 0 0 10px #DBE5F0, 0 0 20px #DBE5F0, 0 0 30px #DBE5F0, 0 0 40px #B4D3F6, 0 0 70px #B4D3F6, 0 0 80px #B4D3F6, 0 0 100px #B4D3F6, 0 0 150px #B4D3F6;
  73. //从白色到B4D3F6这个颜色看着会更亮一点,因为两个颜色间隔更多
  74. // text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #B4D3F6, 0 0 70px #B4D3F6, 0 0 80px #B4D3F6, 0 0 100px #B4D3F6, 0 0 150px #B4D3F6;
  75. }
  76. }
  77. .content
  78. {
  79. margin-left: 16px;
  80. margin-right: 16px;
  81. }
  82. }
  83. </style>

使用

先引用组件

  1. import ZuxiaGrid from '@/views/component/zuxia_grid.vue'
  2. components: {
  3. ZuxiaGrid
  4. },

然后在需要的地方引用即可,这里引用了三次

  1. <div class="middle_com">
  2. <div class="middle_top">
  3. <ZuxiaGrid title="学习活跃度" style="height:100%">
  4. <template #content>
  5. 11
  6. </template>
  7. </ZuxiaGrid>
  8. </div>
  9. <div class="middle_bottom">
  10. <ZuxiaGrid title="学习趋势图" style="height:100%">
  11. <template #content>
  12. 22
  13. </template>
  14. </ZuxiaGrid>
  15. </div>
  16. </div>
  17. <div class="right_com">
  18. <ZuxiaGrid title="分析趋势" style="height:100%">
  19. <template #content>
  20. 33
  21. </template>
  22. </ZuxiaGrid>
  23. </div>

封装的组件更新一下增加了事件的传递,以及右边的三个小点小菜单等

  1. <template>
  2. <div>
  3. <div class="zuxia_grid">
  4. <div class="title_com">
  5. <div class="title_com_tag">
  6. </div>
  7. <div class="title_com_text">
  8. {{ title }}
  9. </div>
  10. <!-- 右边的三个小点小菜单 -->
  11. <div class="title_com_menu" v-if="ShowtitleMenu" @click="$emit('menuclick', '可以传递需要的参数回去')">
  12. <div class="title_com_menu_dot"></div>
  13. <div class="title_com_menu_dot"></div>
  14. <div class="title_com_menu_dot"></div>
  15. </div>
  16. </div>
  17. <div class="content">
  18. <slot name="content"></slot>
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. // 组件名字
  26. name: 'ZuxiaGrid',
  27. // 组件参数
  28. props: {
  29. percentage: {
  30. type: Number,
  31. default: 0
  32. },
  33. title: {
  34. type: String,
  35. default: '默认标题'
  36. },
  37. ShowtitleMenu:
  38. {
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. data() {
  44. return {
  45. }
  46. },
  47. components: {
  48. },
  49. mounted() {
  50. },
  51. beforeDestroy() {
  52. },
  53. methods: {
  54. }
  55. }
  56. </script>
  57. <style lang="scss" scoped>
  58. .zuxia_grid {
  59. height: 100%;
  60. width: 100%;
  61. background-image: url("../../assets/img/boxbox4.png");
  62. border: 1px solid #0E466C;
  63. border-radius: 4px;
  64. .title_com {
  65. display: flex;
  66. padding-top: 22px;
  67. margin-left: 9px;
  68. position: relative;
  69. .title_com_tag {
  70. width: 8px;
  71. height: 19px;
  72. background: #23FFFC;
  73. margin-left: 10px;
  74. margin-top: -1px;
  75. }
  76. .title_com_text {
  77. margin-left: 10px;
  78. font-size: 22px;
  79. // 字体设置成白色会更亮一点
  80. color: #fff;
  81. // text-shadow: 0 0 100px #B4D3F6, 0 0 60px #B4D3F6, 0 0 70px #B4D3F6
  82. // 然后把数值改大一点就更暗了,数值越大越暗,可以根据需要调整
  83. text-shadow: 0 0 40px #B4D3F6, 0 0 80px #B4D3F6, 0 0 90px #B4D3F6 // 在把前面的一个删除了,后面的几个删除了,看着就更暗了,感觉就要舒服一些了
  84. //text-shadow: 0 0 30px #B4D3F6, 0 0 70px #B4D3F6, 0 0 80px #B4D3F6
  85. //把前面几个删除了看着就要暗一些了也
  86. //text-shadow: 0 0 30px #DBE5F0, 0 0 40px #B4D3F6, 0 0 70px #B4D3F6, 0 0 80px #B4D3F6, 0 0 100px #B4D3F6, 0 0 150px #B4D3F6;
  87. // 这个太亮了后面要调暗一点
  88. //text-shadow: 0 0 10px #DBE5F0, 0 0 20px #DBE5F0, 0 0 30px #DBE5F0, 0 0 40px #B4D3F6, 0 0 70px #B4D3F6, 0 0 80px #B4D3F6, 0 0 100px #B4D3F6, 0 0 150px #B4D3F6;
  89. //从白色到B4D3F6这个颜色看着会更亮一点,因为两个颜色间隔更多
  90. // text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #fff, 0 0 40px #B4D3F6, 0 0 70px #B4D3F6, 0 0 80px #B4D3F6, 0 0 100px #B4D3F6, 0 0 150px #B4D3F6;
  91. }
  92. .title_com_menu {
  93. display: flex;
  94. position: absolute;
  95. right: 16px;
  96. margin-top: 5px;
  97. cursor: pointer;
  98. .title_com_menu_dot {
  99. width: 5px;
  100. height: 5px;
  101. border-radius: 50%;
  102. background-color: #fff;
  103. margin-left: 5px;
  104. }
  105. }
  106. }
  107. .content {
  108. margin-left: 16px;
  109. margin-right: 16px;
  110. }
  111. }
  112. </style>

引用组件的时候可以使用组件里边封装的事件处理menuclick

  1. <ZuxiaGrid style="height:100%" title="教学动态" :ShowtitleMenu="true" @menuclick="menuclick()">

其他参考

https://www.tnblog.net/aojiancc2/article/details/4250


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价