基本使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<cpn>
<button>123</button>
<p>111</p>
<div>222</div>
</cpn>
<cpn></cpn>
<cpn></cpn>
<cpn></cpn>
</div>
<template id="cpn">
<div>
<h2>i am zujian</h2>
<p>i am zujian hhh</p>
<slot><button>案例</button></slot>//默认值
</div>
</template>
</body>
<script src="vue.js"></script>
<script>
const app=new Vue({
el:'#app',
data:{
message:''
},
components:{
cpn:{
template:'#cpn'
}
}
})
</script>
</html>
具名插槽:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<cpn><p slot="top">biaoti</p></cpn>
</div>
<template id="cpn">
<div>
<!-- 没有名字的会被替换;如果要替换有名字的,需要在父组件里面也加上相同的名字 -->
<slot name='top'><p>top</p></slot>
<slot><p>center</p></slot>
<slot><p>bottom</p></slot>
</div>
</template>
</body>
<script src="vue.js"></script>
<script>
const app=new Vue({
el:'#app',
data:{
message:''
},
components:{
cpn:{
template:'#cpn'
}
}
})
</script>
</html>