当前位置:   article > 正文

VueRouter的基本使用_vue中router文件的

vue中router文件的

router文件夹中index.js文件

  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. // import Home from '../components/Home';
  4. // import About from '../components/About';
  5. // import HelloWorld from '../components/HelloWorld';
  6. // import User from '../components/User'
  7. // 路由懒加载导入
  8. const Home = ()=>import('../components/Home');
  9. const About = ()=>import('../components/About');
  10. const User = ()=>import('../components/User');
  11. // const HelloWorld = ()=>import('../components/HelloWorld');
  12. const HomeMessage = ()=>import('../components/HomeMessage');
  13. const HomeProfile = ()=>import('../components/HomeProfile');
  14. const Profile = ()=>import('../components/Profile')
  15. // 1.使用Vue.use(插件),安装插件
  16. Vue.use(VueRouter)
  17. // 2.创建VueRouter对象
  18. const routes=[
  19. {
  20. path:'/',
  21. // redirect重定向
  22. redirect:'/home'
  23. },
  24. {
  25. path:'/home',
  26. component:Home,
  27. redirect:'/message',
  28. meta:{
  29. title:"首页"
  30. },
  31. children:[
  32. {
  33. path:'/message',
  34. component:HomeMessage
  35. },
  36. {
  37. path:'/profile',
  38. component:HomeProfile
  39. }
  40. ]
  41. },
  42. {
  43. path:'/about',
  44. component:About,
  45. meta:{
  46. title:"关于"
  47. },
  48. },
  49. {
  50. path:'/user/:userId',
  51. component:User,
  52. meta:{
  53. title:"用户"
  54. },
  55. },
  56. {
  57. path:'/profile',
  58. component:Profile,
  59. meta:{
  60. title:"简介"
  61. },
  62. }
  63. ];
  64. const router=new VueRouter({
  65. // 这个自定义的名字,是可以的,但是这里由于是使用了es6 的语法糖的形式,将前面相同给省略了, 但是如果省略的话,就必须要求是一致的 routes:routes (没有引号)
  66. // routes:routers,
  67. routes,
  68. mode:'history',
  69. linkActiveClass:'active'
  70. });
  71. // to:即将跳转进入的路由对象;from:当前导航即将离开的路由对象;next:调用该方法后,才能进入下一个钩子
  72. // 前置守卫/回调(guard)
  73. router.beforeEach((to,from,next)=>{
  74. // 从from跳转到to
  75. document.title=to.matched[0].meta.title;
  76. console.log(to)
  77. next()
  78. })
  79. // 3.导出Router
  80. export default router

app.vue文件

  1. <template>
  2. <div id="app">
  3. <div>
  4. <!-- <router-link to="/home" tag="button" replace active-class="active">首页</router-link>
  5. <router-link to="/about" replace active-class="active">关于</router-link> -->
  6. <!-- <router-link to="/home" tag="button" replace>首页</router-link>
  7. <router-link to="/about" replace>关于</router-link> -->
  8. <!-- <button @click="homeBtn">首页</button>
  9. <button @click="aboutBtn">关于</button> -->
  10. <router-link to="/home">首页</router-link>
  11. <router-link to="/about">关于</router-link>
  12. <!-- <router-link :to="'/user/'+userId">用户</router-link> -->
  13. <!-- <router-link :to="{path:'/profile',query:{name:'Bob',age:19,height:188}}">简介</router-link> -->
  14. <button @click="userBtn">用户</button>
  15. <button @click="profileBtn">简介</button>
  16. </div>
  17. <keep-alive exclude="About,User">
  18. <router-view></router-view>
  19. </keep-alive>
  20. </div>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'App',
  25. data(){return {userId:'lisi'}},
  26. methods:{
  27. homeBtn(){
  28. this.$router.push('./home')
  29. console.log("homeBtn")
  30. },
  31. aboutBtn:function(){
  32. this.$router.push('./about')
  33. console.log("aboutBtn")
  34. },
  35. userBtn(){
  36. this.$router.push('/user/'+this.userId)
  37. },
  38. profileBtn(){
  39. this.$router.push({
  40. path:'/profile',
  41. query:{
  42. name:'Bob',
  43. age:19,
  44. height:188
  45. }
  46. })
  47. }
  48. }
  49. }
  50. </script>
  51. <style>
  52. .active{color: red;}
  53. </style>

Profile.vue文件

  1. <template>
  2. <div>
  3. <h2>我是Profile组件</h2>
  4. <p>我是Profile;哈哈哈哈哈</p>
  5. <h3>{{$route.query.name}}</h3>
  6. <h3>{{$route.query.age}}</h3>
  7. <h3>{{$route.query.height}}</h3>
  8. </div>
  9. </template>
  10. <script>
  11. export default{
  12. name:'Profile',
  13. created(){
  14. console.log('ProfileCreated')
  15. },
  16. destroyed(){
  17. console.log('ProfileeDestroyed')
  18. }
  19. }
  20. </script>
  21. <style>
  22. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/616504
推荐阅读
相关标签
  

闽ICP备14008679号