vue路由配置和404页面配置

 一.打开vue router官网:入门 | Vue RouterVue.js 的官方路由https://router.vuejs.org/zh/guide/

安装教程如下:

在终端运行如下代码:

然后就不按照教程走了。

在你的项目文件中,src文件夹下,新建一个router文件,再在router文件夹下新建一个index.js文件。

在index.js文件中写如下代码:

import {
    createRouter as _createRouter,
    createWebHashHistory,
} from 'vue-router'

// import index from "~/pages/index.vue"
const routes = [
]

//这里有错误,不知道什么原因只能使用函数返回,直接定义会报错
function createRouter() {
    return _createRouter({
        history: createWebHashHistory(),
        routes
    })
}

const router = createRouter()

export default router

在main.js下写如下代码:

二.配置路由跳转

随便在src下创建一个空的vue文件:

index.vue:

<script setup>

</script>

<template>
  <div>后台首页</div>
</template>

<style scoped>
</style>

打开刚刚创建的router底下的index.vue进行router配置:

//新增如下:
const routes = [
    {
        path:"/",
        component:index
    }
]

完整代码:

import {
    createRouter as _createRouter,
    createWebHashHistory,
} from 'vue-router'

import index from "~/pages/index.vue"
//新增如下:
const routes = [
    {
        path:"/",
        component:index
    }
]

//这里有错误,不知道什么原因只能使用函数返回,直接定义会报错
function createRouter() {
    return _createRouter({
        history: createWebHashHistory(),
        routes
    })
}

const router = createRouter()

export default router

别忘了在App.vue里面引入路由:

<script setup>

</script>

<template>
  <div></div>
  <router-view></router-view>
</template>

<style scoped>
</style>

三.404页面配置

新建一个404页面:

 

<!-- eslint-disable vue/multi-word-component-names -->
<script setup>

</script>
<template>
    <div class="py-18">
        <el-result
            icon="warning"
            title="404提示"
            sub-title="你找的页面走丢了~"
        >
            <template #extra>
            <el-button type="primary" size="medium" @click="$router.push('/')">返回首页</el-button>
            </template>
        </el-result>
    </div>
</template>

<style scoped>
</style>

打开vue-router官网:

找到:

在路由页面进行如下配置:

import {
    createRouter as _createRouter,
    createWebHashHistory,
} from 'vue-router'

import index from "~/pages/index.vue"
//新增如下:
const routes = [
    {
        path: "/",
        component: index
    },
    //404页面捕获
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound', 
        component: () => import('~/pages/404.vue')
    },
]

//这里有错误,不知道什么原因只能使用函数返回,直接定义会报错
function createRouter() {
    return _createRouter({
        history: createWebHashHistory(),
        routes
    })
}

const router = createRouter()

export default router

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值