老莫的笔记本  
  
查看: 1106|回复: 1

VUE js跳转路由【程序化导航】、路由别名、组件嵌套路由

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2018-11-29 23:23:55 | 显示全部楼层 |阅读模式
一个个来, 1.先说 路由别名吧:
路由别名 就是 给一个路由加一个名字【我觉得挺无聊的 不过 如果放在嵌套路由内的话到还是不错的用法】


2.再来说说 组件嵌套路由吧:
嵌套路由,和angular 中一样, 给 他的路由项目配置children 属性,然后在组件的页面中的 加入<router-view></router-view> ; 接着 跳去吧

3.再来掰扯一下 程序化导航 ,这是个啥呢—— 这就是个 JS跳转 ,鬼知道为啥起这么高大尚的名字-、-
  1.   methods: {
  2.     jsrun() {
  3.       // this.$router.push('home')
  4.       this.$router.push({ path: "anli" });
  5.     },
  6.   }
复制代码

贴代码【记得 大前提  就是安装路由模块】
先贴最主要的 main.js
  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. // 3.1
  4. import Home from './components/Home'
  5. import News from './components/News'
  6. import Anli from './components/Anli'
  7. import Info from './components/Info'
  8. import One from './components/One'
  9. import Two from './components/Two'
  10. import Six from './components/Six'
  11. import Three from './components/Three'
  12. import VueRouter from 'vue-router'
  13. Vue.use(VueRouter);
  14. // 3.2
  15. const routes = [
  16.   { path: '/home', component: Home },
  17.   { path: '/news', component: News },
  18.   {
  19.     path: '/anli', component: Anli, children: [   //利用 children 来规定子路由 注意 子路由无/
  20.       { path: 'one', component: One }
  21.       , { path: 'two', component: Two }
  22.       , { path: 'six', name: 'lmsix', component: Six }  // 别名路由
  23.       , { path: 'three', redirect: '/news' , component: Three} //重定向 定到新闻页
  24.     ]
  25.   },
  26.   { path: '/info/:id/:name', component: Info }, //多传参
  27.   { path: '*', component: Home }, //默认首页
  28. ]
  29. const router = new VueRouter({
  30.   // routes // short for `routes: routes`
  31.   mode: 'history',
  32.   routes
  33. })
  34. Vue.config.productionTip = false
  35. new Vue({
  36.   // 4
  37.   router,
  38.   render: h => h(App),
  39. }).$mount('#app')
复制代码

后贴组件 Anli.vue 的
  1. <template>
  2.     <div id="anli">
  3.         这是案例组件
  4.       
  5.           <button @click="lmback()">点击触发返回上一步操作</button>
  6.         <h2>组件嵌套</h2>
  7.         <hr>
  8.         <ol>
  9.           <li> <router-link to="/anli/one"> 案例组件1</router-link></li>
  10.           <li> <router-link to="/anli/two"> 案例组件2</router-link></li>
  11.           <li> <router-link to="/anli/three"> 案例组件3 路由重定向的测试</router-link></li>
  12.           <li> <router-link :to="{ name: 'lmsix', params: { userId: 123 }}" > 案例组件6 路由命名的测试</router-link></li>
  13.         </ol>
  14.         <div class="lmbox">
  15.             <router-view></router-view>
  16.         </div>
  17.     </div>
  18. </template>
  19. <script>
  20. export default {
  21.   data() {
  22.     return {
  23.       b: false,
  24.     };
  25.   },
  26.   methods: {
  27.     // 广播数据
  28.     lmback() {
  29.       this.$router.go(-1);
  30.     }
  31.   },
  32.   mounted() {
  33.     //在组件加载的时候就接收
  34.   }
  35. };
  36. </script>
  37. <style>
  38. .lmbox{
  39.   margin:10px auto;
  40.   border:3px solid #eee;
  41.   width: 400px;
  42.   height: 200px;
  43. }
  44. </style>
复制代码

回复

使用道具 举报

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
 楼主| 发表于 2018-11-29 23:26:08 | 显示全部楼层
关于  回退 与前进 的 JS操作
  1. // go forward by one record, the same as history.forward()
  2. router.go(1)
  3. // go back by one record, the same as history.back()
  4. router.go(-1)
  5. // go forward by 3 records
  6. router.go(3)
  7. // fails silently if there aren't that many records.
  8. router.go(-100)
  9. router.go(100)
复制代码
  1. lmback() {
  2.       this.$router.go(-1);
  3.     }
复制代码
咳咳 这是加载路由工具后的 用法

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表