• 【Vue 开发实战】生态篇 # 19:Vue Router的使用场景


    说明

    【Vue 开发实战】学习笔记。

    传统开发模式

    • www.xxx.com —— index.html
    • www.xxx.com/about —— about.html
    • www.xxx.com/xxx —— xxx.html

    单页面(SPA)开发模式

    • www.xxx.com —— index.html
    • www.xxx.com/about —— index.html
    • www.xxx.com/xxx —— index.html

    解决的问题

    • 监听 URL 的变化,并在变化前后执行相应的逻辑
    • 不同的 URL 对应不同的不同的组件
    • 提供多种方式改变 URL 的 API ( URL的改变不能导致浏览器刷新)

    使用方式

    • 提供一个路由配置表,不同 URL 对应不同组件的配置
    • 初始化路由实例 new VueRouter()
    • 挂载到 Vue 实例上
    • 提供一个路由占位,用来挂载 URL 匹配到的组件

    实战例子

    完整的请看:https://github.com/kaimo313/vue-development-practice/tree/master/router-demo

    router-demo\src\main.js

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    import App from './App.vue'
    import routes from './routes'
    
    Vue.config.productionTip = false
    
    Vue.use(VueRouter)
    
    const router = new VueRouter({
      mode: 'history',
      routes,
    })
    
    new Vue({
      router,
      render: h => h(App),
    }).$mount('#app')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    router-demo\src\routes.js

    import RouterDemo from './components/RouterDemo'
    import RouterChildrenDemo from './components/RouterChildrenDemo'
    
    const routes = [
      { path: '/foo', component: RouterDemo, name: '1' },
      { path: '/bar', component: RouterDemo, name: '2' },
      // 当 /user/:id 匹配成功,
      // RouterDemo 会被渲染在 App 的 
      { path: '/user/:id', 
        component: RouterDemo, 
        name: '3',
        props: true,
        children: [
          {
            // 当 /user/:id/profile 匹配成功,
            // RouterChildrenDemo 会被渲染在 RouterDemo 的 
            path: 'profile',
            component: RouterChildrenDemo,
            name: '3-1'
          },
          {
            // 当 /user/:id/posts 匹配成功
            // RouterChildrenDemo 会被渲染在 RouterDemo 的 
            path: 'posts',
            component: RouterChildrenDemo
          }
        ]
      },
      { path: '/a', redirect: '/bar' },
      { path: '*', component: RouterDemo, name: '404' }
    ]
    
    export default routes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    router-demo\src\components\RouterDemo.vue

    <template>
      <div>
        <router-link to="/foo">Go to Foorouter-link>
        <br/>
        <router-link to="/user/12">Go to /user/12router-link>
        <br/>
        <router-link to="/user/12/profile">Go to /user/12/profilerouter-link>
        <br/>
        <router-link to="/other">Go to 404router-link>
        <br/>
        <router-link to="/a">Go to a 重定向到 barrouter-link>
        <br/>
        <a href="#/foo">Go to Fooa>
        <br/>
        <button @click="$router.push('foo')">Go to Foobutton>
        <p>id: {{id}}p>
        <p>{{routerInfo}}p>
        <router-view>router-view>
      div>
    template>
    <script>
    export default {
      props: ['id'],
      computed: {
        routerInfo() {
          const { fullPath, path, name, params, query, meta } = this.$route
          return {
            fullPath, path, name, params, query, meta
          }
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    router-demo\src\components\RouterChildrenDemo.vue

    <template>
      <div>
        {{routerInfo}}
      div>
    template>
    <script>
    export default {
      computed: {
        routerInfo() {
          const { fullPath, path, name, params, query, meta } = this.$route
          return {
    fullPath, path, name, params, query, meta
          }
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    效果如下:

    在这里插入图片描述

  • 相关阅读:
    【SpringBoot】SpringBoot开启MyBatis缓存+ehcache(一二级缓存和myBatis的差不多,第三方缓存是jar包的不一样)
    。。。springboot
    TCS34725颜色感应识别模块
    【SpringBoot2】-【核心功能】
    ArcGIS中的Python入门知识点整理
    全面解析C语言多媒体开源框架GStreamer
    Vue3+TypeScript+Vite如何使用require动态引入类似于图片等静态资源
    vue3 webviewAPP
    xgboost配置GPU
    Redis 配置文件信息中文翻译版
  • 原文地址:https://blog.csdn.net/kaimo313/article/details/126701811