0
点赞
收藏
分享

微信扫一扫

vue路由,路由传参(parmas,query)

佃成成成成 2022-02-07 阅读 136

一、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中

//路由跳转
首页
新闻
helloWorld

二、Vue路由配置的抽出

  1. 安装
    npm install vue-router --save
    1

  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中
    /* */

    //暴露出去
    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’)

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

    1
  2. 路由跳转
    首页
    新闻
    helloWorld

三、路由动态传值:

  1. 获取路由的get传值
    //路由配置
    import BYHomeDetail from ‘…/BYHomeDetail.vue’
    {path: ‘/homeDetail’, component:BYHomeDetail},
    //跳转时跟get参数
  • {{listItem.title}}
  • //子页面获取路由的get传值 mounted(){ console.log(this.$route.query); }
    1. 动态路由传值
      //路由配置:带形参
      import BYNewDetail from ‘…/BYNewDetail.vue’
      {path: ‘/newDetail/:aid’, component: BYNewDetail},
      //跳转时传值
  • {{key}}--{{item}}
  • //子页面获取动态路由传值 mounted(){ console.log(this.$route.params); }

    四 、路由的跳转方式:

    1. 编程式导航
      {path: ‘/news’, component: BYNews},
      this. r o u t e r . p u s h ( p a t h : ′ n e w s ′ ) ; 带 参 : p a t h : ′ / n e w D e t a i l / : a i d ′ , c o m p o n e n t : B Y N e w D e t a i l , t h i s . router.push({path:'news'}); 带参: {path: '/newDetail/:aid', component: BYNewDetail}, this. router.push(path:news);path:/newDetail/:aid,component:BYNewDetail,this.router.push({path:’/newDetail/495’});

    2. 命名路由
      {path: ‘/news’, component: BYNews,name:‘news’},
      this. r o u t e r . p u s h ( n a m e : ′ n e w s ′ ) ; 带 参 : t h i s . router.push({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
    })

    六、路由的嵌套
    User.vue页面中有两个子页面
    UserAdd.vue
    UserList.vue

    //路由的配置

    <script>
    import BYUser from '../BYUser.vue'
      import UserAdd from '../User/UserAdd.vue'
      import UserList from '../User/UserList.vue'
    
    {path: '/user' , component:BYUser,
        children:[
          {path: 'useradd',component:UserAdd},
          {path: 'userlist',component:UserList}
        ]
    },
    </script>
    

    //路由的跳转

    • 增加用户
    • 用户列表

    七、登录及首页的路由配置说明

    1. 创建一个localstorage本地存储类storage.js,用来记录登录状态
      var storage={
      set(key,value){
      console.log(“storage---->”)
      console.log(value)
      localStorage.setItem(key, JSON.stringify(value));
      },
      get(key){
      return JSON.parse(localStorage.getItem(key));
      },remove(key){
      localStorage.removeItem(key);
      }
      }
      export default storage;

    2. 创建一个自定义的路由表:router.js
      import Vue from ‘vue’;
      //引入路由
      import VueRouter from ‘vue-router’
      Vue.use(VueRouter)

    //设置路由表
    const routes = [
     {
       path: '/',
       redirect: '/home'
     },
     {path: '/404', component: resolve => require(['../common/404.vue'],resolve)},
     {path: '/login', component: resolve => require(['../page/Login.vue'],resolve)},
     {path: '/home',component: resolve => require(['../page/Home.vue'],resolve),meta:{requireAuth:true}},//加上meta 表示需要登录才可进入
     {
       path:'*',
       redirect:'/404'
     }
    ]
    

    //实例化路由并指定模式
    const router = new VueRouter({
    /*默认是hash模式,路由上方的路径是用#表示,http://localhost:8080/#/news
    可以将hash模式改为history模式,路由上方的路径就没有了#,http://localhost:8080/news
    如果有history模式,需要后台做一些配置
    */
    // mode:‘history’,
    routes //缩写,相当于 routes:routes
    })
    //暴露出去,供外部调用
    export default router;

    3.在main.js中配置钩子路由:
    //引入自定义的路由表
    import router from ‘./components/router/router.js’
    //引入localstorage本地存储管理
    import storage from “./components/Tools/storage.js”;
    /**在路由跳转之前执行

    • to: 即将进入的路由对象
    • from: 当前导航即将离开的路由
    • next:Function,进行管道中的一个钩子,如果执行完了,则导航的状态就是 confirmed (确认的);否则为false,终止导航。
    • */
      router.beforeEach((to, from, next) => {
      //需要登录,但未登录者可以跳转到登录页面
      const isLogin = storage.get(‘login’);
      if(!isLogin && to.meta.requireAuth){//未登录 且 需要登录(提前在路由表中加上meta)
      next({
      path:’/login’
      })
      }else{//其他
      next();
      }
      });

    4.登录及退出登录
    // 登录:
    console.log(“登录成功”);
    let flag = true;
    storage.set(‘login’,flag);//存储登录状态
    this. r o u t e r . p u s h ( ′ / ′ ) ; / / 按 路 由 规 则 跳 转 / / 退 出 登 录 : c o n s o l e . l o g ( " 退 出 登 录 " ) ; l e t f l a g = f a l s e ; s t o r a g e . s e t ( ′ l o g i n ′ , f l a g ) ; / / 存 储 登 录 状 态 t h i s . router.push('/');//按路由规则跳转 // 退出登录: console.log("退出登录"); let flag = false; storage.set('login',flag);//存储登录状态 this. router.push(/);////退console.log("退");letflag=false;storage.set(login,flag);//this.router.push(’/login’);//按路由规则跳转

    理论:
    路由传参:
    三种:
    分别是query,params,动态路由传参
    接收:
    通过query方式传递过来的参数一般是通过this. r o u t e . q u e r y 接 收 通 过 p a r a m s 方 式 传 递 过 来 的 参 数 一 般 是 通 过 t h i s . route.query接收 通过params方式传递过来的参数一般是通过this. route.queryparamsthis.route.params接收
    通过动态路由传参方式传递过来的参数一般是通过this.$route.params接收

举报

相关推荐

0 条评论