Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
全局组件:提前声明,在任意子组件中均可直接使用该组件,不需要在进行引入声明。
局部组件:就是指只有引入的局部才可以使用这个组件,如果没有引入不可以使用。
<!DOCTYPE html>
<html lang="zh-CN">
<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>组件注册</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<!-- 全局组件 -->
<div class="allapp">
<button-counter></button-counter>
</div>
<template id="love">
<button @click="love++">(全局组件)喜欢: {{love}}</button>
</template>
<hr />
<!-- 局部组件 -->
<div class="app">
<button-counter></button-counter>
</div>
<template #="love">
<button @click="love++">(局部组件)喜欢: {{love}}</button>
</template>
<hr />
<!-- 子组件传参的通信方式 -->
<div class="tel">
<button-counter tel="13000000000" add="北京"></button-counter>
</div>
<template id="counter">
<p>手机: {{tel}}</p>
<p>地址: {{add}}</p>
</template>
<hr />
<!-- 父组件传参的通信方式 -->
<div class="alltel">
<button-counter @review-count="review"></button-counter>
</div>
<template id="alltel">
<button @click="love++">(父组件传参)喜欢: {{love}}</button>
<button @click="$emit('reviewCount', this.love)">评价</button>
</template>
<script>
const allapp = Vue.createApp();
// 1. 全局组件
allapp
.component("button-counter", {
template: "#love",
data() {
return {
love: 0,
};
},
})
.mount(".allapp");
// 2. 局部组件
const app = Vue.createApp({
components: {
buttonCounter: {
template: "#love",
data() {
return {
love: 0,
};
},
},
},
}).mount(".app");
// 子组件传参
const tel = Vue.createApp();
tel
.component("button-counter", {
props: ["tel", "add"],
template: "#counter",
})
.mount(".tel");
// 父组件传参
const alltel = Vue.createApp({
methods: {
review(love) {
console.log(love);
if (love >= 10) alert("大家吃了吗?");
},
},
});
alltel
.component("button-counter", {
template: "#alltel",
data() {
return {
love: 0,
};
},
})
.mount(".alltel");
</script>
</body>
</html>