javascript - VueJS,组件层次结构如何在层次结构中多次使用一个组件
问题描述
我需要在从根组件开始的层次结构中多次使用一个组件。
下面是根的代码Comp1
<template>
<alert/>
<modal/>
<button @click='showAlert()'></button>
</template>
<script>
import modal from './Modal'
export default{
components: {
modal
},
methods: {
showAlert(){
//code that shows the alert
}
}
}
</script>
下面是子组件的代码modal
<template>
<grandchild/>
</template>
<script>
import grandchild from './GrandChild'
export default{
components: {
grandchild
}
}
</script>
下面是代码grandchild
<template>
<alert/>
<button @click='showAlert()'></button>
</template>
<script>
export default{
methods: {
showAlert(){
//code that shows the alert
}
}
}
</script>
alert
是一个全局可用的模态组件,具有很大的z-index
价值,以确保它显示在其他组件之上。当调用showAlert()
in时,Comp1
正在显示的警报是组件中的警报modal
。为什么会这样?
解决方案
推荐阅读
- python - TypeError:“播放器”对象不可调用 Django
- swift - Catalyst datePicker 显示系统时间而不是 utc 时间(datePicker 时区已设置为 UTC)
- wso2 - WSO2 API Manager 3.0 会话未注销发布者
- python - 分组熊猫数据框后索引消失
- python-3.x - TypeError:列表索引必须是整数或切片,而不是它在 if 语句中显示的 str
- python - 在 Heroku 上部署 Django 应用程序所需的支持
- python - 熊猫:无法进行位置索引
- kotlin - 在 git bash 中编译我的 Kotlin 测试
- java - 通过 junit 运行时,jooq 字段值为空
- flutter - 顶部有字母和数字的键盘(在 Flutter 中)