Vue高级

slot 作用域插槽
1. 旧: slot-scope

	* 使用流程

		* 在组件的模板中书写slot插槽,并将当前组件的数据通过 v-bind 绑定在 slot标签上(<slot name = "default" :msg = "msg"></slot>)
		* 在组件使用时,通过slot-scope = "slotProp" 来接收slot标签身上绑定的数据( <template slot = "default" slot-scope = "slotProp">)
		* 通过 slotProp.xxx 就可以进行使用了( <p> {{ slotProp.msg }} </p>)
	* <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body><div id="app"><Hello><template slot = "default" slot-scope = "slotProp"><p> {{ slotProp.msg }} </p></template></Hello></div><template id="hello"><div><slot name = "default" :msg = "msg"></slot></div></template></body><script src="../../lib/vue.js"></script><script>Vue.component('Hello',{template: '#hello',data () {return {msg: 'hello'}}})new Vue({el: '#app'})</script></html>
2. 新: v-slot
{{ slotProp.msg }}
new Vue({components: {'Hello': {template: '#hello',data () {return {msg: 'hello'}}}}}).$mount('#app')属性验证案例: 价格的增加 , 拿到的数据必须做验证 309 + 10 319 30910 1. props: [ 'msg' ] 没有进行验证,知识单纯的接收了一个父组件传递来的数据 2. props: { attr: attrType } 进行普通属性验证 3. props: { type: typeType, default: value } 这里的default是为这个属性设置初始值 4. props: { validator ( val ) { return boolean }} 可以进行一个条件的比较

注意: 除了以上形式的属性验证以外,将来工作中还可能遇到的是 第三方封装的类库 vue-validate vee-validate …
过滤器
1.
什么是过滤器? 用来格式化数据的一个函数 $ 10 ‘$’ + price 日期的格式化

vue 1.x 版本借鉴了 angular , 提供 10 个过滤器, 包括有: 日期 小数点位数保留 货币 大小写 等
Vue 2.x 废弃了这 10个过滤器,但是它提供了自定义过滤器的方式
1.
使用方式

	1. 

全局定义过滤器
2.

{{ time | timeFilter('/)}}

3. Vue.filter('timeFilter',function ( val,type ) { console.log( val ) //val 就是我们获得的数据 // return newVal return 的结果就是格式化之后的新数据 return的结果就是页面呈现的结果

var date = new Date ( val )

// 2019-6-26

return date.getFullYear() + type + ( date.getMonth() + 1 ) + type + date.getDate()
})

	1. 

局部定义过滤器

new Vue({
el: ‘#app’,
data: {
time: Date.now()
},
filters: { //过滤器的配置项
‘timeFilter’: function ( val,type ){
var date = new Date ( val )
return date.getFullYear() + type + ( date.getMonth() + 1 ) + type + date.getDate()
}
}
})
1.
过滤器要想获得我们的数据,要通过一个叫做 ‘管道符 | ’ 来获取数据
2.
过滤器是对已经有的数据进行格式化,也就是必须先有数据,在去格式化

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值