【发布时间】:2020-11-10 13:24:23
【问题描述】:
div中有两个components,当两个components一起渲染时,我点击button可以正常切换,但是在只渲染一个组件的情况下,切换变得异常。
这是我的代码
Base.vue
<template>
<div :id="id">{{msg}}</div>
</template>
<script lang='ts'>
import { Component, Prop } from "vue-property-decorator";
import Vue from "vue";
@Component
export default class Base extends Vue {
id!: string;
msg = "this is Base";
}
</script>
child.vue(无模板)
<script lang='ts'>
import Base from "@/components/Base.vue";
import { Prop, Component } from "vue-property-decorator";
@Component
export default class extends Base {
@Prop({ default: "helloWorld" })
childId!: string;
constructor() {
super();
this.id = this.childId;
this.msg = "this is Child " + this.childId;
}
}
</script>
App.vue(显示这些组件)
<template>
<div id="app">
<Child v-show="!show" childId="child1" style="color:#f00;"/>
<button @click="click">change</button>
<Child v-show="show" childId="child2" style="color:#f0f;"/>
</div>
</template>
<script lang="ts">
import Vue from "vue";
import Child from "@/components/Child.vue";
import Component from "vue-class-component";
@Component({
components:{
Child,
}
})
export default class App extends Vue {
show= false;
click() {
this.show = !this.show;
}
}
</script>
然后点击button结果是
这些结果是预期的。但如果应用程序中的所有v-show。上面的vue改成v-if,结果比较混乱
然后点击button结果是
在我们的预期中,它应该在此处显示 child2。那么为什么会这样呢?
【问题讨论】:
标签: javascript typescript vue.js