咱们将会选择使用一些vue周边的库javascript
1.使用node.js后台,了解到如何获取数据
2.实现单页路由
3.实现HTTP请求咱们的node
4.单项数据流
5.使用.vue文件进行开发css
最终咱们将会构建出一个小demo,不废话,直接上图。html
1.咱们将会使用webpack去为咱们的模块打包,预处理,热加载。若是你对webpack不熟悉,它就是能够帮助咱们把多个js文件打包为1个入口文件,而且能够达到按需加载。这就意味着,咱们不用担忧因为使用太多的组件,致使了过多的HTTP请求,这是很是有益于产品体验的。但咱们并不仅是为了这个而使用webpack,咱们须要用webpack去编译.vue文件,若是没有使用一个loader去转换咱们.vue文件里的style、js和html,那么浏览器就没法识别。vue
2.模块热加载是webpack的一个很是碉堡的特性,将会为咱们的单页应用带来极大的便利。
一般来讲,当咱们修改了代码刷新页面,那应用里的全部状态就都没有了。这对于开发一个单页应用来讲是很是痛苦的,由于须要从新在跑一遍流程。若是有模块热加载,当你修改了代码,你的代码会直接修改,页面并不会刷新,因此状态也会被保留。java
3.Vue也为咱们提供了CSS预处理,因此咱们能够选择在.vue文件里写LESS或者SASS去代替原生CSS。node
4.咱们过去一般须要使用npm下载一堆的依赖,可是如今咱们能够选择Vue-cli。这是一个vue生态系统中一个伟大创举。这意味着咱们不须要手动构建咱们的项目,而它就能够很快地为咱们生成。webpack
首先,安装vue-cli。(确保你有node和npm)git
npm i -g vue-cli
github
而后建立一个webpack项目而且下载依赖web
vue init webpack vue-time-tracker
cd vue-time-tracker
npm i
接着使用 npm run dev
在热加载中运行咱们的应用
这一行命令表明着它会去找到package.json
的scripts
对象,执行node bulid/dev-server.js
。在这文件里,配置了Webpack,会让它去编译项目文件,而且运行服务器,咱们在localhost:8080
便可查看咱们的应用。
这些都准备好后,咱们须要为咱们的路由和XHR请求下载两个库,咱们能够从vue的官网中找到他们。
npm i vue-resource vue-router --save
查看咱们的应用文件,咱们能够在src目录下找到App.vue
和main.js
。在main.js
文件中,咱们引入Vue
和App
,而且建立了一个vue的实例(由于在router这行引入了App组件router.start(App, '#app')
)
// src/main.js import Vue from 'vue' import App from './App.vue' import Hello from './components/Hello.vue' import VueRouter from 'vue-router' import VueResource from 'vue-resource' //注册两个插件 Vue.use(VueResource) Vue.use(VueRouter) const router = new VueRouter() // 路由map router.map({ '/hello': { component: Hello } }) router.redirect({ '*': '/hello' }) router.start(App, '#app')
咱们还须要在index.html
包裹下咱们的<app></app>
//index.html <div id="app"> <app></app> </div>
咱们的初始化就到这结束了,接下来让咱们开始建立别的组件。
首先,咱们须要为咱们的应用增长下bootstrap.css,为了方便,在这就直接在头部引入CDN。
<head> <meta charset="utf-8"> <title>计划板</title> <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> </head>
接着在App.vue里为咱们的应用写个顶部导航。
// src/App.vue <template> <div id="wrapper"> <nav class="navbar navbar-default"> <div class="container"> <a class="navbar-brand" href="#"> <i class="glyphicon glyphicon-time"></i> 计划表 </a> <ul class="nav navbar-nav"> <li><a v-link="'/home'">首页</a></li> <li><a v-link="'/time-entries'">计划列表</a></li> </ul> </div> </nav> <div class="container"> <div class="col-sm-3"> </div> <div class="col-sm-9"> <router-view></router-view> </div> </div> </div> </template>
除了咱们的navbar
之外,咱们还须要一个.container
去放咱们其他须要展现的信息。
而且在这里咱们要放一个router-view
标签,vue-router
的切换就是经过这个标签开始显现的。
接着,咱们须要建立一个Home.vue
做为咱们的首页
// src/components/Home.vue <template> <div class="jumbotron"> <h1>任务追踪</h1> <p> <strong> <a v-link="'/time-entries'">建立一个任务</a> </strong> </p> </div> </template>
既然咱们须要显示Home,那就须要开始配置路由,这很简单,只须要在main.js
里把Hello.vue
换为Home.vue
便可
//... router.map({ '/Home': { component: Home } }) router.redirect({ '*': '/Home' })
在这个页面,咱们须要去建立咱们的时间跟踪列表。
PS:如今这个页面没有数据,以后咱们会在后台配置
// src/components/TimeEntries.vue <template> <div> //`v-if`是vue的一个指令 //`$route.path`是当前路由对象的路径,会被解析为绝对路径当 //`$route.path !== '/time-entries/log-time'`为`true`是显示,`false`,为不显示。 //v-link 路由跳转地址 <button v-if="$route.path !== '/time-entries/log-time'" v-link="'/time-entries/log-time'" class="btn btn-primary"> 建立 </button> <div v-if="$route.path === '/time-entries/log-time'"> <h3>建立</h3> </div> <hr> //下一级视图 <router-view></router-view> <div class="time-entries"> <p v-if="!timeEntries.length"><strong>尚未任何任务</strong></p> <div class="list-group"> //v-for 循环渲染 <a class="list-group-item" v-for="timeEntry in timeEntries"> <div class="row"> <div class="col-sm-2 user-details"> //`:src`属性,这个是vue的属性绑定简写`v-bind`能够缩写为`:` // 好比a标签的`href`能够写为`:href` //而且在vue的指令里就必定不要写插值表达式了(`:src={{xx}}`),vue本身会去解析 <img :src="timeEntry.user.image" class="avatar img-circle img-responsive" /> <p class="text-center"> <strong> {{ timeEntry.user.name }} </strong> </p> </div> <div class="col-sm-2 text-center time-block"> <h3 class="list-group-item-text total-time"> <i class="glyphicon glyphicon-time"></i> {{ timeEntry.totalTime }} </h3> <p class="label label-primary text-center"> <i class="glyphicon glyphicon-calendar"></i> {{ timeEntry.date }} </p> </div> <div class="col-sm-7 comment-section"> <p>{{ timeEntry.comment }}</p> </div> <div class="col-sm-1"> <button class="btn btn-xs btn-danger delete-button" //事件绑定简写 @xxx @click="deleteTimeEntry(timeEntry)"> X </button> </div> </div> </a> </div> </div> </div> </template>
关于template的解释,都写在一块儿了,再看看咱们的script
// src/components/TimeEntries.vue <script> export default { data () { // 事先模拟一个数据 let existingEntry = { user: { name: '二哲', email: 'kodo@forchange.cn', image: 'https://sfault-avatar.b0.upaiyun.com/888/223/888223038-5646dbc28d530_huge256' }, comment: '个人一个备注', totalTime: 1.5, date: '2016-05-01' } return { timeEntries: [existingEntry] } }, methods: { deleteTimeEntry (timeEntry) { //这个方法用于删除某一项计划 let index = this.timeEntries.indexOf(timeEntry) if (window.confirm('肯定要删除吗?')) { this.timeEntries.splice(index, 1) //这里会派发到父组件上,执行父组件events里的deleteTime方法 this.$dispatch('deleteTime', timeEntry) } } }, events: { timeUpdate (timeEntry) { this.timeEntries.push(timeEntry) //继续向上派发 return true } } } </script>
别忘了为咱们的组件写上一些须要的样式
// src/components/TimeEntries.vue <style> .avatar { height: 75px; margin: 0 auto; margin-top: 10px; margin-bottom: 10px; } .user-details { background-color: #f5f5f5; border-right: 1px solid #ddd; margin: -10px 0; } .time-block { padding: 10px; } .comment-section { padding: 20px; } </style>
因为新增了页面,因此咱们继续配置咱们的路由
// src/main.js import TimeEntries from './components/TimeEntries.vue' //... router.map({ '/home': { component: Home }, '/time-entries': { component: TimeEntries } }) //...
这个比较简单咱们直接给出代码
// src/components/LogTime.vue <template> <div class="form-horizontal"> <div class="form-group"> <div class="col-sm-6"> <label>日期</label> <input type="date" class="form-control" v-model="timeEntry.date" placeholder="Date" /> </div> <div class="col-sm-6"> <label>时间</label> <input type="number" class="form-control" v-model="timeEntry.totalTime" placeholder="Hours" /> </div> </div> <div class="form-group"> <div class="col-sm-12"> <label>备注</label> <input type="text" class="form-control" v-model="timeEntry.comment" placeholder="Comment" /> </div> </div> <button class="btn btn-primary" @click="save()">保存</button> <button v-link="'/time-entries'" class="btn btn-danger">取消</button> <hr> </div> </template> <script> export default { data () { return { //模拟一个默认值 timeEntry: { user: { name: '二哲', email: 'kodo@forchange.cn', image: 'https://sfault-avatar.b0.upaiyun.com/888/223/888223038-5646dbc28d530_huge256' } } } }, methods: { save () { let timeEntry = this.timeEntry this.$dispatch('timeUpdate', timeEntry) this.timeEntry = {} } } } </script>
这个组件很简单就3个input输入而已,而后就两个按钮,保存咱们就把数据push进咱们的列表里,而且初始化咱们的timeEntry。取消的话,咱们就路由定位到/time-entries
便可。
ps:按理来讲咱们应该是要填写6个数据包括名字,邮箱和头像。但这里为了演示就暂时先这样。之后结合后台咱们会继续完善这里。
LogTime
属于咱们TimeEntries
组件的一个子路由,因此咱们依旧须要配置下咱们的router.map
// src/main.js import LogTime from './components/LogTime.vue' //... router.map({ '/home': { component: Home }, '/time-entries': { component: TimeEntries, subRoutes: { '/log-time': { component: LogTime } } } }) //...
目前咱们首页左侧还有一块空白,咱们须要它放下一个侧边栏去统计全部计划的总时间。
// src/App.vue //... <div class="container"> <div class="col-sm-3"> <sidebar :time="totalTime"></sidebar> </div> <div class="col-sm-9"> <router-view></router-view> </div> </div> //...
因为咱们把总时间存放在最上级的父组件上,因此咱们须要把咱们的总时间传入咱们的sidebar
组件。
在写下咱们的两个时间计算方法
<script> import Sidebar from './components/Sidebar.vue' export default { components: { 'sidebar': Sidebar }, data () { return { totalTime: 1.5 } }, events: { timeUpdate (timeEntry) { this.totalTime += parseFloat(timeEntry.totalTime) }, deleteTime (timeEntry) { this.totalTime -= parseFloat(timeEntry.totalTime) } } } </script>
最后给出咱们Sidebar.vue
<template> <div class="panel panel-default"> <div class="panel-heading"> <h1 class="text-center">已有时长</h1> </div> <div class="panel-body"> <h1 class="text-center">{{ time }} 小时</h1> </div> </div> </template> <script> export default { props: ['time'] } </script>
props
就是vue中传值的写法,不只要在咱们自定义的标签上传入<sidebar :time="totalTime"></sidebar>
,还须要在组件里js里定义props: ['time']
本章,咱们能够学习到许多关于vue的特性。
1.了解了vue-cli脚手架
2.初步对webpack有了一些了解和认识
3.如何用.vue愉快的开发
4.父子组件通讯
5.路由(子路由)的应用
下一章,咱们将会结合node学习vue-resource,更好的完善咱们SPA应用