vue学习指南3
来源:互联网 发布:淘宝卖家参加聚划算 编辑:程序博客网 时间:2024/06/05 02:18
2.9 组件
组件可以扩展 HTML 元素,封装可重用的代码。在较高层面上,组件是自定义元素;通过Vue.component()接口将大型应用拆分为各个组件,从而使代码更好具有维护性、复用性以及可读性。
注册组件
<div id="app" > <my-component></my-component> </div>Vue.component('my-component',{template:'<div>my-component</div>'})var app=new Vue({ el:'#app', data:{ }})
解读:
- 注册行为必须在创建实例之前;
- component的template接口定义组件的html元素;
局部注册组件
<div id="app" > <my-component> <heading></heading> </my-component> </div>Vue.component('my-component',{template:'<div>my-first-component</div>'});var Child={ template:'<h3>hello world</h3>'};var app=new Vue({ el:'#app', components:{ 'my-component':Child }});
解读:
- 可以定义一个子组件,在实例的components接口中将子组件挂载到父组件上,子组件只在父组件的作用域下有效;
特殊DOM模板将会限制组件的渲染
像这些包含固定样式的元素
<ul><ol><table><select>
自定义组件中使用这些受限制的元素时会导致渲染失败;
通的方案是使用特殊的 is属性:
<table> <tr is="my-component"></table>
创建组件的data对象必须是函数
<div id="app"> <counter></counter> <counter></counter> <counter></counter> </div>Vue.component('counter',{ template:'<button @click="count+=1">{{count}}</button>', data:function(){ return { count: 0 }; }});var app=new Vue({ el:'#app', data:{ }});
解读:
- 在组件当中定义的数据count必须以函数的形式返回;
使用Props实现父组件向子组件传递数据
<div id="app"> <child some-text='hello'></child> <br> <child v-bind:some-text='message'> </child> </div>Vue.component('child', {template:'<div>{{someText}} </div>',props:['someText'] });var app=new Vue({ el:'#app', components:{ 'my-component':Child }, data:{ message:'你好' }});
解读:
组件实例的作用域是孤立的。这意味着不能并且不应该在子组件的模板内直接引用父组件的数据。可以使用 props 把数据传给子组件;
可以用 v-bind动态绑定 props 的值到父组件的数据中。每当父组件的数据变化时,该变化也会传导给子组件,注意这种绑定方式是单向绑定;
父组件是使用 props 传递数据给子组件,但如果子组件要把数据传递回去则使用自定义事件!
<div id="app"> <p>{{total}}</p> <button-counter v-on:increment='incrementTotal'></button-counter> <button-counter @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') } }})var app = new Vue({ el:'#app', data:{ total:0 }, methods:{ incrementTotal:function(){ this.total += 1; } }})
解读:
- 父组件可以通过监听子组件的自定义事件,从而改变父组件的数据;
- 子组件每点击一次,触发increment函数,该函数在执行过程中通过$emit('increment')发出increment事件;
- button控件同时监听increment事件,每次发出该事件就改变父组件的total值;
使用Slots分发内容
<div id="app"> <h1>I'm the parent title</h1> <my-component> <p>This is some original content</p> <p>This is some more original content</p> </my-component> <hr> </div>Vue.component('my-component',{ template:"<div><h2>sssss</h2><slot>如果没有分发内容则显示我。</slot></div>"});var app = new Vue({ el:'#app', data:{ }});
解读:
- 如果子组件模板一个<slot>都不包含,则父组件内容将会被丢弃;
- 当子组件模板只有一个没有属性的 slot 时,父组件整个内容片段将插入到 slot 所在的 DOM 位置,并替换掉 slot 标签本身;
- 只有在宿主元素为空,且没有要插入的内容时才显示备用内容;
//子组件app-layout模板<div class="container"><header><slot name="header"></slot></header><main><slot></slot></main><footer><slot name="footer"></slot></footer></div>//父组件模板<app-layout><h1 slot="header">Here might be a page title</h1><p>A paragraph for the main content.</p><p>And another one.</p><p slot="footer">Here's some contact info</p></app-layout>//渲染结果<div class="container"><header><h1>Here might be a page title</h1></header><main><p>A paragraph for the main content.</p><p>And another one.</p></main><footer><p>Here's some contact info</p></footer></div>
解读:
- 具名slot相当于给slot设置标识符,只要在父组件的元素上设置<div slot="name"></div>就可以把该元素插入子组件定义的模板;
2.10 vue-resource插件
使用vue-rescource实现前后端的通信
在vue实例中新增ready对象,当页面完成加载时发出请求
new Vue({ el: '#app', ready: function() { this.$http.get('book.json', function(data) { this.$set('books', data); }).error(function(data, status, request) { console.log('fail' + status + "," + request); }) //post方法:this.$http.post(url,postdata,function callback) }, data: { .... books:'' },
解读:
- 这个$http请求和jquery的ajax还是有点区别,这里的post的data默认不是以form data的形式,而是request payload。解决起来也很简单:在vue实例中添加headers字段:
- http: { headers: {'Content-Type': 'application/x-www-form-urlencoded'} }
阅读全文
0 0
- vue学习指南3
- vue学习指南
- vue学习指南
- vue学习指南1
- vue学习指南2
- Spring MVC学习指南3
- 学习指南
- 学习指南
- 学习指南
- javascript小白学习指南3
- 面向开发人员的机器学习指南3
- vue(3)
- 【Vue】-(3) Vue与MVVM
- CCNA学习指南第3章IP寻址笔记
- theano学习指南3(翻译)-多层感知器模型
- Yii学习指南(改编)3--模型-视图-控制器 (MVC)
- shell脚本学习指南-grep文本搜索命令-学习(3)
- Vim学习指南(建议通过前3个层次)
- VS2013创建C#的dll文件
- 标准c++库和stl库,boost库,qt库的总结(一)
- asp.net三种发送邮件代码(stmp,无组件邮件发送)
- 面试编程知识+算法题总结(一)
- Spring boot之读取自定义配置文件
- vue学习指南3
- 每日一题——删除无头链表的非尾节点、逆向打印单链表
- 精华【分布式微服务云架构dubbo+zookeeper+springmvc+mybatis+shiro+redis】分布式大型互联网企业架构!
- js技术--cookie基础(封装cookie、使用cookie记录上一次用户登录名)
- STM32F407系统时钟配置是否添加FPU功能
- 获取wsdl接口账号密码限制读取的
- 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
- webpack + react + redux + es6 开发组件化前端项目
- μC/OS Ⅱ之任务就绪表的操作_2012.5.31