<div>
<h1>这是首页</h1>
</div>
</template>
<script>
export default {}
</script>
<style>
h1 {
text-align: center;
}
</style>
两种写样式的方式可以根据具体需求选择使用
提取样式文件
上面引入 css 的方式最终打包之后 CSS 代码都在 js 里面,为了网站的性能需要将 CSS 单独提取出来,使用 mini-css-extract-plugin 插件来提取 CSS
执行 npm install --save-dev mini-css-extract-plugin
webpack.config.js
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
// ...
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader // 代替 style-loader
},
{
loader: 'css-loader'
}
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: `[name].css`
})
]
}
处理图片
项目中如果有用到图片需要 file-loader 来处理
执行 npm install --save-dev file-loader
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader'
}
]
}
}
准备一张图片 logo.gif 放在 src/images 目录中(images 目录需要新建,这张图片是用来测试的)
project
project-name
|- .babelrc
|- index.html
|- index.js
|- package.json
|- style.css
|- webpack.config.js
|- /src
+ |- /images
+ |- logo.gif
|- /js
|- routes.js
|- /pages
|- about.vue
|- app.vue
|- index.vue
index.vue
<template>
<div>
<h1>这是首页</h1>
<img src="@/images/logo.gif">
</div>
</template>
<script>
export default {}
</script>
<style>
h1 {
text-align: center;
}
</style>
执行 npm run build
打包后发现图片已经成功打包进来了,但是图片的名称改变了,如果不希望改变图片名称,可以给 file-loader 配置参数
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
name: 'images/[name].[ext]'
}
}
]
}
}
压缩 CSS
使用 cssnano 压缩 CSS,该插件属于 PostCSS 生态系统,所以需要同时安装 postcss-loader
执行 npm install --save-dev cssnano postcss-loader
创建一个 postcss.config.js 文件,这是 PostCSS 的配置文件,相关配置都写在这里面
project
project-name
|- .babelrc
|- index.html
|- index.js
|- package.json
+ |- postcss.config.js
|- style.css
|- webpack.config.js
...
postcss.config.js
module.exports = {
plugins: {
'cssnano': {
safe: true
}
}
}
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: 'css-loader'
},
{
loader: 'postcss-loader'
}
]
}
]
}
}
CSS 预处理
这里使用 postcss-preset-env 来预处理 CSS(也可以选择使用 Sass 或者 Less 等)
执行 npm install --save-dev postcss-preset-env
该插件也属于 PostCSS 生态系统,直接在 postcss.config.js 里增加配置即可
postcss.config.js
module.exports = {
plugins: {
+ 'postcss-preset-env': {},
'cssnano': {
+ autoprefixer: false, // 这里两个插件都包含了 autoprefixer,只执行其中一个就行
safe: true
}
}
}
HTTP 请求
使用 Axios 发送 HTTP 请求,Axios 基于 Promise,所以同时安装 es6-promise polyfill
执行 npm install --save axios es6-promise
index.js
+ import 'es6-promise/auto'
+ import axios from 'axios'
// ...
在项目中发送一个请求
index.js
import 'es6-promise/auto'
import axios from 'axios'
+ axios.post('/login')
// ...
运行后这个请求明显会返回一个 404,那么如何让它返回有效的数据呢,在 webpack.config.js 里配置 devServer
参数
webpack.config.js
module.exports = {
// ...
devServer: {
+ before(app, server) {
+ app.post('/login', (req, res) => {
+ res.json({success: true})
+ })
+ },
compress: true,
port: 8080
}
}
重新启动后,就可以看到请求 /login 地址返回了数据 {"success": true}
,这样就可以在本地调试接口了
当然,所有接口都这样写未免麻烦,可以用 proxy
参数将请求接口代理到其它地址去
webpack.config.js
module