Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

组件传值和监听事件 #2751

Open
xtzero opened this issue Jul 25, 2024 · 0 comments
Open

组件传值和监听事件 #2751

xtzero opened this issue Jul 25, 2024 · 0 comments

Comments

@xtzero
Copy link

xtzero commented Jul 25, 2024

请按照以下的模版填充相应的内容,一个 PR 会自动生成并保持与本 Issue 的内容同步。

你不需要提供详细的答案或教学,但请保证题目可解。

基本信息

# 题目难度
difficulty: medium

# 题目标题
title: 组件间传值

# 题目标签
tags: Components

题目

修改子组件中的 handleClick 方法,每次点击都让父元素中的 count 加一。

题目模版

filename: App.vue

<script setup>
import { ref } from 'vue'
import Child from './Child.vue'

const count = ref(0)

const handleChildAdd = () => {
  count.value ++
}
</script>

<template>
  <div class="app-container">
    <div>修改子组件中的 `handleClick` 方法,每次点击都让父元素中的 `count` 加一。</div>
    <Child id="child" :btnText="`你已经点击了${count}`" @add="handleChildAdd"></Child>
  </div>
</template>

<style scoped>

</style>

filename: Child.vue

<script setup>
const props = defineProps({
    btnText: {
        type: String,
        default: '按钮'
    }
})
const handleClick = () => {
    // 实现这个方法,变更父元素的 `count` 值
}
</script>

<template>
    <div class="child-container">
        <button @click="handleClick">{{ props.btnText }}</button>
    </div>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant