“vue.js | 在动态组件上进行过渡”

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

vue.js | Transition on dynamic components

问题

Here is the translated content:

尝试在Vue.js v2.6.11中执行动态组件转换。我已参考文档 - 组件之间的过渡,但无法获得结果。

我已经在CodeSandbox中创建了一个示例,进行了不同的尝试和错误。但以下是应该足够实现过渡的基本代码段。

模板

<template>
  <div id="app">
    <button @click="toggle = !toggle">切换</button>
    <transition class="component-fade" mode="out-in">
      <component :is="currentComp" val="msg" :key="currentComp" />
    </transition>
    <transition class="component-fade" mode="out-in">
      <div v-if="toggle">测试</div>
      <p v-else>抱歉,未找到任何项目。</p>
    </transition>
  </div>
</template>

脚本

<script>
export default {
  name: "App",
  data() {
    return {
      toggle: true
    };
  },
  components: {
    home2: {
      template: `<h1>主页2</h1>`,
    },
    "about-us2": {
      template: `<h1>关于2</h1>`,
    },
  },
  computed: {
    currentComp() {
      return this.toggle ? "home2" : "about-us2";
    },
  },
};
</script>

样式

<style scoped>
.component-fade-enter-active,
.component-fade-leave-active {
  transition: all 6s ease;
}

.component-fade-enter,
.component-fade-leave-to {
  opacity: 0;
}
</style>

代码解释:
点击切换按钮后,组件将使用计算值切换,并且组件将使用<component>:is属性进行更改。因此,当它更改时,我正在尝试进行基本的过渡(目前是透明度更改)。

如果我的代码有遗漏,请告诉我。

英文:

Trying to perform transition on dynamic components in vue.js v2.6.11. I have referred to the documentation - Transitioning Between Components, but unable to get the results.

I have created a sample in codesandbox with different trial and errors. But below is the basic code snippet that should have been enough to achieve the transition.

template

&lt;template&gt;
  &lt;div id=&quot;app&quot;&gt;
    &lt;button @click=&quot;toggle = !toggle&quot;&gt;Toggle&lt;/button&gt;
    &lt;transition class=&quot;component-fade&quot; mode=&quot;out-in&quot;&gt;
      &lt;component :is=&quot;currentComp&quot; val=&quot;msg&quot; :key=&quot;currentComp&quot; /&gt;
    &lt;/transition&gt;
    &lt;transition class=&quot;component-fade&quot; mode=&quot;out-in&quot;&gt;
      &lt;div v-if=&quot;toggle&quot;&gt;test&lt;/div&gt;
      &lt;p v-else&gt;Sorry, no items found.&lt;/p&gt;
    &lt;/transition&gt;
  &lt;/div&gt;
&lt;/template&gt;

script

&lt;script&gt;
export default {
  name: &quot;App&quot;,
  data() {
    return {
      toggle: true
    };
  },
  components: {
    home2: {
      template: `&lt;h1&gt;home2&lt;/h1&gt;`,
    },
    &quot;about-us2&quot;: {
      template: `&lt;h1&gt;about2&lt;/h1&gt;`,
    },
  },
  computed: {
    currentComp() {
      return this.toggle ? &quot;home2&quot; : &quot;about-us2&quot;;
    },
  },
};
&lt;/script&gt;

Style

&lt;style scoped&gt;
.component-fade-enter-active,
.component-fade-leave-active {
  transition: all 6s ease;
}

.component-fade-enter,
.component-fade-leave-to {
  opacity: 0;
}
&lt;/style&gt;

Explanation of code:
On click of toggle button, the components will switch using the computed value and the component will change using the :is of &lt;component&gt;. So, when it changes then am trying to a basic transition (opacity change for now).

Let me know if I am missing anything in my code.

答案1

得分: 0

你没有提供“过渡名称”。

class="component-fade" 替换为:

<transition name="component-fade" mode="out-in">

这里是修复后的沙盒链接

英文:

You haven't provided the transition name.

Replace class=&quot;component-fade&quot;

&lt;transition class=&quot;component-fade&quot; mode=&quot;out-in&quot;&gt;

with

&lt;transition name=&quot;component-fade&quot; mode=&quot;out-in&quot;&gt;

Here is the fixed sandbox

huangapple
  • 本文由 发表于 2023年4月19日 16:15:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052174.html
匿名

发表评论

匿名网友

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

确定