组件的初步使用

组件的传参

    <div id="blogColumnPayAdvert">
        <div class="column-group">
            <div class="column-group-item column-group0 column-group-item-one">
                <div class="item-l">
                    <a class="item-target" href="https://blog.csdn.net/huijie_0716/category_11938102.html" target="_blank" title="vue" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.6332&quot;}">
                        <img class="item-target" src="https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64" alt="">
                        <span class="title item-target">
                            <span>
                            <span class="tit">vue</span>
                                <span class="dec">专栏收录该内容</span>
                            </span>
                        </span>
                    </a>
                </div>
                <div class="item-m">
                    <span>2 篇文章</span>
                    <span>0 订阅</span>
                </div>
                <div class="item-r">
                        <a class="item-target article-column-bt articleColumnFreeBt" data-id="11938102">订阅专栏</a>
                </div>
            </div>
        </div>
    </div>
<article class="baidu_pl">
    <div id="article_content" class="article_content clearfix">
    <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-b3c43d3711.css">
            <div id="content_views" class="htmledit_views">
                <h1><a name="t0"></a>组件的定义与使用</h1> 

组件的传参

父App.vue 传给子CounterCom.vue

主要使用的是props

CounterCom.vue 组件中
1.接收参数并定义默认值
props:{
    "num":{type:Number,default:1}
}
2.使用参数num
data(){
    return {counter:this.num}
}


App.vue文件中
<CounterCom :num="10">


使用props
父传给子的数组是只读的(做默认值,读取显示)
不能进行修改

 实现点击加1功能

 子组件数据传递给父组件

使用的事件 $emit


CounterCom.vue
<button @click="counter++; $emit('counterchange',counter)">


App.vue
<CounterCom @counterchange="n=$event">


$emit(事件名,事件值)  发送一次事件,事件名(counterchange)和事件值(counter) 是自定义的

$event 固定写法,事件的值(counterchange 事件的值,也是子组件$emit的第二个参数)

组件设计(购物车加减个数)

 props

  1.     默认值:value
  2.     最大值:max
  3.     最小值:min
  4.     步进制:step(一次加多 多少)

data
    被改变的值:num 加add
methods 
    加add
    减minus
事件
    numchange 数字发送变化
template
    <button>-</button>
    <input/>
    <button>+</button>

组件设计(幻灯片)


 
 
  1. <template>
  2. <div class="swiper" @mousemove="overHd" @mouseout="auto">
  3. <!-- item 从1开始 -->
  4. <div class="swiper-item" v-for="(item,ind) in gallery" :key="item" v-show="ind===index">
  5. <img :src="item" alt="" width="100%">
  6. </div>
  7. <!-- 指示小点 -->
  8. <div class="thumbs">
  9. <!-- 当index值等于item的值添加.active 类 -->
  10. <!-- 单击改变index的值,切换的轮播图 -->
  11. <span class="thumb" @click="index=item-1" :class="{'active':item===index+1}" v-for="item in gallery.length" :key="item">{{item}} </span>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. // item 1,2,3,4
  17. // index0
  18. export default{
  19. props:{
  20. // 引用类型的默认值必须是函数返回数据
  21. gallery:{
  22. type: Array,
  23. default( ){ return []}
  24. }
  25. },
  26. data( ){
  27. return {
  28. // 默认显示
  29. index: 0,
  30. stopID: null, //停止id
  31. }
  32. },
  33. // 当组件创建完毕,执行自动播放
  34. created( ) {
  35. this. auto();
  36. },
  37. methods:{
  38. // 自动播放
  39. auto( ){
  40. this. stopID= setInterval( ()=>{
  41. // 每隔2500毫秒让index值加1
  42. // 检查index的边界
  43. this. index++;
  44. this. check()
  45. }, 2500)
  46. },
  47. // 鼠标移入停止
  48. overHd( ){
  49. clearInterval( this. stopID);
  50. },
  51. // 检查边界
  52. check( ){
  53. // 如果index等于gallery长度 让index值为0
  54. if( this. index>= this. gallery. length){
  55. this. index= 0
  56. }
  57. }
  58. }
  59. }
  60. </script>
  61. <style scoped="scoped">
  62. .swiper{
  63. position: relative;
  64. }
  65. .thumbs{
  66. position: absolute;
  67. bottom: 10px;
  68. text-align: center;
  69. width: 100%;
  70. }
  71. .thumb{
  72. height: 15px;
  73. width: 15px;
  74. display: inline-block;
  75. border-radius: 15px;
  76. margin: 0 5px;
  77. background-color: #fff;
  78. /* 隐藏文本(首行缩进) */
  79. text-indent: 999em;
  80. }
  81. .thumb :hover{
  82. cursor: pointer;
  83. background-color: #f70;
  84. }
  85. .active{
  86. background-color: #f70;
  87. }
  88. </style>

 
 
  1. <template>
  2. <div>
  3. <h1>swiper幻灯片组件 </h1>
  4. <SwiperCom style="width: 800px;" :gallery="gallery"> </SwiperCom>
  5. </div>
  6. </template>
  7. <script>
  8. // 01导入组件
  9. import SwiperCom from './components/SwiperCom.vue'
  10. export default {
  11. components: {
  12. SwiperCom
  13. },
  14. data( ) {
  15. return {
  16. gallery: [
  17. "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/290270b6fc499fc5fcb653417e99fe91.jpg?w=632&h=340",
  18. "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5d1892854c8bb165e755d68bde287d71.jpg?w=632&h=340",
  19. "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/28c13d0d11b38ec17fa5d83bc6ba5912.jpg?w=632&h=340"
  20. ]
  21. }
  22. }
  23. }
  24. </script>
  25. <style>
  26. </style>

组件设计(跑马灯)


 
 
  1. //vue.app
  2. <template>
  3. <div>
  4. <h1>文字滚动 </h1>
  5. <!-- 使用组件 -->
  6. <MarQuee :inter='100' str="vue 学好,月薪过万,很容易!"> </MarQuee>
  7. <MarQuee :inter='200' str="我喜欢jsdjn!"> </MarQuee>
  8. </div>
  9. </template>
  10. <script>
  11. // 导入组件
  12. import MarQuee from './components/MarQuee.vue'
  13. export default{
  14. // 注册组件
  15. components:{
  16. MarQuee
  17. }
  18. }
  19. </script>
  20. <style>
  21. </style>
  22. //MarQuee.vue
  23. <template>
  24. <div>
  25. {{msg}}
  26. </div>
  27. </template>
  28. <script>
  29. export default {
  30. props:{
  31. inter:{ type: Number, default: 50}, //滚动的速度
  32. str:{ type: String, default: ''}, //滚动的初始文本
  33. },
  34. data( ){
  35. return{
  36. msg: this. str //显示文本
  37. }
  38. },
  39. created( ) {
  40. // 组件创建完毕,调用自动播放
  41. this. auto();
  42. },
  43. methods:{
  44. auto( ){
  45. setInterval( ()=>{
  46. // 把msg的文本第一个字符放到最后一个
  47. // slice(1)从第一截取到最后
  48. // slice(0,1)截取第0个字符
  49. this. msg= this. msg. slice( 1)+ this. msg. slice( 0, 1)
  50. }, this. inter)
  51. }
  52. }
  53. }
  54. </script>
  55. <style>
  56. </style>

生命周期

概念

每个组件从创建到销毁都会经历一系列特定的过程,称为生命周期
把过程执行的回调函数称为生命周期函数

作用

创建后发起ajax请求
挂载后操作dom
添加事件监听
销毁前移除间隔调用,事件监听
说明:并不是每个生命周期都必须使用 

 创建前后

beforeCreate创建前
    特点: 有this,没有data与methods

created创建后
    特点: 有data,没有$el,dom节点
    用处: ajax请求,定时器,事件监听

 挂载前后

beforeMount挂载前
    特点: 有$el,数据没有渲染

mounted挂载后
    特点: 有dom节点,数据也渲染
    用处: 操作节点,ajax请求

 更新前后

beforeUpdate更新前
    特点:会执行多次  数据更新,dom节点没有更新

updated更新完毕
    特点:会执行多次 数据更新,dom节点也更新

 销毁前后

beforeDestroy销毁前
    特点: 数据可以更新,视图已经不更新
    用处: 移除事件监听,停止定时器

destroyed销毁完毕
    特点: 没有this,切换视图与vue实例的联系

 

文章知识点与官方知识档案匹配,可进一步学习相关知识
Vue入门技能树Vue组件全局与局部组件 7960 人正在系统学习中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值