JavaScript的MVVM库Vue.js入门学习笔记__第1页
JavaScript的MVVM库Vue.js入门学习笔记__第2页
JavaScript的MVVM库Vue.js入门学习笔记__第3页
JavaScript的MVVM库Vue.js入门学习笔记__第4页
JavaScript的MVVM库Vue.js入门学习笔记__第5页
免费预览已结束,剩余21页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JavaScript的MVVM库Vue.js入门学习笔记_ 这篇文章主要介绍了JavaScript的MVVM库Vue.js入门学习笔记,Vue.js是一个新兴的js库,主要用于实现响应的数据绑定和组合的视图组件,需要的伴侣可以参考下 一、v-bind 缩写 !- 完整语法 - a v-bind:href=url/a !- 缩写 - a :href=url/a !- 完整语法 - button v-bind:disabled=someDynamicConditionButton/button !- 缩写 - button :disabled=someDynamicConditionButton/

2、button 二、v-on 缩写 !- 完整语法 - a v-on:click=doSomething/a !- 缩写 - a click=doSomething/a 三、过滤器 message | capitalize 四、条件渲染 v-if h1 v-if=okYes/h1 h1 v-elseNo/h1 div v-if=Math.random() 0.5 Sorry /div div v-else Not sorry /div template-v-if template v-if=ok h1Title/h1 pParagraph 1/p pParagraph 2/p /template

3、 v-show h1 v-show=okHello!/h1 五、列表渲染 for v-for ul id=example-1 li v-for=item in items item.message /li /ul var example1 = new Vue( el: #example-1, data: items: message: Foo , message: Bar ); ul id=example-2 li v-for=item in items parentMessage - $index - item.message /li /ul var example2 = new Vue(

4、el: #example-2, data: parentMessage: Parent, items: message: Foo , message: Bar ); 数组变动检测 Vue.js 包装了被观看数组的变异方法,故它们能触发视图更新。被包装的方法有:push(), pop(), shift(), unshift(), splice(), sort(), reverse() example1.items.push( message: Baz ); example1.items = example1.items.filter(function (item) return item.mes

5、sage.match(/Foo/); ); template-v-for ul template v-for=item in items li item.msg /li li class=divider/li /template /ul 对象 v-for ul id=repeat-object class=demo li v-for=value in object $key : value /li /ul new Vue( el: #repeat-object, data: object: FirstName: John, LastName: Doe, Age: 30 ); 值域 v-for

6、div span v-for=n in 10 n /span /div 六、方法与大事处理器 方法处理器 div id=example button v-on:click=greetGreet/button /div var vm = new Vue( el: #example, data: name: Vue.js , / 在 methods 对象中定义方法 methods: greet: function (event) / 方法内 this 指向 vm alert(Hello + + !) / event 是原生 DOM 大事 alert(event.target.t

7、agName) ) / 也可以在 JavaScript 代码中调用方法 vm.greet(); / - Hello Vue.js! 内联语句处理器 div id=example-2 button v-on:click=say(hi)Say Hi/button button v-on:click=say(what)Say What/button /div new Vue( el: #example-2, methods: say: function (msg) alert(msg) ); 有时也需要在内联语句处理器中访问原生 DOM 大事。可以用特别变量 $event 把它传入方法 button

8、 v-on:click=say(hello!, $event)Submit/button methods: say: function (msg, event) / 现在我们可以访问原生大事对象 event.preventDefault() ; # 大事修饰符 !- 阻挡单击大事冒泡 - a v-on:click.stop=doThis/a !- 提交大事不再重载页面 - form v-on:submit.prevent=onSubmit/form !- 修饰符可以串联 - a v-on:click.stop.prevent=doThat !- 只有修饰符 - form v-on:submit

9、.prevent/form # 按键修饰符 !- 只有在 keyCode 是 13 时调用 vm.submit() - input v-on:keyup.13=submit !- 同上 - input v-on:keyup.enter=submit !- 缩写语法 - input keyup.enter=submit 全部的按键别名:enter,tab,delete,esc,space,up,down,left,right # 其他实例 new Vue( el: #demo, data: newLabel: , stats: stats , methods: add: function (e)

10、 e.preventDefault() if (!this.newLabel) return; this.stats.push( label: this.newLabel, value: 100 ); this.newLabel = ; , remove: function (stat) if (this.stats.length 3) this.stats.$remove(stat); / 留意这里的$remove else alert(Cant delete more!) ); 七、过渡 CSS 过渡 div v-if=show transition=expandhello/div 然后为

11、 .expand-transition, .expand-enter 和 .expand-leave 添加 CSS 规章: /* 必需 */ .expand-transition transition: all .3s ease; height: 30px; padding: 10px; background-color: #eee; overflow: hidden; /* .expand-enter 定义进入的开头状态 */ /* .expand-leave 定义离开的结束状态 */ .expand-enter, .expand-leave height: 0; padding: 0 10

12、px; opacity: 0; 你可以在同一元素上通过动态绑定实现不同的过渡: div v-if=show :transition=transitionNamehello/div new Vue( el: ., data: show: false, transitionName: fade 另外,可以供应 JavaScript 钩子: Vue.transition(expand, beforeEnter: function (el) el.textContent = beforeEnter , enter: function (el) el.textContent = enter , afte

13、rEnter: function (el) el.textContent = afterEnter , enterCancelled: function (el) / handle cancellation , beforeLeave: function (el) el.textContent = beforeLeave , leave: function (el) el.textContent = leave , afterLeave: function (el) el.textContent = afterLeave , leaveCancelled: function (el) / ha

14、ndle cancellation ); 八、组件 1.注册 / 定义 var MyComponent = Vue.extend( template: divA custom component!/div ); / 注册 Vponent(my-component, MyComponent); / 创建根实例 new Vue( el: #example ); div id=example my-component/my-component /div 或者挺直写成: Vponent(my-component, template: divA custom component!/div ); / 创建

15、根实例 new Vue( el: #example ); div id=example my-component/my-component /div 2.用法prop 传递数据 实例一: Vponent(child, / 声明 props props: msg, / prop 可以用在模板内 / 可以用 this.msg 设置 template: span msg /span ); child msg=hello!/child 实例二: Vponent(child, / camelCase in JavaScript props: myMessage, template: span myMes

16、sage /span ); !- kebab-case in HTML - child my-message=hello!/child 3.动态props div input v-model=parentMsg br child v-bind:my-message=parentMsg/child /div 用法 v-bind 的缩写语法通常更简洁: child :my-message=parentMsg/child 4.Prop 绑定类型 prop 默认是单向绑定:当父组件的属性改变时,将传导给子组件,但是反过来不会。这是为了防止子组件无意修改了父组件的状态这会让应用的数据流难以理解。不过,也

17、可以用法 .sync 或 .once 绑定修饰符显式地强制双向或单次绑定: 比较语法: !- 默认为单向绑定 - child :msg=parentMsg/child !- 双向绑定 - child :msg.sync=parentMsg/child !- 单次绑定 - child :msg.once=parentMsg/child 其他实例: modal :show.sync=showModal h3 slot=headercustom header/h3 /modal /div 5.Prop 验证 组件可以为 props 指定验证要求。当组件给其他人用法时这很有用,由于这些验证要求构成了组

18、件的 API,确保其他人正确地用法组件。此时 props 的值是一个对象,包含验证要求: Vponent(example, props: / 基础类型检测 (null 意思是任何类型都可以) propA: Number, / 必需且是字符串 propB: type: String, required: true , / 数字,有默认值 propC: type: Number, default: 100 , / 对象/数组的默认值应当由一个函数返回 propD: type: Object, default: function () return msg: hello , / 指定这个 prop

19、为双向绑定 / 假如绑定类型不对将抛出一条警告 propE: twoWay: true , / 自定义验证函数 propF: validator: function (value) return value 10 , / 转换函数(1.0.12 新增) / 在设置值之前转换值 propG: coerce: function (val) return val + / 将值转换为字符串 , propH: coerce: function (val) return JSON.parse(val) / 将 JSON 字符串转换为对象 ); 其他实例: Vponent(modal, template:

20、#modal-template, props: show: type: Boolean, required: true, twoWay: true ); 6.注册 / 定义 var MyComponent = Vue.extend( template: divA custom component!/div ); / 注册 Vponent(my-component, MyComponent); / 创建根实例 new Vue( el: #example ); div id=example my-component/my-component /div 或者挺直写成: Vponent(my-comp

21、onent, template: divA custom component!/div ); / 创建根实例 new Vue( el: #example ); div id=example my-component/my-component /div 7.用法prop 传递数据 实例一: Vponent(child, / 声明 props props: msg, / prop 可以用在模板内 / 可以用 this.msg 设置 template: span msg /span ); child msg=hello!/child 实例二: Vponent(child, / camelCase i

22、n JavaScript props: myMessage, template: span myMessage /span ); !- kebab-case in HTML - child my-message=hello!/child 8.动态props div input v-model=parentMsg br child v-bind:my-message=parentMsg/child /div 用法 v-bind 的缩写语法通常更简洁: ? 1 child :my-message=parentMsg/child 9.Prop 绑定类型 prop 默认是单向绑定:当父组件的属性改变时

23、,将传导给子组件,但是反过来不会。这是为了防止子组件无意修改了父组件的状态这会让应用的数据流难以理解。不过,也可以用法 .sync 或 .once 绑定修饰符显式地强制双向或单次绑定: 比较语法: !- 默认为单向绑定 - child :msg=parentMsg/child !- 双向绑定 - child :msg.sync=parentMsg/child !- 单次绑定 - child :msg.once=parentMsg/child 其他实例: modal :show.sync=showModal h3 slot=headercustom header/h3 /modal /div 10.Prop 验证 组件可以为 props 指定验证要求。当组件给其他人用法时这很有用,由于这些验证要求构成了组件的 API,确保其他人正确地用法组件。此时 props 的值是一个对象,包含验证要求: Vponent(example, props: / 基础类型检测 (null 意思是任何类型都可以) propA: Number, / 必需且是字符串 propB: type: String, required: true , / 数字,有默认值 propC: type: Number, default: 100 , / 对象/数组的默认值应当由一个函数返回 p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论