【问题标题】:How can I transition components pages in vuejs?如何在 vuejs 中转换组件页面?
【发布时间】:2021-02-12 10:25:15
【问题描述】:

我想将组件更改为徽标图像和表单,当我单击徽标时,它会转换为表单。 阅读文件很简单,但我做不到。不知道是不是少了点什么。

索引.vue:

    <template>
      <v-app>
        <transition name="fade" mode="out-in">
        <Logo @click="show = !show" key="logo" />
        <Form key="form" v-if="show"  />
        </transition>
      </v-app>
    </template>
    
    <script>
    export default {
      components:{
        Logo: () => import('./components/Logo-Image'),
        Form: () => import('./components/Image-Form'),
      }
    }
    </script>

<style>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>

徽标.vue:

<template>
    <v-container fill-height fluid>
        <v-row align="center"
            justify="center">
            <v-col
            align="center"
            justify="center">
                <a><v-img src="/img/logo.png" max-height="360" max-width="570"></v-img></a>
            </v-col>
        </v-row>
    </v-container>
</template>

Form.vue

       <template>
 <v-container fill-height fluid>
            <v-row align="center"
                justify="center">
                <v-col
                align="center"
                justify="center">
                    <v-card
                        max-width="300"
                    >
                        <v-img
                        src="/img/logo.png"
                        max-height="360"
                        max-width="520"
                        >
                        </v-img>
                        <v-card-text>
                            <v-text-field
                                label="CPF"
                                
                            >
                            </v-text-field>
                        </v-card-text>
                    </v-card>
                </v-col>
            </v-row>
        </v-container>
    </template>

【问题讨论】:

  • 你能展示你的 CSS 吗?如果您还没有创建一些过渡类,则需要。参见示例 CSS:vuejs.org/v2/guide/…
  • 我用 CSS 更新了

标签: vue.js components css-transitions


【解决方案1】:

您必须在徽标中添加数据和 v-if

 <template>
  <v-app>
    <transition name="fade" mode="out-in">
    <Logo @click="show = !show" v-if="!show" key="logo" />
    <Form key="form" v-if="show"  />
    </transition>
  </v-app>
</template>

<script>
export default {
  data:{
    return(){
       show: false
    }
  },
  components:{
    Logo: () => import('./components/Logo-Image'),
    Form: () => import('./components/Image-Form'),
  }
}
</script>

【讨论】:

  • 感谢回复,正如我所说@wSkc 不起作用,我测试了两个答案
【解决方案2】:

在您的 Index.vue 中没有 data (),您需要保存 show 的值:

<script>
export default {
  components: {
    Logo: () => import('./components/Logo-Image'),
    Form: () => import('./components/Image-Form')
  },
  data () {
    return {
      show: true
    }
  }
}
</script>

然后,如果您希望表单在点击时替换徽标,则必须像这样切换它们:

<transition name="fade">
  <Logo v-if="show" @click="show = !show" key="logo" />
  <Form v-else key="form" />
</transition>

示例:

new Vue({
  el: '#app',
  data() {
    return {
      show: true
    }
  }
})
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <transition name="fade">
    <div v-if="show" @click="show = !show" key="logo">Logo</div>
    <div v-else key="form">Form</div>
  </transition>
</div>

【讨论】:

  • 感谢回复,但它不起作用,所有控制台错误都消失了,但是当点击徽标时,过渡不起作用
  • @TMoraes 我已经用一个例子更新了我的答案。一定要在data()中声明show
  • 还是不行,可能是组件导入的问题?
  • 删除这些组件并将其放在一个页面中,工作!为什么不使用组件?
猜你喜欢
  • 2018-12-24
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 2020-04-17
  • 2020-09-06
  • 2021-04-20
  • 2020-01-13
  • 2018-01-10
相关资源
最近更新 更多