码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • vue2配置路由及运行过程中遇到的问题


    vue2配置路由及运行过程中遇到的问题

      • 1、报错:export 'default' (imported as 'VueRouter') was not found in 'vue-router'
      • 2、报错:Catch all routes ("*") must now be defined using a param with a custom regexp.
      • 3、报错:Refused to apply style from 'http://localhost:8080/iconfont.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
      • 4、vue2项目错误下载了vue-router 4.x版本

    1、报错:export ‘default’ (imported as ‘VueRouter’) was not found in ‘vue-router’

    原代码:

    //引入插件
    import Vue from 'vue';
    import VueRouter from 'vue-router';
    
    //使用插件
    Vue.use(VueRouter);
    
    //引入路由组件
    import Home from '@/pages/Home';
    import Login from '@/pages/Login';
    import Register from '@/pages/Register';
    import Search from '@/pages/Search';
    
    //配置路由
    export default new VueRouter({
        //配置路由
        routes: [
            {
                path: "/home",
                component: Home
            },
            {
                path: "/login",
                component: Login
            },
            {
                path: "/register",
                component: Register
            },
            {
                path: "/search",
                component: Search
            },
            // 重定向,在项目跑起来的时候,访问/,立马让他重定向到首页
            {
                path: "*",
                redirect: "/home"
            }
        ]
    })
    
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    原因:vue-Router@4.0+不支持这种写法
    解决:

    //引入插件
    import Vue from 'vue';
    import { createRouter, createWebHistory } from "vue-router";
    
    //引入路由组件
    import Home from '@/pages/Home';
    import Login from '@/pages/Login';
    import Register from '@/pages/Register';
    import Search from '@/pages/Search';
    
    const routes = [
        {
            path: "/home",
            component: Home
        },
        {
            path: "/login",
            component: Login
        },
        {
            path: "/register",
            component: Register
        },
        {
            path: "/search",
            component: Search
        },
        // 重定向,在项目跑起来的时候,访问/,立马让他重定向到首页
        {
            path: "/:pathMatch(.*)",
            redirect: "/home"
        }
    ]
    
    const router = createRouter({
        history: createWebHistory(),
        routes
    })
    
    export default router
    
    • 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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    2、报错:Catch all routes (“*”) must now be defined using a param with a custom regexp.

    解决:将*变为/:pathMatch(.*)

    {
    	path: "/:pathMatch(.*)",
    	redirect: "/home"
    }
    
    • 1
    • 2
    • 3
    • 4

    3、报错:Refused to apply style from ‘http://localhost:8080/iconfont.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.

    解决:删除reset.css中的@import "./iconfont.css";

    4、vue2项目错误下载了vue-router 4.x版本

    原因:cnpm install --save vue-router自动安装最新版本vue-router,在解决上述问题时发现路由仍注册失败,发现vue2应使用vue-router 3.x ,vue-router 4.x 是和 vue3 结合使用的使用
    解决:降低vue-router版本,配置恢复之前的

    cnpm i vue-router@3.5.2 -legacy-peer-deps
    
    • 1
  • 相关阅读:
    读书笔记-Java并发编程的艺术-3.1
    WPF将dll文件嵌入到exe文件中
    和为S的两个数字
    MySQL知识笔记——中级进阶之索引(实施工程师和DBA工作笔记)
    【学计算机都可收藏】《信息资源管理》第6章,信息资源安全管理
    K8S入门前奏之VMware虚拟机网络配置
    vue 实现数字验证码功能
    C# 第三方曲线库及其特点
    Windows系统杀掉某个端口的方法
    2020年之前的往事……
  • 原文地址:https://blog.csdn.net/qq_37344867/article/details/126138464
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号