Vue路由的使用

如图所示:

1.首先创建文件,views中有以上示例图看到的创建的以下路由页面名称:

这个是创建的名字,为了方便看清我把第1步创建的名字写在下面了,为了方便看到这篇文章,创建名字不明白的可以按照我这个名创建,为了不出错误

CarView.vue       //最上面显示的页面CarView
CarViewOne.vue    //这两个是点击第一个页面显示出来的下面跳转的路由页面
CarViewTwo.vue    
PhoneView.vue     //最上面显示的页面PhoneView

然后在router下的index.js中复制以下内容:

import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import CarView from '@/views/CarView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'car',
    component: CarView,
   
  },
  {
    path: '/car',
    name: 'car',
    // 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: CarView,
    children: [
      {
        name: "/car/carone",//此处加名字
        path: "carone",
        component: () => import(/* webpackChunkName: "about" */ '../views/CarViewOne.vue')
      },
      {
        name: "/car/cartwo",//此处加名字
        path: "cartwo",
        component: () => import(/* webpackChunkName: "about" */ '../views/CarViewTwo.vue')
      },
    ],
  },
  {
    path: '/phone',
    name: 'phone',
    // 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/PhoneView.vue')
  },
]

const router = new VueRouter({
  routes
})

export default router

app.vue中:

<template>
  <div id="app">
    <nav>
      <!-- <router-link to="/">Home</router-link> |
      <router-link to="/about">About</router-link> -->
      <router-link to="/">CarView</router-link> |
      <router-link to="/phone">PhoneView</router-link>
    </nav>
    <router-view/>
  </div>
</template>

<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style>

CarView.vue中:

<template>
    <div>
<h1>页面</h1>
<button @click="LookRouter">查看路由信息</button>

<hr>
<router-link to="/car/carone">车1</router-link> |
      <router-link to="/car/cartwo">车2</router-link>
      <router-view/>
    </div>
</template>
<script>
export default{
    // name:car,
    data(){
        return{
    
}
    },
    methods:{
LookRouter(){
    // console.log(this.$route); //路由信息对象
    // console.log(this.$router); //路由实例对象
}
    }
}
</script>

PhoneView.vue中:

<template>
    <div>
22222
    </div>
</template>
<script>
export default{
    data(){
return{

}
    }
}
</script>

CarViewOne.vue中:

<template>
    <div>
<h1>我是车1</h1>

    </div>
</template>
<script>
export default{
    data(){
        return{
    
}
    },

}
</script>

CarViewTwo.vue中:

<template>
    <div>
<h1>我是车2</h1>

    </div>
</template>
<script>
export default{
    data(){
        return{
    
}
    },

}
</script>

相关推荐

  1. Vue中嵌套(子使用

    2024-06-14 03:52:03       35 阅读
  2. vue使用

    2024-06-14 03:52:03       42 阅读
  3. Vue使用

    2024-06-14 03:52:03       62 阅读
  4. Vue3:vue-router使用

    2024-06-14 03:52:03       41 阅读
  5. 使用

    2024-06-14 03:52:03       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-14 03:52:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-14 03:52:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-14 03:52:03       82 阅读
  4. Python语言-面向对象

    2024-06-14 03:52:03       91 阅读

热门阅读

  1. vue封装全局的防抖节流函数

    2024-06-14 03:52:03       32 阅读
  2. 用Python编写自动发送每日电子邮件报告的脚本

    2024-06-14 03:52:03       28 阅读
  3. SuntoryProgrammingContest2024(AtCoder Beginner Contest 357)

    2024-06-14 03:52:03       28 阅读
  4. trpc快速上手

    2024-06-14 03:52:03       29 阅读
  5. kotlin 中的字符

    2024-06-14 03:52:03       29 阅读
  6. 计算机网络

    2024-06-14 03:52:03       30 阅读
  7. 【动态规划算法题记录】 509. 斐波那契数

    2024-06-14 03:52:03       30 阅读