vue-router

前言

一、Vue路由基础用法:

1 .安装
 npm install vue-router --save
2 .main.js中
//Vue路由:引入
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//Vue路由:引入并创建组件
import BYHome from './components/BYHome.vue'
import BYNews from './components/BYNews.vue'
import HelloWorld from './components/HelloWorld.vue'

//Vue路由:配置路由
const routes = [
  {path: '/home', component: BYHome},
  {path: '/news', component: BYNews},
  {path: '/helloworld', component: HelloWorld},
  {path: '*', redirect: '/home'} /*默认跳转路由 */
]

//Vue路由:实例化VueRouter 
const router = new VueRouter({
  routes //缩写,相当于 routes:routes
})

new Vue({
  router, //Vue路由:挂载路由
  render: h => h(App),
}).$mount('#app')

//Vue路由:根组件的模板里面放上下面这句话,需要在App.vue 中配置路由出口:路由匹配到的组件将渲染在根组件App.vue中
<router-view></router-view>

//路由跳转
<router-link to="/home">首页</router-link>  
<router-link to="/news">新闻</router-link>
<router-link to="/helloworld">helloWorld</router-link>

二、Vue路由配置的抽出

1.安装

  npm install vue-router --save

2.创建router.js文件,在该文件中配置路由并暴露出去

 import Vue from 'vue';
  //Vue路由:引入
    import VueRouter from 'vue-router'
    Vue.use(VueRouter)

    //Vue路由:引入并创建组件
    import BYHome from '../BYHome.vue'
    import BYNews from '../BYNews.vue'
    import HelloWorld from '../HelloWorld.vue'

    //Vue路由:配置路由
    const routes = [
      {path: '/home', component: BYHome},
      {path: '/news', component: BYNews},
      {path: '/helloworld', component: HelloWorld},
      {path: '*', redirect: '/home'} /*默认跳转路由 */
    ]

    //Vue路由:实例化VueRouter 
    const router = new VueRouter({
      routes //缩写,相当于 routes:routes
    })
    //Vue路由:需要在App.vue 中配置路由出口:路由匹配到的组件将渲染在根组件App.vue中
    /* <router-view></router-view> */


    //暴露出去
    export default router;

3.在main.js中

//Vue路由:引入路由文件
import router from ‘./components/jsTool/router.js’

new Vue({undefined
router, //Vue路由:挂载路由
render: h => h(App),
}).$mount(’#app’)

4.Vue路由

根组件的模板里面放上下面这句话,需要在App.vue 中配置路由出口:路由匹配到的组件将渲染在根组件App.vue中

   <router-view></router-view>

5.路由跳转

<router-link to="/home">首页</router-link>  
<router-link to="/news">新闻</router-link>
<router-link to="/helloworld">helloWorld</router-link>

三、路由动态传值:

1.获取路由的get传值

//路由配置

import BYHomeDetail from '../BYHomeDetail.vue'
{path: '/homeDetail', component:BYHomeDetail},
//跳转时跟get参数
<li li v-for="(listItem,homeKey) in msglist">
            <router-link :to="'/homeDetail?id='+homeKey"> {{listItem.title}} </router-link>
 </li>
 //子页面获取路由的get传值
  mounted(){
    console.log(this.$route.query);
}

2.动态路由传值

//路由配置:带形参
import BYNewDetail from '../BYNewDetail.vue'
{path: '/newDetail/:aid', component: BYNewDetail}, 
//跳转时传值
<li v-for="(item,key) in list">
    <!-- 给 newDetail 传值 -->
    <router-link :to="'/newDetail/'+key">{{key}}--{{item}}</router-link>
</li>
//子页面获取动态路由传值
 mounted(){
    console.log(this.$route.params);
}

四、路由的跳转方式:

第一种跳转方式:编程式导航

{path: '/news', component: BYNews},
this.$router.push({path:'news'});
带参:
{path: '/newDetail/:aid', component: BYNewDetail},
this.$router.push({path:'/newDetail/495'});

第二种跳转方式:命名路由

 {path: '/news', component: BYNews,name:'news'},
    this.$router.push({name:'news'});
    带参:
    this.$router.push({name:'news',params:{userId:123}});

五、路由的hash模式以及history模式:

默认是hash模式,路由上方的路径是用#表示,http://localhost:8080/#/news

可以将hash模式改为history模式,路由上方的路径就没有了

#,http://localhost:8080/news 
  如果有history模式,需要后台做一些配置
  //Vue路由:实例化VueRouter 
const router = new VueRouter({
  mode: 'history',   //若是默认的hash模式,则mode不需要写
  routes //缩写,相当于 routes:routes
})

此处查看路由守卫

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值