Vue-VueRouter-Webpack 项目教程
1. 项目的目录结构及介绍
vue-vueRouter-webpack/
├── build/ # Webpack 配置文件目录
│ ├── webpack.base.conf.js # 基础配置文件
│ ├── webpack.dev.conf.js # 开发环境配置文件
│ └── webpack.prod.conf.js # 生产环境配置文件
├── config/ # 项目配置目录
│ ├── dev.env.js # 开发环境变量
│ ├── index.js # 主要配置文件
│ └── prod.env.js # 生产环境变量
├── src/ # 源代码目录
│ ├── assets/ # 静态资源目录
│ ├── components/ # Vue 组件目录
│ ├── router/ # Vue Router 配置目录
│ ├── App.vue # 根组件
│ └── main.js # 入口文件
├── static/ # 静态文件目录
├── .babelrc # Babel 配置文件
├── .editorconfig # 编辑器配置文件
├── .gitignore # Git 忽略文件配置
├── index.html # 入口 HTML 文件
├── package.json # 项目依赖和脚本配置
└── README.md # 项目说明文档
2. 项目的启动文件介绍
main.js
main.js
是项目的入口文件,主要负责初始化 Vue 实例并引入必要的模块和配置。以下是 main.js
的基本结构:
import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
import Vue from 'vue'
:引入 Vue 库。import App from './App'
:引入根组件App.vue
。import router from './router'
:引入 Vue Router 配置。Vue.config.productionTip = false
:关闭生产环境提示。new Vue({...})
:创建 Vue 实例,挂载到#app
元素上。
3. 项目的配置文件介绍
config/index.js
config/index.js
是项目的主要配置文件,定义了开发和生产环境的配置参数。以下是 config/index.js
的基本结构:
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
build
对象:定义了生产环境的配置,如输出目录、资源路径、Gzip 压缩等。dev
对象:定义了开发环境的配置,如端口号、自动打开浏览器、代理设置
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考