英文:
v-if directive not working at hte provide/inject logic vue
问题
似乎 `v-if` 指令在提供/注入逻辑中不起作用。我试图保持这个项目结构,但是所有组件同时显示,即使我使用 `v-if` 来显示一个单独的组件。我想组件没有正确地从 App.vue 中读取提供的数据,或者我做错了什么?
英文:
It seems like the v-if
directive is not working at the provide/inject logic. I am trying to keep this project structure, but bot components are displayed at the same time, even though I'm using v-if
for displaying a singular one. I suppose the component isn't reading my provided data from App.vue correctly, or what am I doing wrong?
https://codesandbox.io/s/boring-einstein-bhyfeu?file=/src/components/Step1.vue
<template v-if="activePhase == 1"> //this line is not working for each component
<div class="container">
<div class="content-wrapper">
<h1>Step 1</h1>
<div class="form-group">
<label>First Name</label
><input
name="name"
v-model="firstName"
placeholder="Your first name"
class="form-control"
required
/>
</div>
<div class="form-group">
<label>Last Name</label
><input
name="lastname"
v-model="lastName"
placeholder="Your last name"
class="form-control"
required
/>
</div>
<button type="button" @click="nextStep" class="btn">Next step</button>
</div>
<Button />
</div>
</template>
<script>
import Button from "./Button.vue";
export default {
components: {
Button,
},
inject: ["activePhase", "firstName", "lastName"],
data() {
return {
activePhase: this.activePhase,
firstName: this.firstName,
lastName: this.lastName,
};
},
methods: {
nextStep() {
this.$emit("next");
console.log(this.activePhase);
},
},
};
</script>
<style scoped>
.form-group {
display: block;
}
</style>
答案1
得分: 0
是的,你不能在单文件组件(SFC)中的外部 template
标签上使用 v-if
,因为它是SFC结构的一部分,而不是实际的模板。
尝试使用:
<template>
<div class="container" v-if="activePhase == 1">
...
</div>
</template>
英文:
Yes, you cannot use v-if
on the outer template
tag in an SFC, as it is part of the SFC's structure and not the actual template.
Try
<template>
<div class="container" v-if="activePhase == 1">
...
</div>
</template>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论