
批改狀態(tài):合格
老師批語:
<div id="app">
<!-- vue指令的本質是什么? 自定義屬性 -->
<!-- <p v-text="'Hello World'"></p> -->
<!-- vue組件的本質是什么? 自定義標簽 -->
<!-- <hello-world></hello-world> -->
<button-counter></button-counter>
<button-counter></button-counter>
</div>
<template id="counter">
<button @click="count++">點贊: {{count}}</button>
</template>
<script>
// const app = Vue.createApp();
// 注冊組件有二種方式
// 1. 全局組件: 注冊在vue實例上
// app.component('button-counter', {
// template: '#counter',
// data() {
// return {
// count: 0,
// };
// },
// });
// 2. 局部組件: 注冊vue實例的選項中
const app = Vue.createApp({
components: {
// 可以使用規(guī)范的駝峰命名法, 不過在html中要轉為蛇形
buttonCounter: {
template: '#counter',
data() {
return {
count: 0,
};
},
},
},
});
app.mount('#app');
</script>
<div id="app">
<!-- 使用自定義屬性將父組件參數傳入子組件中 -->
<button-counter username="admin" email="498446472@qq.com"></
button-counter>
</div>
<template id="counter">
<p>用戶名: {{username}}</p>
<p>郵箱: {{email}}</p>
</template>
<script>
const app = Vue.createApp();
app.component('button-counter', {
// props: 用數組接收父組件傳入的自定義屬性做為載體的參數
props: ['username', 'email'],
template: '#counter',
});
app.mount('#app');
</script>
<body>
<div id="app">
<button-counter @review-count="review"></button-counter>
</div>
<template id="counter">
<button @click="count++">點贊: {{count}}</button>
<!-- 發(fā)布訂閱 -->
<!-- $emit(自定義事件名稱, 向父組件傳遞的參數(可選)) -->
<!-- $emit('reviewCount', this.count) -->
<button @click="$emit('reviewCount', this.count)">評價</
button>
</template>
<script>
const app = Vue.createApp({
methods: {
review(count) {
console.log(count);
if (count >= 10) alert('大家吃了嗎?');
},
},
});
app.component('button-counter', {
template: '#counter',
data() {
return {
count: 0,
};
},
});
app.mount('#app');
</script>
</body>
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號