Vue中怎么实现点击切换页面

 2914

vue实现点击切换页面的方法:1、注册一个组件,并在父元素中使用;2、使用v-if和v-else来控制页面的显示与隐藏;3、给两个按钮绑定事件,并控制v-if值的更改;4、自定义事件,将父元素的值传给子元素,用来显示到页面上,从而更好显示页面的切换效果。


Vue中怎么实现点击切换页面


vue中怎么实现点击切换页面?

Vue案例--点击按钮切换页面

用vue的方式做一个切换页面的效果。


思路:

注册一个组件,并在父元素中使用。

使用v-if 和 v-else 来控制页面的显示与隐藏。

给两个按钮绑定事件(如果是同一个方法,使用传参来区别点击的哪个按钮;如果是不同的事件,就很好区分),控制v-if 值的更改。

自定义事件,将父元素的值传给子元素,用来显示到页面上,从而更好显示页面的切换效果。

父组件代码:

<template>
    <div>
        <div className="boxs">
            <Page v-if="isShow" :pa="info1" style="background-color: lightpink;width: 200px; height:200px;"></Page>
            <Page v-else id="soecnd" :pa="info2"  style="background-color: lightskyblue;width: 200px; height:200px;"></Page>
            <button @click="fn(1)" >切换至第二个页面</button>
            <button @click="fn(2)">切换至第一个页面</button>
        </div>
    </div>
</template>
<script>
import Page from "./components2/Page.vue";
export default {
    data() {
        return {
            isShow: true,
            info1:"第一个页面",
            info2:"第二个页面"
        }
    },
    components: {
        Page
    },
    methods: {
        fn(i) {
            if (i == 1) {
                this.isShow = false
            } else if (i == 2) {
                this.isShow = true
            }
            console.log(2222)
        }
    }
}
</script>
<style scoped>
/* #soecnd {
    width: 200px;
    height: 200px;
    background-color: cornflowerblue;
} */
</style>

子组件的代码:

<template>
    <div>
        <div class="pageBox">
            {{pa}}
        </div>
    </div>
</template>
  
<script>
export default {
    data(){
        return{
            msg:"11111"
        }
    },
    props:["pa"]
}
</script>
  
<!-- <style>
    /* 如果这里有样式,页面渲染的时候一直是这个样式,父组件引入子组件时的行内样式也改不了样式 */
    .pageBox {
        width: 200px;
        height: 200px;
        background-color: coral;
    }
</style> -->


TAG标签:
本文网址:https://www.zztuku.com/detail-13379.html
站长图库 - Vue中怎么实现点击切换页面
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐