第一步:
npm init -y
第二步:
npm install --save-dev vue
第三步:
npm i --save-dev vue-loader vue-template-compiler
第四步:
npm install webpack webpack-cli --save-dev
添加配置文件:webpack.config.js
const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
module.exports = {
entry: './src/main.js',
output:{
path: path.resolve(__dirname,'dist'),
filename :'bundle.js'
},
plugins:[
new VueLoaderPlugin()
],
module:{
rules:[
{ test: /\.vue$/, use:'vue-loader'}
]
},
resolve:{
alias:{
"vue$":"vue/dist/vue.js"
}
}
}
package.json文件如下:
{
"name": "webpack_vue",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"vue": "^2.6.11",
"vue-loader": "^15.8.3",
"vue-template-compiler": "^2.6.11",
"webpack": "^4.41.4",
"webpack-cli": "^3.3.10"
}
}
编写一个index.html文件如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<p>{{msg}}</p>
<login></login>
</div>
<script src="../dist/bundle.js"></script>
</body>
</html>
编写一个main.js
import Vue from 'vue'
import login from './login.vue'
var vm = new Vue({
el: '#app',
data: {
msg: '123'
},
components:{
login
}
})
再写一个vue的组件 login.vue
<template>
<h1>helloword</h1>
</template>
效果如下: