我正在try 将vuejs 3集成到一个使用webpack的现有项目中.我读过关于vue loader的文章,所以我正在try 使用它.

在官方文件中,我有:

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.

所以,当我试图编译时,我得到了以下错误:

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.

但当我try 安装vue模板时-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的情况下与网页包配合使用,请执行以下步骤:

  1. 比如说:
{
    "private": true,
    "name": "vue-3",
    "description": null,
   
}
  1. 安装vue的最新版本:

npm i --save vue@next vue装载机@next

  1. 还可以安装dev依赖项,其中包括@vue/编译器sfc个,取代了vue-template-compiler
npm i -D @vue/编译器sfc css加载器 文件加载器 迷你css提取插件
 url加载器 网页包 网页包-cli 网页包-dev-server
  • @vue/编译器sfc
  • css加载器
  • 文件加载器
  • 迷你css提取插件
  • url加载器
  • vue装载机
  • 网页包
  • 网页包-cli
  • 网页包-dev-server
  1. create or edit your 网页包.config.js with following content :
const path = require("path");
const { VueLoaderPlugin } = require("vue装载机");
const MiniCssExtractPlugin = require("迷你css提取插件");

module.exports = (env = {}) => ({
  mode: env.prod ? "production" : "development",
  devtool: env.prod ? "source-map" : "cheap-module-eval-source-map",
  entry: [
    require.resolve(`网页包-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 网页包 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装载机"
      },
      {
        test: /\.png$/,
        use: {
          loader: "url加载器",
          options: { limit: 8192 }
        }
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: { hmr: !env.prod }
          },
          "css加载器"
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new MiniCssExtractPlugin({
      filename: "[name].css"
    })
  ],
  devServer: {
    inline: true,
    hot: true,
    stats: "minimal",
    contentBase: __dirname,
    overlay: true,
    injectClient: false,
    disableHostCheck: true
  }
});

  1. 添加dev个脚本以运行应用程序:
{
    "private": true,
    "scripts": {
        "dev": "网页包-dev-server"
    },
    "dependencies": {
        "vue": "^3.0.2"
    },
    "name": "vue-3",
    "description": null,
    "devDependencies": {
      ...
    }
}

  1. 用以下内容填写index.html:
<link rel="stylesheet" href="/dist/main.css" />
<div id="app"></div>
<script src="/dist/main.js"></script>

Finally运行npm run dev次访问http://localhost:8080/

对于一个随时可用的项目,请try 克隆这个100,它是按照上述步骤构建的.

Edit 网页包-vue3

Vue.js相关问答推荐

虚拟DOM在Vue中姗姗来迟

Vue ChildComponent (GrandchildComponent) 应该更新 ParentComponent 的列表

在预渲染模式下部署 nuxt 3 时出错

Vuetify右侧并排switch

Nuxt 2.15.7 的构建错误 - 无法解析 CSS @font-face URL

如何在 vuejs 自定义指令中传递动态参数

在 VueJS 中存储图像的正确位置是什么 - 公共文件夹或assets文件夹?

Vuetify v-data-table ,如何在 HTML 中呈现标题文本?

VueJS:向左,元素的顶部位置?

在没有组件的vue js中拖放

Vue.js + Vuex:如何改变嵌套项状态?

如何在 vuejs 中异步加载图像 src url?

VueJS 组件中的图像未加载

如何有条件地更改 vue/vuetify 文本字段 colored颜色

如何判断 Vue 组件是否处于活动状态

交换数组项

Vue.js 的 $emit 和 $dispatch 有什么区别?

从 vuex 存储访问 $vuetify 实例属性

$emit 不会触发子事件

如何在 Vue.js 2 应用程序中模拟 onbeforeunload?