先上Vue3组件的实例代码:
<!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>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="michael">
<michael></michael>
<sky></sky>
<blue></blue>
</div>
<script>
var app= Vue.createApp({
data(){
return {
"msg":"hello"
}
}
});
app.component('michael', {
'template':`
<div>michael</div>
`
});
app.component('sky', {
'template':`
<div>sky</div>
`
});
app.component('blue', {
'template':`
<div>blue</div>
`
});
app.mount("#michael");
</script>
</body>
</html>
注意:以上是Vue3的叠加的写法,不一定要写成链式的代码。
运行效果:
动态组件的写法:
<!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>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<style>
.abc{
width: 100px;
height: 100px;
border: 1px solid black;
background-color: green;
color:white;
}
</style>
</head>
<body>
<div id="michael">
<!--<michael></michael>
<sky></sky>
<blue></blue>-->
<button v-for='tab in btnMsg' :key="tab" @click="tabName=tab">
{{tab}}
</button>
<component :is="tabName" class="abc"></component><!--动态组件-->
</div>
<script>
var app= Vue.createApp({
data(){
return {
"msg":"hello",
"btnMsg":["michael","sky","blue"],
"tabName":"michael"
}
}
});
app.component('michael', {
'template':`
<div>michael</div>
`
});
app.component('sky', {
'template':`
<div>sky</div>
`
});
app.component('blue', {
'template':`
<div>blue</div>
`
});
app.mount("#michael");
</script>
</body>
</html>
运行效果如下:
[小结]
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。