博客列表 >认识组件库

认识组件库

初见
初见原创
2021年11月04日 19:43:55766浏览

Vue的组件化开发

  • 组件化是Vue的精髓,Vue开发就是由一个一个的组件构成的。
  • 组件的分类:
    页面级组件
    业务上可复用的基础组件
    与业务无关的独立功能组件
  • 组件开发三要素(prop,自定义事件,slot)
    prop用于定义组件的属性。
    自定义事件用于触发组件的事件。
    slot用于组件功能的扩展。
  • 组件设计需要考虑的问题
    可扩展性强
    组件中方法函数的抽离,便于复用,适用程度高。
    文档清楚详细
    颗粒度合适,适度抽象
    功能尽可能单一,代码行数适中

样式 lang(scss) scoped(局域)

  1. Index.vue
  2. <template>
  3. <div>
  4. Index 根组件
  5. <hr>
  6. <!-- <MyBut></MyBut>-->
  7. <my-but></my-but>
  8. <hr>
  9. <sub-btn></sub-btn>
  10. </div>
  11. </template>
  12. <script>
  13. import MyBut from "./components/MyBut";
  14. import SubBtn from "./components/SubBtn";
  15. export default {
  16. name:'Index',
  17. components:{
  18. MyBut,
  19. SubBtn
  20. },
  21. data(){
  22. },
  23. methods:{
  24. }
  25. }
  26. </script>
  27. <style scoped>
  28. </style>
  29. MyBut.vue
  30. <template>
  31. <div>
  32. MyBut子组件<hr>
  33. <div class="but">
  34. <sub-btn></sub-btn>
  35. </div>
  36. </div>
  37. </template>
  38. <script>
  39. import SubBtn from "./SubBtn";
  40. export default {
  41. name: "MyBut",
  42. components:{
  43. SubBtn
  44. }
  45. }
  46. </script>
  47. <style scoped lang="scss">
  48. .but{
  49. width: 80px;
  50. height: 30px;
  51. line-height: 30px;
  52. box-shadow: 0px 3px 5px #666;
  53. text-align: center;
  54. background: coral;
  55. color: aliceblue;
  56. :hover{
  57. cursor: pointer;
  58. background: brown;
  59. }
  60. }
  61. </style>
  62. SubBtn.vue
  63. <template>
  64. <div class="but">
  65. 确定
  66. </div>
  67. </template>
  68. <script>
  69. export default {
  70. name: "SubBtn"
  71. }
  72. </script>
  73. <style scoped>
  74. </style>

vue

  • Vue父子组件之间的通信

父传子

  1. <template>
  2. <div>
  3. <my-but one="hello" two="world" ></my-but>
  4. <hr>
  5. <my-but :one="msg" :two="user" :three="obj"></my-but>
  6. </div>
  7. </template>
  8. <script>
  9. import MyBut from "./components/MyBut";
  10. import SubBtn from "./components/SubBtn";
  11. export default {
  12. name:'Index',
  13. components:{
  14. MyBut,
  15. SubBtn
  16. },
  17. data(){
  18. return{
  19. msg:'this msg',
  20. user:['aaa','bbb','ccc'],
  21. obj:{
  22. name:'xiaoming',
  23. age:30
  24. }
  25. }
  26. },
  27. methods:{
  28. }
  29. }
  30. </script>
  31. <template>
  32. <div>
  33. <div class="but">
  34. {{one}} --
  35. <span v-for="item in two" :key="item">{{item}}</span>
  36. </div>
  37. {{three.name}}
  38. </div>
  39. </template>
  40. <script>
  41. import SubBtn from "./SubBtn";
  42. export default {
  43. name: "MyBut",
  44. // props:[
  45. // 'one','two'
  46. // ],
  47. props:{
  48. one:{
  49. type:String,
  50. require:true
  51. },
  52. two:{
  53. type:Array,
  54. },
  55. three:{
  56. type:Object,
  57. default:{
  58. name:'xiaohua',
  59. age:11
  60. }
  61. }
  62. },
  63. components:{
  64. SubBtn
  65. }
  66. }
  67. </script>
  68. <style scoped lang="scss">
  69. .but{
  70. width: 180px;
  71. height: 180px;
  72. line-height: 30px;
  73. box-shadow: 0px 3px 5px #666;
  74. text-align: center;
  75. background: coral;
  76. color: aliceblue;
  77. :hover{
  78. cursor: pointer;
  79. background: brown;
  80. }
  81. }
  82. </style>

父传子

子传父
子传父通过事件

  1. <template>
  2. <div>
  3. <sunny @mynum="fun3"></sunny>
  4. {{count}}
  5. </div>
  6. </template>
  7. <script>
  8. import MyBut from "./components/MyBut";
  9. import SubBtn from "./components/SubBtn";
  10. import Sunny from "./components/Sunny";
  11. export default {
  12. name:'Index',
  13. components:{
  14. MyBut,
  15. SubBtn,
  16. Sunny
  17. },
  18. data(){
  19. return{
  20. msg:'this msg',
  21. count:0,
  22. user:['aaa','bbb','ccc'],
  23. obj:{
  24. name:'xiaoming',
  25. age:30
  26. }
  27. }
  28. },
  29. methods:{
  30. fun3(num){
  31. console.log('我是父组件')
  32. console.log(num)
  33. this.count +=num
  34. }
  35. }
  36. }
  37. </script>
  38. <style scoped>
  39. </style>
  40. <template>
  41. <div>
  42. <div @click="numchange(2)" class="but"></div>
  43. </div>
  44. </template>
  45. <script>
  46. export default {
  47. name: "Sunny",
  48. data(){
  49. return{
  50. num:0
  51. }
  52. },
  53. methods:{
  54. numchange(num){
  55. console.log('我是子组件');
  56. this.$emit('mynum',num)
  57. }
  58. }
  59. }
  60. </script>
  61. <style scoped lang="scss">
  62. .but{
  63. width: 180px;
  64. height: 180px;
  65. line-height: 30px;
  66. box-shadow: 0px 3px 5px #666;
  67. text-align: center;
  68. background: coral;
  69. color: aliceblue;
  70. :hover{
  71. cursor: pointer;
  72. background: brown;
  73. }
  74. }
  75. </style>

子传父

  • Vue父子组件之间的访问方式

子组件调用父组件的方法:$parent或$root
父组件调用子组件的方法:$children或 $refs

  1. 子访问父
  2. this.$parent.changen(); //调用父的方法
  3. this.$parent.count; //调用父的属性
  4. this.$parent.$parent.msg; //调用父的父成员
  5. this.$root.appmet(); //调用根的成员
  6. 父访问子
  7. console.log('这是父组件中的two方法')
  8. this.$refs.aaa.changeone(); //调用别名为aaa的子组件方法
  9. this.$refs.bbb.changeone(); //调用别名为bbb的子组件方法
  10. console.log(this.$refs.aaa.num) //访问子组件成员
  11. console.log(this.$refs.bbb.num) //访问子组件成员
  12. <div @click="fun5">点击</div>
  13. <sunny ref="aaa"></sunny>
  14. fun5(){
  15. this.$refs.aaa.sun();
  16. console.log(this.$refs.aaa.test)
  17. }
  18. data(){
  19. return{
  20. num:0,
  21. test:'test111'
  22. }
  23. }
  24. sun(){
  25. console.log('子组件测试')
  26. }

父调用子

  • 插槽slot
  1. <template>
  2. <div>
  3. <my-slot>
  4. <template v-slot:default>
  5. <button>OK</button>
  6. </template>
  7. <template v-slot:one>
  8. <button> 确定</button>
  9. </template>
  10. <!-- v-slot == # -->
  11. <template #two="myuser">
  12. <button> {{ myuser.user.name }}</button>
  13. </template>
  14. </my-slot>
  15. <br>
  16. <my-slot>
  17. <a href="">删除</a>
  18. </my-slot>
  19. <br>
  20. <my-slot>
  21. <span>aaaa</span>
  22. </my-slot>
  23. <br>
  24. </div>
  25. </template>
  26. <script>
  27. import MyBut from "./components/MyBut";
  28. import SubBtn from "./components/SubBtn";
  29. import Sunny from "./components/Sunny";
  30. import MySlot from "./components/MySlot";
  31. export default {
  32. name:'Index',
  33. components:{
  34. MyBut,
  35. SubBtn,
  36. Sunny,
  37. MySlot
  38. },
  39. data(){
  40. return{
  41. msg:'this msg',
  42. count:0,
  43. user:['aaa','bbb','ccc'],
  44. obj:{
  45. name:'xiaoming',
  46. age:30
  47. }
  48. }
  49. },
  50. methods:{
  51. fun3(num){
  52. console.log('我是父组件')
  53. console.log(num)
  54. this.count +=num
  55. },
  56. fun(){
  57. console.log('我是fun')
  58. },
  59. fun5(){
  60. this.$refs.aaa.sun();
  61. console.log(this.$refs.aaa.test)
  62. }
  63. }
  64. }
  65. </script>
  66. <style scoped>
  67. </style>
  68. <template>
  69. <div>
  70. <slot></slot>
  71. <br>
  72. this slot
  73. <br>
  74. <!-- 声明具名插槽 -->
  75. <slot name="one" :user="user"></slot>
  76. <br>
  77. <slot name="two" :user="user"></slot>
  78. </div>
  79. </template>
  80. <script>
  81. export default {
  82. name: "MySlot",
  83. data(){
  84. return{
  85. user:{name:'xiaoming',age:30}
  86. }
  87. }
  88. }
  89. </script>
  90. <style scoped>
  91. </style>

插槽

  • Vue3中组件的生命周期函数

生命周期函数

  1. <button @click="show=!show">销毁</button>
  2. <hr>
  3. <!-- 销毁-->
  4. <xiao-hui v-if="show"></xiao-hui>
  5. <hr>
  6. <!-- 隐藏-->
  7. <xiao-hui v-show="show"></xiao-hui>
  8. <br>
  9. <!-- 保持生命-->
  10. <keep-alive>
  11. <xiao-hui v-if="show"></xiao-hui>
  12. </keep-alive>
  13. show:true,
  14. <button @click="num++">{{num}} ++</button>
  15. beforeCreate() {
  16. console.log('1、在创建组件之前')
  17. },
  18. created() {
  19. console.log('2、组件已经创建完成')
  20. },
  21. beforeMount() {
  22. console.log('3、在模板挂载之前')
  23. },
  24. mounted() {
  25. console.log('4、在模板挂载完成之后')
  26. },
  27. beforeUpdate() {
  28. console.log('5、在内容有改变之前')
  29. },
  30. updated() {
  31. console.log('6、在数据改变之后')
  32. },
  33. beforeUnmount() {
  34. console.log('7、在组件销毁之前')
  35. },
  36. unmounted() {
  37. console.log('8、在组件销毁之后')
  38. },
  39. activated() {
  40. console.log('9、被 keep-alive 缓存的组件激活时调用')
  41. },
  42. deactivated() {
  43. console.log('10、被 keep-alive 缓存的组件失活时调用')
  44. },
  45. data(){
  46. return{
  47. num:0
  48. }

生命周期

  • Element-UI/ElementPlus组件库
  1. <template>
  2. <div class="block">
  3. <el-timeline>
  4. <el-timeline-item
  5. v-for="(activity, index) in activities"
  6. :key="index"
  7. :icon="activity.icon"
  8. :type="activity.type"
  9. :color="activity.color"
  10. :size="activity.size"
  11. :hollow="activity.hollow"
  12. :timestamp="activity.timestamp"
  13. >
  14. {{ activity.content }}
  15. </el-timeline-item>
  16. </el-timeline>
  17. </div>
  18. </template>
  19. <script>
  20. import {ElTimeline, ElTimelineItem} from 'element-plus';
  21. import 'element-plus/dist/index.css';
  22. import { MoreFilled } from '@element-plus/icons';
  23. export default {
  24. name: "Ele",
  25. components:{
  26. ElTimeline,
  27. ElTimelineItem
  28. },
  29. data() {
  30. return {
  31. activities: [
  32. {
  33. content: 'Custom icon',
  34. timestamp: '2018-04-12 20:46',
  35. size: 'large',
  36. type: 'primary',
  37. icon: MoreFilled,
  38. },
  39. {
  40. content: 'Custom color',
  41. timestamp: '2018-04-03 20:46',
  42. color: '#0bbd87',
  43. },
  44. {
  45. content: 'Custom size',
  46. timestamp: '2018-04-03 20:46',
  47. size: 'large',
  48. },
  49. {
  50. content: 'Custom hollow',
  51. timestamp: '2018-04-03 20:46',
  52. type: 'primary',
  53. hollow: true,
  54. },
  55. {
  56. content: 'Default node',
  57. timestamp: '2018-04-03 20:46',
  58. },
  59. ],
  60. }
  61. },
  62. }
  63. </script>
  64. <style scoped>
  65. </style>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议