1、build:构建脚本目录
1)build.js ==> 生产环境构建脚本;
2)check-versions.js ==> 检查npm,node.js版本;
3)utils.js ==> 构建相关工具方法;
4)vue-loader.conf.js ==> 配置了css加载器以及编译css之后自动添加前缀;
5)webpack.base.conf.js ==> webpack基本配置;
6)webpack.dev.conf.js ==> webpack开发环境配置;
7)webpack.prod.conf.js ==> webpack生产环境配置;
2、config:项目配置
1)dev.env.js ==> 开发环境变量;
2)index.js ==> 项目配置文件;
3)prod.env.js ==> 生产环境变量;
3、node_modules:npm 加载的项目依赖模块
4、src:这里是我们要开发的目录,基本上要做的事情都在这个目录里。里面包含了几个目录及文件:
1)assets:资源目录,放置一些图片或者公共js、公共css。这里的资源会被webpack构建;
2)components:组件目录,我们写的组件就放在这个目录里面;
3)router:前端路由,我们需要配置的路由路径写在index.js里面;
4)App.vue:根组件;
5)main.js:入口js文件;
5、static:静态资源目录,如图片、字体等。不会被webpack构建
6、index.html:首页入口文件,可以添加一些 meta 信息等
7、package.json:npm包配置文件,定义了项目的npm脚本,依赖包等信息
8、README.md:项目的说明文档
9、.xxxx文件:这些是一些配置文件,包括语法配置,git配置等
项目目录结构: 1、在components目录下面写我们的vue组件
1)开始我们的第一个组件:
a:在components目录下新建First.vue
b:在router目录下的index.js里面配置路由路径
import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import MyFirst from '@/components/First' Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld }, { path: '/First', name: 'First', component: MyFirst, } ] })c:First.vue文件内容
<template> <div id="first"> <h1>The first component.</h1> <a> written by {{ author }} </a> </div> </template> <script type="text/javascript"> export default { data () { return { author: "First Person" } } } </script> <style> </style>d:输入ip: http://localhost:8080/#/First,查看页面效果 注意: 一个组件下只能有一个并列的 div,以下写法是错误: 数据要写在 return 里面,而不是像文档那样子写,以下写法错误: 2、讲讲父子组件
1)在components目录下新建sub文件夹,用于存放一下可以复用的子组件。比如新建一个Child.vue组件 2)在父组件中引入子组件 引入:import Confirm from ‘…/sub/Child’ 注册:在标签内的 name代码块后面加上 components: {Child} 使用:在内加上 完整代码:
<template> <div id="first"> <h1>The first component.</h1> <a> {{ msg }} </a> <child childText='text' @message="getMessage"></child>//引入了子组件 </div> </template> <script type="text/javascript"> import Child from './sub/Child' export default { name: 'First', data () { return { msg: "First Person" } }, components: { 'Child': Child }, methods: { getMessage: function (val) { console.log(val) } } } </script> <style> </style>3)父子组件通信 子组件:
<template> <div id="child-button"> <h1>The child component.</h1> <a> {{ msg }} </a> <button @click="clickButton">{{childText || '确认'}} </button>//获取父组件传的值 </div> </template> <script type="text/javascript"> export default { name: 'Child', props: ['childText'],//父组件传给子组件的值 data () { return { msg: "Children" } }, methods: { clickButton: function () { this.$emit('message', this.msg) } } } </script> <style> </style>3、使用路由搭建单页应用 1)按照以上方法,新建一个Second.vue组件
<template> <div id="second"> <h1>The second component.</h1> <a> written by {{ author }} </a> </div> </template> <script type="text/javascript"> export default { data () { return { author: "Second Person" } } } </script> <style> </style>2)路由跳转:去第二个页面 注意: 在router目录下的index.js里面需要注册Second路由
1、解决vue不能自动打开浏览器的问题:当我们输入npm run dev,运行项目,命令行提示我们运行成功,但是浏览器也没有自动打开,只能自己手动输入。 解决: 1)打开config ==> index.js 2)module.exports配置中找到autoOpenBrowser,默认设置的是false
module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: {}, // Various Dev Server settings host: 'localhost', // can be overwritten by process.env.HOST port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined autoOpenBrowser: false, errorOverlay: true,3)将autoOpenBrowser改为true
module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: {}, // Various Dev Server settings host: 'localhost', // can be overwritten by process.env.HOST port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined autoOpenBrowser: true,4)Ctrl+C,然后我们重启一下,就能自动打开浏览器了