1 slot插槽 (内容分发)
1 2 3 4
|
a. 单个slot b. 具名slot *混合父组件的内容与子组件自己的模板-->内容分发 *父组件模板的内容在父组件作用域内编译;子组件模板的内容在子组件作用域内编译。
|
1.1 基本使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script> </head> <body> <divid="box"> <child1>aaa</child1>
</div> </body> <script> var bus = new Vue() Vue.component('child1', { template: `<div> 首页 <slot></slot> </div>`,
}) var vm = new Vue({ el: '#box',
}) </script> </html>
|
1.2 插槽应用场景1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script> </head> <body> <divid="box">
<swiper> <pv-for="data in 4">{{data}}</p> </swiper>
<swiper> <img:src="data"v-for="data in 5"> </swiper>
</div> </body> <script> var bus = new Vue() Vue.component('swiper', { template: `<div> <slot></slot> </div>`,
}) var vm = new Vue({ el: '#box',
}) </script> </html>
|
1.3 插槽应用场景2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script> </head> <body> <divid="box"> <navbar> <button @click="isShow=!isShow">点我显示隐藏</button></navbar>
<swiperv-if="isShow"></swiper> </div> </body> <script> Vue.component('navbar', { template: `<div> navbar <slot></slot> </div>`,
}) Vue.component('swiper', { template: `<div> <p>111</p> <p>222</p> <p>333</p> </div>`,
}) var vm = new Vue({ el: '#box', data:{ isShow:true }
}) </script> </html>
|
1.4 具名插槽
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script> </head> <body> <divid="box">
<navbar> <pslot="a">pppp</p> <divslot="b">bbbb</div> </navbar> </div> </body> <script> Vue.component('navbar', { template: `<div> <slotname="a"></slot> navbar <slotname="b"></slot> </div>`,
}) var vm = new Vue({ el: '#box', data:{
}
}) </script> </html>
|
2 transition过渡
1 2 3 4 5 6 7 8 9
|
Vue 在插入、更新或者移除 DOM 时,提供多种不同方式的应用过渡效果。 (1)单元素/组件过渡 * css过渡 * css动画 * 结合animate.css动画库 (2) 多个元素过渡(设置key) *当有相同标签名的元素切换时,需要通过 key 特性设置唯一的值来标记以让 Vue 区分它们,否则 Vue 为了效率只会替换相同标签内部的内容。 mode:in-out ; out-in (3)多个组件过渡 (4)列表过渡(设置key) *<transition-group>不同于 transition, 它会以一个真实元素呈现:默认为一个 <span>。你也可以通过tag 特性更换为其他元素。 * 提供唯一的 key 属性值
|
3 生命周期
1 2
|
i. 生命周期各个阶段https://cn.vuejs.org/v2/guide/instance.html#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%9B%BE%E7%A4%BA ii. 生命周期钩子函数的触发条件与作用
|
4 swiper学习
https://www.swiper.com.cn/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script>
<linkrel="stylesheet"href="https://unpkg.com/swiper/swiper-bundle.min.css"> <scriptsrc="https://unpkg.com/swiper/swiper-bundle.min.js"></script> <style> .swiper-container { width: 80%; height: 600px; } </style>
</head> <body> <divid="box">
<divclass="swiper-container"> <divclass="swiper-wrapper"> <divclass="swiper-slide"v-for="data in datalist">
{{data}} <imgsrc="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608145297276&di=dd396caeaa0bb6a2a50609a109cd3120&imgtype=0&src=http%3A%2F%2Fattachments.gfan.com%2Fforum%2Fattachments2%2Fday_110915%2F1109151356c0717d7e6a91e985.jpg"alt=""> </div> </div> <divclass="swiper-pagination"></div> <divclass="swiper-button-prev"></div> <divclass="swiper-button-next"></div>
<divclass="swiper-scrollbar"></div> </div> </div> </body> <script> var vm = new Vue({ el: '#box', data: { datalist: [], }, mounted() { setTimeout(() => { this.datalist = ['111', '222', '333'] }, 2000)
</script> </html>
|
5 自定义组件的封装
自定义封装swiper组件(基于swiper)
注意: 防止swipe初始化过早
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script>
<linkrel="stylesheet"href="https://unpkg.com/swiper/swiper-bundle.min.css"> <scriptsrc="https://unpkg.com/swiper/swiper-bundle.min.js"></script> <style> .swiper-container { width: 80%; height: 600px; } </style>
</head> <body> <divid="box"> {{name}}
<swipper:key="datalist.length">
<divclass="swiper-slide"v-for="data in datalist"> <imgsrc="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1608145297276&di=dd396caeaa0bb6a2a50609a109cd3120&imgtype=0&src=http%3A%2F%2Fattachments.gfan.com%2Fforum%2Fattachments2%2Fday_110915%2F1109151356c0717d7e6a91e985.jpg"alt=""> </div> </swipper> </div> </body> <script>
Vue.component('swipper',{ template:` <div class="swiper-container"> <div class="swiper-wrapper"> <slot></slot> </div> <div class="swiper-pagination"></div> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> <div class="swiper-scrollbar"></div> </div> `, mounted(){ console.log('子组件mounted') var mySwiper = new Swiper('.swiper-container', {
loop: true,
pagination: { el: '.swiper-pagination', },
navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', },
scrollbar: { el: '.swiper-scrollbar', },
}) },
}) var vm = new Vue({ el: '#box', data: { name:'lqz', datalist: [], }, mounted() { setTimeout(() => { this.datalist = ['111', '222', '333'] }, 2000)
},
}) </script> </html>
|
6 自定义指令
1 2 3 4 5 6
|
(1)自定义指令介绍 directives (2)钩子函数* 参数 el,binding,vnode(vnode.context)* bind,inserted,update,componentUpdated,unbind (3)函数简写 (4)自定义指令-轮播 *inserted 插入最后一个元素时调用(vnode.context.datalist.length-1) *this.$nextTick()
|
6.1 基本使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script>
</head> <body> <divid="box"> <divv-mystyle>divdiv我是div</div> </div> </body> <script>
</script> </html>
|
6.2 让所有使用自定义指令的标签背景都变红色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script>
</head> <body> <divid="box"> <divv-mystyle>divdiv我是div</div> <pv-mystyle>我是p,用了自定义指令,会变红</p> </div> </body> <script>
</script> </html>
|
6.3 用户指定自定义指令的背景色,修改变量,背景变化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script>
</head> <body> <divid="box"> <divv-mystyle="'red'">divdiv我是div</div> <pv-mystyle="mycolor">我是p,用了自定义指令,会变红</p> </div> </body> <script>
</script> </html>
|
6.4 通过指令控制swipper初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="js/vue.js"></script>
<linkrel="stylesheet"href="https://unpkg.com/swiper/swiper-bundle.min.css"> <scriptsrc="https://unpkg.com/swiper/swiper-bundle.min.js"></script> <style> .swiper-container { width: 80%; height: 600px; } </style>
</head> <body> <divid="box">
<divclass="swiper-container"> <divclass="swiper-wrapper"> <divclass="swiper-slide"v-for="(data,i) in datalist"v-comp="{index:i,length:datalist.length}">
{{data}}
</div> </div> <divclass="swiper-pagination"></div> <divclass="swiper-button-prev"></div> <divclass="swiper-button-next"></div>
<divclass="swiper-scrollbar"></div> </div> </div> </body> <script> Vue.directive('comp', { inserted(el, input) { console.log(input.value)
</script> </html>
|
7 过滤器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
<!DOCTYPE html> <htmllang="en"> <head> <metacharset="UTF-8"> <title>Title</title> <scriptsrc="https://unpkg.com/axios/dist/axios.min.js"></script> <scriptsrc="js/vue.js"></script> </head> <body> <divid="box"> <ul> <liv-for="data in datalist"> <h2>{{data.nm}}</h2> <br>
<img:src="data.img | myChange"alt=""> </li> </ul> </div> </body> <script>
</script> </html>
|
每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)