webpack(7)webpack使用vue配置

前言

若是咱们想在webpack中使用vue,就须要在webpack中配置vue
 php

配置vue

首先,咱们须要在项目中安装vue,安装命令以下:css

npm install vue --save

安装完成后,咱们在主入口main.js文件中导入vue并建立一个实例html

import Vue from 'vue'
const app = new Vue({
  el: "#app",
  data: {
    message: "hello"
  }
})

最后咱们在index.html中,写入模板代码以下:vue

<div id="app">
  <h2>{{message}}</h2>
</div>

修改完成后咱们从新运行命令打包npm run build,可是运行程序,在页面上没有出现想要的效果,且控制台里报错以下webpack

vue.runtime.esm.js:623 [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

这个错误的意思是说咱们使用的runtime-only的版本Vue,是不能包含模板代码的,而咱们正好使用了模板代码,因此报错web

解决方案
解决办法就是在webpack中配置如下内容npm

const path = require('path')

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: "dist/"
  },
  // 新增的vue配置,给vue取别名
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
    }
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.png/,
        type: 'asset'
      },
    ],
  },
}

配置完成以后,咱们在访问首页,就能正常显示message中的信息了app

相关文章
相关标签/搜索