37_Vue_编辑式导航前进和后退的跳转

实现前进和后退,使用熟悉的原生js,即$router.go(n)/$router.back()/$router.forward()
一、AMain.vue页面
1、不使用<router-link></router-link>路由跳转,通过方法传入name实现跳转,即@click="goto(nav.name)",传入name:

<template>
    <div class="home">
        <el-menu :default-active="activeIndex+''" class="el-menu-demo" mode="horizontal" @select="handleSelect">
            <el-menu-item :index="idx+''" v-for="(nav,idx) in navs" :key="nav.name" @click="goto(nav.name)">
                <!-- <router-link :to="nav.path" replace tag="span">{{nav.text}}</router-link> -->
                {{nav.text}}
            </el-menu-item>
        </el-menu>
         <router-view></router-view>
    </div>
</template>
<script>
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI)
export default{
    data(){
         return{
            navs:[
                {
                    text:'首页',
                    name:'Home',
                },
                {
                    text:'列表',
                    name:'List',
                },
                {
                    text:'详情',
                    name:'Goods',
                },
                {
                    text:'购物车',
                    name:'Cart',
                }
            ],
            activeIndex:0
        }
    },
    methods:{
        handleSelect(index,path){
            console.log(index,path);
            this.activeIndex=index
        },
        //方法如下
        goto(name){
            console.log('App',this)
            //获取路由
            this.$router.push({name})
        }
    },
    created(){
        this.navs.forEach(item=>{
            item.path='/'+item.name.toLowerCase()
        });
    }
}
</script>

二、AMain.vue页面
1、不使用<router-link></router-link>路由跳转,通过方法传入path实现跳转,即@click="goto(nav.path)",传入path:

<template>
    <div class="home">
        <el-menu :default-active="activeIndex+''" class="el-menu-demo" mode="horizontal" @select="handleSelect">
           <!--传入path,@click="goto(nav)"-->
            <el-menu-item :index="idx+''" v-for="(nav,idx) in navs" :key="nav.name" @click="goto(nav)">
                <!-- <router-link :to="nav.path" active-class="active" replace tag="span">{{nav.text}}</router-link> -->
                {{nav.text}}
            </el-menu-item>
        </el-menu>
         <router-view></router-view>
    </div>
</template>
<script>
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI)
export default{
    data(){
        return{
            navs:[
                {
                    text:'首页',
                    name:'Home',
                }, 
               { 
                    text:'列表',
                    name:'List',
                },
                { 
                    text:'详情',
                    name:'Goods',
                },
                {
                    text:'购物车',
                    name:'Cart',
                }
            ],
            activeIndex:0
        }
    },
    methods:{
        handleSelect(index,path){
            console.log(index,path);
            this.activeIndex=index
        },
        //传入path
        goto(nav){
            console.log('App',this)
            //获取路由
            //this.$router.push({path:nav.path})
        }
    },
    created(){
        this.navs.forEach(item=>{
            item.path='/'+item.name.toLowerCase()
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值