vue是一套构建用户界面的渐进式框架。css
他的特色:html
简洁:页面由HTML模板+Json数据+Vue实例组成vue
数据驱动:自动计算属性和追踪依赖的模板表达式node
组件化:用可复用、解耦的组件来构造页面webpack
轻量:代码量小,不依赖其余库git
快速:精确有效批量DOM更新es6
模板友好:多种方式安装,很容易融入github
所须要储备的技术
扎势的
html,css,JavaScript
基础知识 火狐中文社区 https://developer.mozilla.org...web
es6
相关知识 ECMAScript 6 入门 http://es6.ruanyifeng.com/#RE...vue-router
nodejs
相关知识 npm 基本用法和实用技巧 https://github.com/theicebear...
webpack
相关知识 webpack中文社区 https://doc.webpack-china.org/能够简单的使用命令终端
使用
vue-cli
建立项目
1.
node
环境安装(更新到最新
)
2.
vue-cli
脚手架安装2.1
npm install vue-cli -g
3. 建立项目
3.1
vue init webpack
项目名称
4. 进入该目录
4.1
cd 项目名称
5. 安装依赖包
5.1
npm install(或者简写 i )
6. 启动项目
npm run dev
数据绑定最多见的形式
使用 “Mustache”
语法(双大括号
)的文本插值: {{msg}}
使用 v-bind
在 HTML 属性中使用: <div v-bind.id="msg"><div>
<span v-text="msg"></span>
等同于 <span>{{msg}}</span>
注意:内容按普通 HTML 插入 - 不会做为 Vue 模板进行编译
。<div v-html="msg"></div>
<div v-if></div> <div v-else-if></div> <div v-else-if></div> <div v-else></div>`
<div v-for="(val, key, index) in data"></div>
数组更新检测的方法有
push()
pop()
shift()
unshift()
splice()
sort()
reverse()
重塑数组方法
filter()
concat()
slice()
注意事项因为 JavaScript 的限制, Vue 不能检测如下变更的数组:
当你利用索引直接设置一个项时,例如:
vm.items[indexOfItem] = newValue
解决办法Vue.set(example1.items, indexOfItem, newValue)
当你修改数组的长度时,例如:
vm.items.length = newLength
解决办法example1.items.splice(newLength)
@
监听当前实例上的自定义事件。事件能够由vm.$emit触发。回调函数会接收全部传入事件触发函数的额外参数。
vm.$on('test', function(msg) { console.log(msg) }) vm.$emit('test', 'hi')修饰符:
.stop
- 调用 event.stopPropagation()
.prevent
- 调用 event.preventDefault()。
.capture
- 添加事件侦听器时使用 capture 模式。
.self
- 只当事件是从侦听器绑定的元素自己触发时才触发回调。
.{keyCode | keyAlias}
- 只当事件是从侦听器绑定的元素自己触发时才触发回调。
.native
- 监听组件根元素的原生事件。
:
<!-- 绑定一个属性 --> <img v-bind:src="src"> <!-- 缩写 --> <img :src="src">
限制使用在
<input>
<select>
<textarea>
components
可以使用的修饰符
.lazy
.number
.trim
配合 CSS 规则使用 如 [v-cloak] { display: none }
一次
。2.1 全局自定义指令
Vue.directive('focus', { // 当绑定元素插入到 DOM 中。 inserted: function(el) { // 聚焦元素 el.focus() } })2.2 局部自定义指令
组件
是可扩展 HTML 元素,封装可重用的代码
。
注意:
要确保在初始化根实例 以前 注册了组件
Vue.component('my-component', { })
var Child = { template: '<div>A custom component!</div>' } new Vue({ // ... components: { // <my-component> 将只在父模板可用 'my-component': Child } })
这些元素 <ul> , <ol>, <table> , <select> 限制了能被它包裹的元素, <option> 只能出如今其它元素内部。
组件实例的做用域是孤立的。
子组件要显式地用 props 选项声明它期待得到的数据: Vue.component('child', { // 声明 props props: ['message'], // 就像 data 同样,prop 能够用在模板内 // 一样也能够在 vm 实例中像 “this.message” 这样使用 template: '<span>{{ message }}</span>' }) 向父组件传入一个普通字符串: <child message="hello!"></child>当使用的
不是字符串模版
,camelCased (驼峰式)
命名的prop
须要转换为相对应
的kebab-case (短横线隔开式)
命名:Vue.component('child', { // camelCase in JavaScript props: ['myMessage'], template: '<span>{{ myMessage }}</span>' }) <!-- kebab-case in HTML --> <child my-message="hello!"></child>
若是你使用字符串模版,则没有这些限制。
使用
$on(eventName)
监听事件使用
$emit(eventName)
触发事件
<div id="counter-event-example"> <p>{{ total }}</p> <button-counter v-on:increment="incrementTotal"></button-counter> <button-counter v-on:increment="incrementTotal"></button-counter> </div> Vue.component('button-counter', { template: '<button v-on:click="increment">{{ counter }}</button>', data: function () { return { counter: 0 } }, methods: { increment: function () { this.counter += 1 this.$emit('increment') } }, }) new Vue({ el: '#counter-event-example', data: { total: 0 }, methods: { incrementTotal: function () { this.total += 1 } } })
简单的场景下,可使用一个空的 Vue 实例做为中央事件总线:
复杂的状况下,咱们应该考虑使用专门的 状态管理模式(vuex).
无名 Slot
子组件 my-component 模板: <div> <h2>我是子组件的标题</h2> <slot></slot> </div> 父组件模版: <div> <h1>我是父组件的标题</h1> <my-component></my-component> </div> 渲染结果: <div> <h1>我是父组件的标题</h1> <div> <h2>我是子组件的标题</h2> </div> </div>有名
npm install vue-router
### main.js ### import router from './router' new Vue({ el: '#app', router, template: '<App/>', components: { App } }) ### router.js ### import VueRouter from 'vue-router' Vue.use(VueRouter) export default new Router({ routes: [ { path: '/', name: 'foo', component: foo } })