Vuejs 3:vue-template-compiler 的问题

Ale*_*ejo 24 javascript webpack vue.js vue-loader vuejs3

我正在尝试将 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.
Run Code Online (Sandbox Code Playgroud)

但是当我尝试安装 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
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

Bou*_*him 31

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

  1. 初始化之package.json类的:
{
    "private": true,
    "name": "vue-3",
    "description": null,
   
}
Run Code Online (Sandbox Code Playgroud)
  1. 安装最新版本的 vue :

npm i --save vue@next vue-loader@next

  1. 还安装包含@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
Run Code Online (Sandbox Code Playgroud)
  • @vue/编译器-sfc
  • css加载器
  • 文件加载器
  • mini-css-extract-plugin
  • 网址加载器
  • vue-loader
  • 网络包
  • webpack-cli
  • webpack-dev-server
  1. 使用以下内容创建或编辑您的 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
  }
});

Run Code Online (Sandbox Code Playgroud)
  1. 添加dev脚本以运行您的应用程序:
{
    "private": true,
    "scripts": {
        "dev": "webpack-dev-server"
    },
    "dependencies": {
        "vue": "^3.0.2"
    },
    "name": "vue-3",
    "description": null,
    "devDependencies": {
      ...
    }
}

Run Code Online (Sandbox Code Playgroud)
  1. 填写index.html以下内容:
<link rel="stylesheet" href="/dist/main.css" />
<div id="app"></div>
<script src="/dist/main.js"></script>
Run Code Online (Sandbox Code Playgroud)

最后运行npm run dev访问http://localhost:8080/

对于准备使用的项目,请尝试克隆按照上述步骤构建的这个REPOSITORY

编辑 webpack-vue3


Mic*_*evý 10

我相信你需要在Vue 3 中使用vue-loader@next

在Vue公司3证监会编译包不再vue-template-compiler,但compiler-sfc点击这里

我完全同意使用 Vue CLI 来管理项目的建议——它会为你省去管理所有依赖项的很多麻烦(尤其是现在当 Vue 3 生态系统试图赶上 Vue 3 发布并且许多工具事件不没有任何迁移文档....像 vue-loader)

如果您因为现有项目已经有 Webpack 配置而无法使用 CLI,您仍然可以使用 CLI 作为指导。只需在一边生成新项目,使用vue inspect命令检查它正在使用的 Webpack 配置以及package.json所需的依赖项...