vue.js - vue在一个组件中打开一个组件
问题描述
在 App.vue 我有一个按钮来打开一个组件。在组件 BigForm 中,我还有一个按钮可以打开一个名为 的组件。但是当我打开组件时。Vue 重新渲染带有警告的页面:
The "data" option should be a function that returns a per-instance value in component definitions.
应用程序.Vue
<template>
<div id="app">
<button @click="showModal()">Show</button>
<BigForm v-if="modal" />
</div>
</template>
<script>
import BigForm from "./components/BigForm";
export default {
name: "App",
components: {
BigForm,
},
data() {
return {
modal: false,
};
},
methods: {
showModal() {
this.modal = !this.modal;
},
},
};
</script>
BigForm.vue:
<template>
<div class="hello">
<form style="height: 300px; width: 300px; background-color: green">
<button @click="showSmallForm()">Show small form</button>
<SmallForm
v-if="toggleSmallForm"
style="width: 100px; height: 100px; background-color: yellow"
/>
</form>
</div>
</template>
<script>
import SmallForm from "./SmallForm";
export default {
name: "HelloWorld",
components: {
SmallForm,
},
data() {
return {
toggleSmallForm: false,
};
},
methods: {
showSmallForm() {
this.toggleSmallForm = !this.toggleSmallForm;
},
},
};
</script>
和 SmallForm.vue:
<template>
<form>
<input placeholder="Small form input" />
<button>This is small form</button>
</form>
</template>
这是我在代码框中的示例的代码:
解决方案
推荐阅读
- android - 使用 RecycleView 加载下一页帖子
- javascript - reduce 函数中的累加器为空或没有数组数据
- ionic5 - 离子Macincloud
- python - 硒上的 element.click() 不起作用(菜鸟需要帮助)
- sql - 执行接受 2 个输入 arg 并返回 2 个输出的 oracle 存储过程
- r - 如何计算R中数据框的jaccard相似度?
- python - 在同一列但不同的行中解析来自 Excel 的数据?
- asp.net-core - 在 nopCommerce 中实现通用事件侦听器
- python - 尽管安装成功,但无法导入 torchvision
- javascript - Discord.js 机器人无法向特定频道发送消息