Uncaught TypeError: Cannot redefine property: $router

简介: Uncaught TypeError: Cannot redefine property: $router

问题

今天vuecli3打包完之后,线上访问页面报错:无法重新定义属性:$router ,错误如下:

原因以及解决方式

https://stackoverflow.com/questions/52209717/vuejs-uncaught-typeerror-cannot-redefine-property-router

报这个错误的原因就是多次引入了 vue-router,我的 tpl.html 添加了cdn

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <div id="app"></div>
    <% if (NODE_ENV === 'production') { %>
      <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
      <script src="https://cdn.bootcdn.net/ajax/libs/vue-router/3.2.0/vue-router.min.js"></script>
    <% } %>
  </body>
</html>

然后 vue.config.js 配置如下:

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
module.exports = {
    publicPath: process.env.NODE_ENV === "production" ? "/kaimo/" : "/kaimo",
    // 将构建好的文件输出到哪里
    outputDir: "kaimo",
    pages: {
        index: {
            entry: "src/main.js",
            template: "src/tpl.html",
            filename: "index.html",
            title: "测试页面",
            chunks: ["chunk-vendors", "chunk-common", "index"]
        },
    },
    // 生产环境下的sourceMap
    productionSourceMap: false,
    chainWebpack: config => {
    },
    configureWebpack: config => {
        if (process.env.NODE_ENV === "production") {
            config.optimization.minimizer = [
                new UglifyJsPlugin({
                    sourceMap: false,
                    uglifyOptions: {
                        output: {
                            comments: true, // 删除注释
                        },
                        warnings: false,
                        compress: {
                            drop_console: true,
                            drop_debugger: true, // 删除debugger
                            pure_funcs: ["console.log"],
                        }
                    }
                })
            ];
        }
    }
};

我们可以看到 vue-router 打包到 bundle 中了,然后我又加了cdn,这样就会多了一次。

我们可以在 vue.config.js 中配置 externals,或者去掉 cdn 的引入,直接用打包的。下面讲一下配置 externals 的方式。

externals 它配置项提供了阻止将某些 import 的包打包到 bundle 中的功能,在运行时再从外部获取这些扩展依赖。

config.externals({
    key: value
});
  • key:是第三方依赖库的名称
  • value:值可以是字符串、数组、对象。CDN引入的 js 文件赋值给window的全局变量名称。

这里我们配置忽略打包文件:

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
module.exports = {
    publicPath: process.env.NODE_ENV === "production" ? "/kaimo/" : "/kaimo",
    // 将构建好的文件输出到哪里
    outputDir: "kaimo",
    pages: {
        index: {
            entry: "src/main.js",
            template: "src/tpl.html",
            filename: "index.html",
            title: "测试页面",
            chunks: ["chunk-vendors", "chunk-common", "index"]
        },
    },
    // 生产环境下的sourceMap
    productionSourceMap: true,
    chainWebpack: config => {
        if (process.env.NODE_ENV === "production") {
            // 忽略的打包文件
             config.externals({
                 "vue": "Vue",
                 "vue-router": "VueRouter"
             });
         }
    },
    configureWebpack: config => {
        if (process.env.NODE_ENV === "production") {
            config.optimization.minimizer = [
                new UglifyJsPlugin({
                    sourceMap: true,
                    uglifyOptions: {
                        output: {
                            comments: true, // 删除注释
                        },
                        warnings: false,
                        compress: {
                            drop_console: true,
                            drop_debugger: true, // 删除debugger
                            pure_funcs: ["console.log"],
                        }
                    }
                })
            ];
        }
    }
};

这个样我们就不会报错了。

目录
相关文章
|
JavaScript 前端开发 NoSQL
【入门毕设项目】基于 Vue 的医院门诊预约挂号系统(一)
【入门毕设项目】基于 Vue 的医院门诊预约挂号系统
801 0
|
4月前
|
JSON 监控 API
淘宝京东比价接口,商品详情AP接口概述
淘宝和京东作为国内两大主流电商平台,均提供了商品详情API接口,支持开发者获取商品信息以实现比价、数据分析等功能。
项目打包优化上线Uncaught TypeError: Cannot redefine property: $router
项目打包优化上线Uncaught TypeError: Cannot redefine property: $router
|
12月前
|
JavaScript 前端开发 UED
vue中vue-router路由懒加载(按需加载)的作用以及常见的实现方法
vue中vue-router路由懒加载(按需加载)的作用以及常见的实现方法
585 1
|
JavaScript 前端开发
什么是前端构建工具?vite和vite脚手架的关系!
【8月更文挑战第1天】前端构建工具简析
207 4
Cannot read properties of undefined (reading ‘$router‘)
Cannot read properties of undefined (reading ‘$router‘)
|
JavaScript
Vue PDF预览(微信公众号,app也可用)
Vue PDF预览(微信公众号,app也可用)
479 0
|
JavaScript
Ant design Vue 父子组件传递(代码案例--不懂的地方可以私信本博主)
Ant design Vue 父子组件传递(代码案例--不懂的地方可以私信本博主)
272 0
|
JavaScript
【Error】Vue.js is detected on this page. Open DevTools and look for the Vue panel.
【Error】Vue.js is detected on this page. Open DevTools and look for the Vue panel.
|
前端开发 JavaScript API
React与Vue 3:谁在前端框架中更具有优势?
React与Vue 3:谁在前端框架中更具有优势?