slot 作用域插槽
- 使用流程
- 在组件的模板中书写slot插槽,并将当前组件的数据通过 v-bind 绑定在 slot标签上
- 在组件使用时,通过slot-scope = “slotProp” 来接收slot标签身上绑定的数据
- 通过 slotProp.xxx 就可以进行使用了
<!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>
<Hello>
<template v-slot:default = "slotProp">
{
{ slotProp.msg }}
</template>
</Hello>
</div>
<template id="hello">
<div>
<slot name = "default" :msg = "msg"></slot>
</div>
</template>
new Vue({
components: {
'Hello': {
template: '#hello',
data () {
return {
msg: 'hello'
}
}
}
}
}).$mount('#app')
属性验证
案例: 价格的增加 , 拿到的数据必须做验证 309 + 10 319 30910
<!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">