10.webpack中使用vue

初始化npm。

npm init -y

npm安装vue。

npm install vue --save-dev

npm安装vue相关编译器

npm i vue-loader vue-template-compiler --save-dev

npm安装babel

npm install @babel/cli @babel/core @babel/preset-env @babel/preset-react babel-loader --save-dev

npm安装webpack。

npm install webpack webpack-cli webpack-dev-server --save-dev

npm安装webpack插件:

npm install html-webpack-plugin open-browser-webpack-plugin --save-dev

npm安装CSS、Sass

npm install sass-loader css-loader style-loader --save-dev

查看安装地依赖:

npm list --depth 0

相关文件

webpack配置webpack.config.js:

const path = require('path')
const HtmlWebPackPlugin = require('html-webpack-plugin')
const OpenBrowserPlugin = require('open-browser-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')

module.exports = {
	entry: './index.js',
	output: {
		path: path.join(__dirname, 'dist'),
		filename: 'bundle.js'
	},
	module: {
		rules: [
			{
				test: /\.jsx?/,
				// 排除 node_modules 目录下的文件,防止被打抱
				exclude: path.resolve(__dirname, 'node_modules'),
				use: {
					loader: 'babel-loader',
					options: {
						babelrc: false,
						presets: [
							require.resolve('@babel/preset-react'),
							[require.resolve('@babel/preset-env', {module: false})],
						],
					}
				}
			},
			{
				test: /\.vue$/,
				use: ['vue-loader'],
			},
		]
	},
	plugins: [
		new VueLoaderPlugin(),
		new HtmlWebPackPlugin(),
		new OpenBrowserPlugin({
			url: 'http://localhost:8080'
		}),
		new webpack.HotModuleReplacementPlugin(),
	],
	devServer: {
		hot: true
	}
};

app.vue

<template>
	<div>{{message}}</div>
	<button v-on:click='hi'>hi</button>
</template>
<script type="text/javascript">
	export default {
		data() {
			return {
				message: 'hello vue'
			}
		},
		methods: {
			hi(){
				alert('hi');
			}
		},
		beforeCreate: function(){
			console.log('创建之前');
		},
		created: function(){
			console.log('创建');
		},
		beforeMount: function(){
			console.log('挂载之前');
		},
		mounted: function(){
			console.log('挂载');
		},
	}
</script>

测试js

import Vue from 'vue'
import app from './app.vue'

const root = document.createElement('div');
document.body.appendChild(root)

Vue.config.productionTip = false;
new Vue({
    render: (h) => h(app)
}).$mount(root);

测试页面index.html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>webpack</title>
</head>
<body>
	<script type="text/javascript" src="dist/bundle.js"></script>
</body>
</html>

文件目录

编译

webpack

webpack-dev-server服务器上部署

webpack-dev-server

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值