zoukankan      html  css  js  c++  java
  • vue动态路由

    我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件。例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染。能够提供参数的路由即为动态路由
    第一步:定义组件

    const Goods = {
          template: `
            <div>{{this.$route.params}}商品</div>
          `,
          created(){
            console.log(this.$route.params);
          },
          watch:{
            $route(){
              console.log(this.$route.params);
            }
          }
        }

     第二部配置路由

    const routes = [
          {
            path: '/goods/:type', 
            component: Goods
          }
    
        ]
    
        const router = new VueRouter({
          routes
        })
    
        const app = new Vue({
          el: '#app',
          router
        })

    第三不配置模板

    <router-view></router-view>
        <router-link to="/goods/book">图书商品</router-link>
        <router-link to="/goods/car">汽车商品</router-link>
        <router-link to="/goods/food">美食商品</router-link>
    //监听路由
    const Goods = {
          template: `
            <div>{{this.$route.params}}商品</div>
          `,
          watch:{
            '$route': {
                handler: function(){
                  console.log(this.$route.params);
                },
                immediate: true  (立刻)
              }
          }
    希望自己写的东西能够对大家有所帮助!谢谢
  • 相关阅读:
    ADO.NET的记忆碎片(四)
    ADO.NET的记忆碎片(八)
    卡特兰数 应用
    hdu 1249 三角形
    hdu 1143
    nyist 93 汉诺塔(三)
    hdu 1123 Train Problem II
    hdu 1133 Buy the Ticket
    hdu 1022 Train Problem I
    nyist 610 定长覆盖
  • 原文地址:https://www.cnblogs.com/mrxinxin/p/10138385.html
Copyright © 2011-2022 走看看