vue.js - 属性或方法“isOpen”未在实例上定义,但在渲染期间被引用
问题描述
我对 vue.js 比较陌生。我正在尝试创建一个初始显示状态设置为 false 的模式对话框。此对话框用于另一个组件,如波浪所示。我无法弄清楚为什么data
是isOpen
undefined
// My main component here
<template>
<button @click="openMyModal">Open</button>
<MyDialog ref="dialog"/>
</template>
<script>
...
methods: {
openMyModal(){
this.$refs.dialog.open().then((confirm) => {
console.log("confirm", confirm)
return true
}).catch();
}
}
...
</script>
<template>
<div class="overlay" v-if="isOpen">
<div class="modal">
<h1>My modal dialog here</h1>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'my-dialog'
}
data () {
return {
isOpen: false
...
}
}
methods() {
open() {
this.isOpen = true;
...
},
close() {
this.isOpen = false;
},
}
</script>
解决方案
这主要是因为语法错误。这是调试代码后的示例:
在父级中:
methods: {
openMyModal() {
this.$refs.dialog.open();
}
}
在孩子身上:
export default {
name: "my-dialog",
data() {
return {
isOpen: false
};
},
methods: {
open() {
this.isOpen = true;
},
close() {
this.isOpen = false;
}
}
};
推荐阅读
- java - org.springframework.dao.EmptyResultDataAccessException:不存在 id x 的类 com.jea.user.User 实体
- python - 如何使用 RSA 算法加密文本
- c# - Azure 设置内部 url 的可用性测试
- c - 如何?- 彩票模拟(C)
- java - 如何限制 Eclipse Code Minings 搜索以避免 JRE 和依赖项
- java - JAVA:为什么 Eclipse 将我的第二个 else-if 语句称为“死代码”?
- java - 我们可以传递子类的实例而不是超类作为方法/构造函数参数吗?
- qt - Qt - 如何从包含在特定模式中的 QString 中提取文本片段
- javascript - 创建逗号分隔的对象列表的最短方法
- html - 解析 HTML getElementsByTagName 不返回所有单元格