• vue-router 动态路由


    App.vue

    <template>
      <div id="nav">
        <!-- 导航 -->
        <router-link to="/" active-class="current" replace>Home</router-link> |
        <router-link to="/about" active-class="current" replace>About</router-link> |
        <router-link to="/test1_bak">Test1_bak</router-link> |
        <router-link to="/test1">Test1</router-link> |
        <router-link to="/test2">Test2</router-link> |
        <router-link :to="'/test3/'+test3_id">Test3</router-link>
      </div>
      <!-- 路由出口 -->
      <router-view/>
    </template>
    <script>
    import {ref} from "vue";
    
    export default {
      setup(){
        const test3_id = ref('001')
    
        return{
          test3_id
        }
      }
    
    }
    </script>
    <style>
    #app {
      font-family: Avenir, Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
    }
    
    #nav {
      padding: 10px;
      background-color: lightskyblue;
    }
    
    #nav a {
      font-weight: bold;
      color: #2c3e50;
    }
    
    /* 默认选中样式类 */
    #nav a.router-link-exact-active {
      color:orangered;
    }
    
    .current{
      font-size: 24px;
      font-weight: bolder;
    }
    </style>

    index.js

    //引入
    import { createRouter, createWebHashHistory } from 'vue-router'
    import Home from '../views/Home.vue'
    import Test3 from '../views/Test3.vue'
    
    //创建路由对象
    const routes = [
      {
        path: '/',
        name: 'Home',
        component: Home
      },
        //路由重定向
      {
        path: '/home',
        redirect:'/'
      },
      {
        path: '/about',
        name: 'About',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
      },
      {
        path: '/test1_bak',
        name: 'Test1_bak',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/Test1_bak.vue')
      },
      {
        path: '/test1',
        name: 'Test1',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/Test1.vue')
      },
      {
        path: '/test2',
        name: 'Test2',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/Test2.vue')
      },
      {
        path: '/test3/:id',
        name: 'Test3',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: Test3
      }
    ]
    
    const router = createRouter({
      history: createWebHashHistory(),
      linkActiveClass:'current',
      routes
    })
    
    //导出路由对象
    export default router

    Test3.vue

    <template>
      <div>
        <h1>This is an test3 page</h1>
        test3_id: {{$route.params.id}}
      </div>
    </template>
    <script>
    import {useRoute} from "vue-router";
    
    export default {
      name: 'Test3',
      setup(){
        const route = useRoute();
        console.log(route);
        console.log(route.params.id);
      }
    }
    </script>
  • 相关阅读:
    Angularjs总结(一)表单验证
    list集合中指定字段去重
    NodeJS学习笔记—2.AMD规范
    NodeJS学习笔记—1.CommonJS规范
    WCF上传、下载、删除文件
    .net RAW(16)与GUID互相转换
    Angularjs总结(六) 上传附件
    可以打开mdb文件的小软件
    数据库导出导入操作(expdp,impdp)
    用Ueditor存入数据库带HTML标签的文本,从数据库取出来后,anjular用ng-bind-html处理带HTML标签的文本
  • 原文地址:https://www.cnblogs.com/mingforyou/p/15214278.html
Copyright © 2020-2023  润新知