Vuejs 3:vue-template-compiler出现问题 [英] Vuejs 3: Problem with vue-template-compiler

查看:2036
本文介绍了Vuejs 3:vue-template-compiler出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将vuejs 3集成到使用webpack的现有项目中.我读了有关vue-loader的文章,因此我正在尝试使用它.

在官方文档中我有以下内容:

每次发布​​新版本的vue时,都会同时发布相应版本的vue-template-compiler.编译器的版本必须与基本vue软件包同步,以便vue-loader生成与运行时兼容的代码.这意味着每次在项目中升级vue时,都应同时升级vue-template-compiler以使其匹配.

因此,当我尝试编译时,出现此错误:

  Vue软件包版本不匹配:-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)这可能会导致事情无法正常运行.确保两者使用相同的版本.如果您使用的是vue-loader @> = 10.0,只需更新vue-template-compiler.如果您使用的是vue-loader @< 10.0或vueify,则重新安装vue-loader/vueify会将vue-template-compiler升级到最新版本. 

但是当我尝试安装vue-template-compiler@3.0.2时,出现此错误:

 ❯npm install vue-template-compiler@3.0.2npm ERR!代码ETARGETnpm ERR!notarget找不到vue-template-compiler@3.0.2的匹配版本.npm ERR!notarget在大多数情况下,您或您的一个依赖项正在请求npm ERR!notarget不存在的软件包版本.npm ERR!可以在以下位置找到此运行的完整日志:npm ERR!/home/alejo/.npm/_logs/2020-11-17T02_52_46_458Z-debug.log 

我该如何解决这个问题?

解决方案

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

  1. 像这样初始化 package.json :

  {私人":是的,"名称":"vue-3",说明":null,} 

  1. 安装最新版本的vue:

npm i-保存vue @ next vue-loader @ next

  1. 还安装包含 @ vue/compiler-sfc 的开发依赖关系,以取代 vue-template-compiler

  npm i -D @ vue/compiler-sfc css-loader文件加载器mini-css-extract-plugin网址加载器webpack webpack-cli webpack-dev-server 

  • @ vue/compiler-sfc
  • css-loader
  • 文件加载器
  • mini-css-extract-plugin
  • 网址加载器
  • vue-loader
  • webpack
  • 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 = {})=>({模式:env.prod吗?生产":发展",devtool:env.prod吗?源地图":"cheap-module-eval-source-map",入口: [require.resolve(`webpack-dev-server/client`),path.resolve(__ dirname,"./src/main.js")] .filter(Boolean),输出: {路径:path.resolve(__ dirname,"./dist"),publicPath:"/dist/"},解决: {别名:{//这在技术上不是必需的,因为捆绑程序的默认`vue`条目//是来自@ vue/runtime-dom的简单`export *.但是有这个//额外的重新导出会导致webpack始终使模块无效//第一次HMR更新,并导致页面重新加载.vue:"@ vue/runtime-dom"}},模块: {规则:[{测试:/\.vue $/,使用:"vue-loader"},{测试:/\.png $/,用: {加载程序:"url-loader",选项:{限制:8192}}},{测试:/\.css $/,用: [{加载器:MiniCssExtractPlugin.loader,选项:{hmr:!env.prod}},"css-loader"]}]},插件:[新的VueLoaderPlugin(),新的MiniCssExtractPlugin({文件名:"[[name] .css""})],devServer:{内联:是的,热门:是的,统计信息:最小值",contentBase:__dirname,重叠:正确,injectClient:否,disableHostCheck:是}}); 

  1. 添加 dev 脚本以运行您的应用程序:

  {私人":是的,脚本":{"dev":"webpack-dev-server";},依赖项":{"vue":"^ 3.0.2";},"名称":"vue-3",说明":null,"devDependencies":{...}} 

  1. 用以下内容填充 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/

对于准备使用的项目,请尝试克隆此

I am trying to integrate vuejs 3 to an existing project which uses webpack. I read about vue-loader, so I am trying to use it.

In the official documentation I have this:

Every time a new version of vue is released, a corresponding version of vue-template-compiler is released together. The compiler's version must be in sync with the base vue package so that vue-loader produces code that is compatible with the runtime. This means every time you upgrade vue in your project, you should upgrade vue-template-compiler to match it as well.

So, when I try to compile I get this error:

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.

But when I try to install vue-template-compiler@3.0.2 I get this error:

❯ 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

How can I solve this problem?

解决方案

To make vue 3 work fine with webpack without using vite or vue cli follow these steps :

  1. init the package.json like :

{
    "private": true,
    "name": "vue-3",
    "description": null,
   
}

  1. install the last version of vue :

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

  1. install also the dev dependencies that includes @vue/compiler-sfc which replaces 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-loader
  • file-loader
  • mini-css-extract-plugin
  • url-loader
  • vue-loader
  • webpack
  • webpack-cli
  • webpack-dev-server

  1. create or edit your webpack.config.js with following content :

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
  }
});

  1. Add dev script to run your app :

{
    "private": true,
    "scripts": {
        "dev": "webpack-dev-server"
    },
    "dependencies": {
        "vue": "^3.0.2"
    },
    "name": "vue-3",
    "description": null,
    "devDependencies": {
      ...
    }
}

  1. Fill the index.html with following content :

<link rel="stylesheet" href="/dist/main.css" />
<div id="app"></div>
<script src="/dist/main.js"></script>

Finally run npm run dev the visit http://localhost:8080/

for a ready to use project try to clone this REPOSITORY which built by following the steps above.

这篇关于Vuejs 3:vue-template-compiler出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆