`v-if`指令在提供/注入逻辑中不起作用。

huangapple go评论70阅读模式
英文:

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>

huangapple
  • 本文由 发表于 2023年2月24日 14:30:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定