zoukankan      html  css  js  c++  java
  • 路由完整实例代码

    D:workspacexxxsrcmain.js

    import Vue from 'vue'
    import App from './App'
    import VueRouter from 'vue-router'
    import Apple from '@/components/Apple'
    import RedApple from '@/components/RedApple'
    import Banana from '@/components/Banana'
    
    Vue.use(VueRouter)
    
    let router = new VueRouter({
      mode: 'history',
      routes: [
        {
          path: '/',
          // redirect: Apple 错误示范
          redirect: '/apple'
        },
        {
          path: '/apple',
          component: Apple,
          // component: {
          //   ViewA: Apple,
          //   ViewB: RedApple
          // },
          children: [
            {
              path: 'red',
              component: RedApple
            }
          ]
        },
        {
          path: '/banana',
          component: Banana
        }
      ]
    })
    // router.beforeEach() 检查登录状态
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      router,
      template: '<App/>',
      components: { App }
    })
    

      

    D:workspacexxxsrcApp.vue

    <template>
      <div id="app">
        <img src="./assets/logo.png">
        <!-- <router-view name="ViewA"></router-view>
        <router-view name="ViewB"></router-view> -->
        <transition>
          <keep-alive>
            <router-view/>
          </keep-alive>
        </transition>
        <router-link to="/apple">to apple</router-link>
        <!-- 具名路由<router-link :to="{name: 'apple'}" tag="li">to apple</router-link> -->
        <router-link to="/banana">to banana</router-link>
        <!-- <router-link to="/apple/red">to apple red</router-link> -->
      </div>
    </template>
    
    <script>
    export default {
      name: 'App'
    }
    </script>
    
    <style>
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    </style>
    

      

    D:workspacexxxsrccomponentsApple.vue

    <template>
        <div>
          <h1 >{{ msg }}</h1>
          <p>{{ this.$route.params.color }}</p>
          <button @click="showSize">showSize</button>
          <router-view/>
        </div>
    </template>
    
    <script>
    export default {
      name: 'Apple',
      data () {
        return {
          msg: 'I am an apple'
        }
      },
      methods: {
        showSize () {
          console.log(this.$route.params)
        }
      }
    }
    </script>
    

      D:workspacexxxsrccomponentsBanana.vue

    <template>
        <h3 >{{ mg }}</h3>
    </template>
    
    <script>
    export default {
      name: 'Banana',
      data () {
        return {
          mg: 'I am a banana'
        }
      }
    }
    </script>
    

      D:workspacexxxsrccomponentsRedApple.vue

    <template>
        <div>
          <h1 >{{ msg }}</h1>
          <p>{{ this.$route.params.color }}</p>
          <button @click="showSize">showSize</button>
          <router-view/>
        </div>
    </template>
    
    <script>
    export default {
      name: 'RedApple',
      data () {
        return {
          msg: 'I am a red apple'
        }
      },
      methods: {
        showSize () {
          console.log(this.$route.params)
        }
      }
    }
    </script>
    

      

  • 相关阅读:
    PAT 1007 Maximum Subsequence Sum 最大连续子序列和
    数据库系统原理课程设计——图书借阅管理系统
    计算机组成原理——数据的表示与运用
    win7系统下安装Ubuntu18.04组成双系统
    C++中set用法详解
    C++ getline函数用法详解
    数据库——关系演算
    计算机组成原理——计算机系统概述考研题
    PAT 1002 A+B for Polynomials(map模拟)
    PAT 1001 A+B Format
  • 原文地址:https://www.cnblogs.com/tabCtrlShift/p/9163234.html
Copyright © 2011-2022 走看看