标签:不同 his render ali 引入 导航 recommend 视图 上线
根据地址栏的改变渲染不同的组件
router: 路由器,代表的是整体
route:路由,代表的是个体
<router-link></router-link><router-view></router-view> npm install vue-router  1.引入vue 
    import Vue from 'vue'
  2.引入路由
    import VueRouter from 'vue-router'
  3.使用路由
    Vue.use(VueRouter)
  4.创建路由对象
    let router = nwe VueRouter({
    
    })
  5.抛出路由对象
    export default router
  6.配置路由 地址栏路径和组件的联系
    (1)引入组件
        import component1 from ‘../component1.vue’ 
      let router = nwe VueRouter({
            //配置路由路径及其跳转的组件
            routes;[
                {
                  path : '/component1',
                  components:component1
                }
            ]
        })
import router from './router.index.js' //index.js可以省略
new Vue({
  router:router,    //key value一样 可以省略
  render: h => h(App),
}).$mount('#app')
import router from './router'
new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
在页面开启一片空间 渲染不同的组件
//路由中可以通过对象和字符串进行访问
//不区分大小写
//直接跳转
<router-link to="/home" ></router-link>>
//访问路由设置path为/home的路由
<router-link :to="{path:"/home"}" replace></router-link>
//访问name:“home”的路由 //区分大小写
<router-link :to="{name:"home"}" replace></router-link>query
//传值
    <router-link :to="{path:'/one',query:{a:2,b:3,c:{}}}">One</router-link>|
    <router-link :to="{name:'one',query:{a:5,b:7,c:{d:9}}}">One</router-link>|
//接收
 console.log(this.$route.query.a,this.$route.query.b,this.$route.query.c);
- 分析
    优点:刷新数据库不会丢失。
    缺点:如果传递的是对象。如果刷新接收的数据会变成对象的原始值。[Object,Object]params
//传值
     <!--params:只可以通过name进行数据传递。-->
     <router-link :to="{name:'two',params:{a:1,b:2,c:{d:4}}}">Two</router-link>|
//接收
console.log(this.$route.params.a,this.$route.params.b,this.$route.params.c);
- 分析
    优点:可以传递对象
    缺点:刷新数据丢失。//传值
      <!--<router-link to="/three/1/2">Three</router-link>|-->
        <!--<router-link to="/three-11-21.html">Three</router-link>|-->
        <!--<router-link to="/three/123412341234.html">Three</router-link>|-->
        <router-link :to="{path:'/three/123412341234.html',query:{a:1,b:2}}">Three</router-link>|
//接收
  console.log(this.$route.params.id,this.$route.params.type)
- 分析
    优点:刷新数据存在。可以让你的地址更加漂亮。
    缺点:不可以传递对象<router-link :to="/home" replace></router-link><router-link :to="/home" tag="li">Home</router-link>
<!-- 渲染结果 -->
<li>Home</li>
//此时页面的li同样会起到a链接跳转的结果,vue会自动为其绑定点击事件,并跳转页面<router-link :to="/home" active-class="u-link--Active">Home</router-link>router-link的严格模式<router-link :to="/" exact>home</router-link>to 属性控制地址栏改变
tag 属性控制渲染的元素
active-class 添加活跃状态的类名
hash 有#
history 没有#
let router=new VueRouter({
  mode:'history',//'hash'默认,
})如果path特别长,可以给路由通过name属性起一个名字
切换的时候通过name 进行切换
 routes:[
     {
         path:'/recommend/a/b/cc/d/d/e/e/d/d/d/d',
         name:'ha',
     }
   ]
<router-link :to="name:'ha'"></router-link>也就是给router-view命名
可以让router-view渲染不同的组件
//1.给视图添加name属性 
<router-view name="r1"></router-view>
<router-view name="r2"></router-view>
//2.配置路由index.js
  //- 引用
  import Recommend1 from  '../Recommend1.vue'
  import Recommend2 from  '../Recommend2.vue'
  //- 配置
   components:{
         r1:Recommend1,
         r2:Recommend2,
       }
     //注:components  加了s没起名字的是默认
<router-view></router-view>
components:{
         default:Recommend,
         r1:Recommend1,
         r2:Recommend2,
       }声明式导航是写在组件的template中,通过router-link来触发    //$router路由对象
    this.$router.push("/my");
    this.$router.push({path:"/my"});
    this.$router.push({name:"my"});
    this.$router.go(-1)// 返回 1前进
    this.$router.go(1)// 前进当跳转页面为/a/b/c/d
push返回会先返回到/c,到/b,到/a
replace替换返回会直接返回到最初
刚进入网页进行默认组件的显示
    {
      path:"/home",   // 当访问的地址为/home 会将地址重定向到/my
      redirect:"/my" 
  }this.$router.push({path:'/singer',query:{us:123,ps:123}})
this.$router.push({name:'singer',query:{us:123,ps:123}})this.$router.push({name:'singer',params:{us:123,ps:123}})    //在配置路由时加‘:’  
   {
      path:'/singer/:hehe/:xixi',
      name:'singer',
      component:Singer
    },
    //无论地址栏写什么都能跳转 
    //例:/singer/1/2 params对象为
        params:{hehe:'1',xixi:'2'}路由里套路由 ,router-view里套router-view
 <router-view></router-view> 
//一定要记得引用
    {
      path:'/my',
      component:My,
      children:[
        {
          path:'login', //不需要加’/‘
          component:Login
        },{
          path:'info',
          component:Info
        }
      ]
    },404
    {
        path:"*",// 当找到匹配的路由时,该路由生效。
        component:()=>import("@/views/Error"),
        meta:{
            isHide:true
        }
    }alias :”/“   相当于给该路由加一个入口 标签:不同 his render ali 引入 导航 recommend 视图 上线
原文地址:https://www.cnblogs.com/zhaoxinran997/p/12348835.html