使用$router

本篇将了解$router的5个方法,分别是,

  1. push
  2. replace
  3. back
  4. forward
  5. go

熟悉js的同学可能会觉得上述方法有些眼熟,确实,因为window.history对象上也有类似方法,如back、forward、go。

具体看例子,项目目录如下。
在这里插入图片描述

  • 一般组件component/Banner.vue
<template>
    <div class="col-xs-offset-2 col-xs-8">
        <div class="page-header">
            <h2>Vue Router Demo</h2>
            <button @click="back">后退</button>
            <button @click="forward">前进</button>
        </div>
    </div>
</template>

<script>
export default {
    name:"Banner",
    methods:{
        back(){
            this.$router.back();
        },
        forward(){
            this.$router.forward();
        }
    }
}
</script>

<style>

</style>

在这里插入图片描述

  • 路由组件pages/Home.vue
<template>
  <div>
    <h2>Home组件内容</h2>
    <div>
      <ul class="nav nav-tabs">
        <li>
          <router-link class="list-group-item" active-class="active" to="/home/news">News</router-link>
        </li>
        <li>
          <router-link class="list-group-item" active-class="active" to="/home/message">Message</router-link>
        </li>
      </ul>
      <div>
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
export default {
    name:'Home'
}
</script>

<style>

</style>
  • 路由组件pages/About.vue
<template>
  <h2>我是About的内容</h2>
</template>

<script>
export default {
    name:'About'
}
</script>

<style>

</style>
  • 路由组件pages/News.vue
<template>
    <ul>
        <li>news001</li>
        <li>news002</li>
        <li>news003</li>
    </ul>
</template>

<script>
export default {
    name:"News"
}
</script>

<style>

</style>
  • 路由组件pages/Message.vue
<template>
   <div>
       <ul>
           <li v-for="message in messageList" :key="message.id">
               <span>{{message.title}}</span>
               <button @click="pushShow(message)">push查看</button>
               <button @click="replaceShow(message)">replace查看</button>
           </li>
       </ul>
       <hr>
       <router-view></router-view>
   </div>
</template>

<script>
export default {
    name:"Message",
    data(){
        return {
            messageList:[
                {id:"001",title:"消息01"},
                {id:"002",title:"消息02"},
                {id:"003",title:"消息03"},
            ]
        }
    },
    methods:{
        pushShow(message){
            this.$router.push({
                path:"/home/message/detail",
                query:{
                    id:message.id,
                    title:message.title
                }
            })
        },
        replaceShow(message){
            this.$router.replace({
                path:"/home/message/detail",
                query:{
                    id:message.id,
                    title:message.title
                }
            })
        }
    }
}
</script>

<style>

</style>

在这里插入图片描述

  • 路由组件pages/Detail.vue
<template>
    <ul>
        <li>消息编号:{{id}}</li>
        <li>消息标题:{{title}}</li>
    </ul>
</template>

<script>
export default {
    name:"Detail",
    props:['id','title']
}
</script>

<style>

</style>
  • 路由器router/index.js
import VueRouter from "vue-router";
import Home from "../pages/Home";
import About from "../pages/About";
import News from "../pages/News";
import Message from "../pages/Message";
import Detail from "../pages/Detail";

export default new VueRouter({
    routes:[
        {
            path:"/about",
            component:About
        },
        {
            path:"/home",
            component:Home,
            children:[
                {
                    path:"message",
                    component:Message,
                    children:[
                        {
                            path:"detail",
                            component:Detail,
                            props({query:{id,title}}){
                                return {
                                    id,
                                    title
                                }
                            }
                        }
                    ]
                },
                {
                    path:"news",
                    component:News
                }
            ]
        }
    ]
})
  • App.vue
<template>
  <div>
    <div class="row">
      <Banner/>
    </div>
    <div class="row">
      <div class="col-xs-2 col-xs-offset-2">
        <div class="list-group">
           <router-link to="/about" active-class="active" class="list-group-item">About</router-link>
          <router-link to="/home" active-class="active" class="list-group-item">Home</router-link>
        </div>
      </div>
      <div class="col-xs-6">
        <div class="panel">
          <div class="panel-body">
            <router-view></router-view>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Banner from "./components/Banner.vue";

export default {
  name: 'App',
  components:{
    Banner
  }
}
</script>
  • 入口文件main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from "vue-router";

import router from "./router/index";

Vue.config.productionTip = false;

Vue.use(VueRouter);

new Vue({
  render: h => h(App),
  router:router
}).$mount('#app')
  • 启动应用,测试效果。
    在这里插入图片描述
    在这里插入图片描述
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值