NO10 vueCli 项目中添加router

78 阅读1分钟

vue-router官网

app.vue

<template>

    <router-view></router-view>

</template>

main/index.vue

<template>

    <div class="nav">

        <router-link to="/carousel">轮播图</router-link>

        <router-link to="/about">Go to About</router-link>

    </div>

    <router-view></router-view>

</template>

<style scoped>

    .nav {

        display: flex;

        justify-content: space-around;

        font-size20px;

        line-height40px;

        border-bottom1px solid #ccc;

    }

    a {

        color#000000;

        text-decoration: none;

        cursor: pointer;

    }

    .router-link-active {

        color#fe5000;

    }

</style>

router/index.js

'use strict'

import { createRouter, createWebHistory } from 'vue-router'

const routes = [

    {

        path'',

        component() => import('../views/main/Index.vue'),

        redirect'/carousel',

        children: [

            {

                path'/carousel',

                name'carousel',

                component() => import('../views/carousel/Index.vue'),

                meta: {

                    title'轮播图'

                } 

             },

        ]

    }          

]


const router = createRouter({

    historycreateWebHistory(),

    routes

})


export default router

main.js

import { createApp } from 'vue'

import App from './App.vue'


import router from '@/router'


createApp(App)

    .use(router)

    .mount('#app')