gpt4 book ai didi

javascript - Vuejs 3 webpack : Problem with vue-template-compiler

转载 作者:行者123 更新时间:2023-12-04 11:07:30 25 4
gpt4 key购买 nike

我正在尝试将 vuejs 3 集成到使用 webpack 的现有项目中。我阅读了有关 vue-loader 的信息,因此我正在尝试使用它。
在官方文档中我有这个:
每次发布新版本的vue,都会同时发布对应版本的vue-template-compiler。编译器的版本必须与基本的 vue 包同步,以便 vue-loader 生成与运行时兼容的代码。这意味着每次在项目中升级 vue 时,都应该升级 vue-template-compiler 以匹配它。
所以,当我尝试编译时,我得到了这个错误:

Vue packages version mismatch:

- vue@3.0.2 (/home/alejo/playground/parquesFrontend/node_modules/vue/index.js)
- vue-template-compiler@2.6.12 (/home/alejo/playground/parquesFrontend/node_modules/vue-template-compiler/package.json)

This may cause things to work incorrectly. Make sure to use the same version for both.
If you are using vue-loader@>=10.0, simply update vue-template-compiler.
If you are using vue-loader@<10.0 or vueify, re-installing vue-loader/vueify should bump vue-template-compiler to the latest.
但是当我尝试安装 vue-template-compiler@3.0.2 我得到这个错误:
❯ npm install vue-template-compiler@3.0.2
npm ERR! code ETARGET
npm ERR! notarget No matching version found for vue-template-compiler@3.0.2.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/alejo/.npm/_logs/2020-11-17T02_52_46_458Z-debug.log
我怎么解决这个问题?

最佳答案

要使 vue 3 在不使用 vite 或 vue cli 的情况下与 webpack 一起正常工作,请执行以下步骤:

  • 初始化package.json喜欢:
  • {
    "private": true,
    "name": "vue-3",
    "description": null,

    }
  • 安装 vue 的最新版本:
  • npm i --save vue@next vue-loader@next
  • 还安装包含 @vue/compiler-sfc 的开发依赖项替换 vue-template-compiler
  • npm i -D @vue/compiler-sfc css-loader file-loader mini-css-extract-plugin
    url-loader webpack webpack-cli webpack-dev-server
  • @vue/compiler-sfc
  • CSS加载器
  • 文件加载器
  • 迷你 CSS 提取插件
  • 网址加载器
  • vue-loader
  • 网页包
  • webpack-cli
  • webpack-dev-server
  • 使用以下内容创建或编辑您的 webpack.config.js:
  • const path = require("path");
    const { VueLoaderPlugin } = require("vue-loader");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");

    module.exports = (env = {}) => ({
    mode: env.prod ? "production" : "development",
    devtool: env.prod ? "source-map" : "cheap-module-eval-source-map",
    entry: [
    require.resolve(`webpack-dev-server/client`),
    path.resolve(__dirname, "./src/main.js")
    ].filter(Boolean),
    output: {
    path: path.resolve(__dirname, "./dist"),
    publicPath: "/dist/"
    },
    resolve: {
    alias: {
    // this isn't technically needed, since the default `vue` entry for bundlers
    // is a simple `export * from '@vue/runtime-dom`. However having this
    // extra re-export somehow causes webpack to always invalidate the module
    // on the first HMR update and causes the page to reload.
    vue: "@vue/runtime-dom"
    }
    },
    module: {
    rules: [
    {
    test: /\.vue$/,
    use: "vue-loader"
    },
    {
    test: /\.png$/,
    use: {
    loader: "url-loader",
    options: { limit: 8192 }
    }
    },
    {
    test: /\.css$/,
    use: [
    {
    loader: MiniCssExtractPlugin.loader,
    options: { hmr: !env.prod }
    },
    "css-loader"
    ]
    }
    ]
    },
    plugins: [
    new VueLoaderPlugin(),
    new MiniCssExtractPlugin({
    filename: "[name].css"
    })
    ],
    devServer: {
    inline: true,
    hot: true,
    stats: "minimal",
    contentBase: __dirname,
    overlay: true,
    injectClient: false,
    disableHostCheck: true
    }
    });

  • 添加 dev运行您的应用程序的脚本:
  • {
    "private": true,
    "scripts": {
    "dev": "webpack-dev-server"
    },
    "dependencies": {
    "vue": "^3.0.2"
    },
    "name": "vue-3",
    "description": null,
    "devDependencies": {
    ...
    }
    }

  • 填写index.html内容如下:
  • <link rel="stylesheet" href="/dist/main.css" />
    <div id="app"></div>
    <script src="/dist/main.js"></script>
    最后运行 npm run dev访问 http://localhost:8080/
    对于准备使用的项目,请尝试克隆此 REPOSITORY 按照上述步骤构建的。
    Edit webpack-vue3

    关于javascript - Vuejs 3 webpack : Problem with vue-template-compiler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64868632/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com