Vue 组件(上)转载
一、定义
组件:应用界面上一个个的区块。
自定义的HTML元素。
二、创建和注册
- Vue.extend() 扩展,创建组件构造器
- Vue.component()注册组件,2个参数,1为标签,2是组件构造器
- Vue实例范围使用组件
<!DOCTYPE html>
<html>
<body>
<div id="app">
<!-- . #app是Vue实例挂载的元素,应该在挂载元素范围内使用组件-->
<my-component></my-component>
</div>
</body>
<script src="js/vue.js"></script>
<script> // 1.创建一个组件构造器
var myComponent = Vue.extend({
template: '<div>This is my first component!</div>'
}) // 2.注册组件,并指定组件的标签,组件的HTML标签为<my-component>
Vue.component('my-component', myComponent) new Vue({
el: '#app'
}); </script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="app1">
<my-component></my-component>
</div> <div id="app2">
<my-component></my-component>
</div> <!--该组件不会被渲染-->
<my-component></my-component>
</body>
<script src="js/vue.js"></script>
<script>
var myComponent = Vue.extend({
template: '<div>This is a component!</div>'
}) Vue.component('my-component', myComponent) var app1 = new Vue({
el: '#app1'
}); var app2 = new Vue({
el: '#app2'
})
</script>
</html>
三、全局注册和局部注册
//全局注册
Vue.component('my-component', myComponent) new Vue({
el: '#app'
});
//局部注册
new Vue({
//只能在#app元素下使用
el: '#app',
components: {
// 2. 将myComponent组件注册到Vue实例下
'my-component' : myComponent
}
});
四、父子组件
父子组件:组件中再定义并使用其他组件。
<!DOCTYPE html>
<html>
<body>
<div id="app">
<parent-component>
</parent-component>
</div>
</body>
<script src="js/vue.js"></script>
<script>
//子组件构造器
var Child = Vue.extend({
template: '<p>This is a child component!</p>'
})
//父组件构造器
var Parent = Vue.extend({
// 在Parent组件内使用<child-component>标签
template :'<p>This is a Parent component</p><child-component></child-component>', //引用子组件
components: {
// 局部注册Child组件,该组件只能在Parent组件内使用
'child-component': Child
}
}) // 全局注册Parent父组件
Vue.component('parent-component', Parent) new Vue({
el: '#app'
}) </script>
</html>
五、组件注册语法糖
//全局注册
Vue.component('my-title1',{
template:'<div>This is the first component!</div>'
})
var vm1 = new Vue({
el:'#app1'
}) var vm2 = new Vue({
el:'#app2',
components:{
// 局部注册,my-title2是标签名称
'my-title2': {
template: '<div>This is the second component!</div>'
},
// 局部注册,my-title3是标签名称
'my-title3': {
template: '<div>This is the second component!</div>'
},
}
})
六、script和template标签
script
使用script标签时,type为text/x-template,是让浏览器忽略标签内容
template
不需要指定type,从用法上来看,就像是script的简化版。
七、el和data选项
el
只由new创建的实例中使用
提供已存在的DOM元素为Vue实例的挂载目标。决定其作用域。
data
只接受function。
实例的数据对象,Vue会将data属性转为getter/setter,让data响应数据变化。对象必须是纯粹的对象 (含有零个或多个的 key/value 对)
Vue.component('my-component', { data: function(){ return {a : } } })
八、props
组件实例有自己的作用域,如果想在组件里使用其他组件的数据,可以使用props传递(默认是单向绑定)
普通绑定
var vm = new Vue({
el: '#app',
data: {
name: 'keepfool',
age:
},
components: {
'my-component': {
template: '#myComponent',
props: ['myName', 'myAge']//定义props属性
}
}
})
<template id="myComponent">
<table>//表格
<tr>//一行
<th colspan="">//colspan代表占几个格子
//子组件数据
</th>//标题
</tr>
<tr>//
<td>my name</td>
<td>{{ myName }}</td>//获取数据
</tr>
<tr>
<td>my age</td>
<td>{{ myAge }}</td>//获取数据
</tr>
</table>
</template>
<div id="app">
//绑定数据
<my-component v-bind:my-name="name" v-bind:my-age="age"></my-component>
</div>
双向绑定
使用.sync双向绑定,修改时数据会回传
<my-component v-bind:my-name.sync="name" v-bind:my-age.sync="age"></my-component>
单次绑定
使用.once单次绑定,关系建立后数据不会同步。
<my-component v-bind:my-name.once="name" v-bind:my-age.once="age"></my-component>
Vue 组件(上)转载的更多相关文章
- vue组件上动态添加和删除属性
1.vue组件上动态添加和删除属性 // 添加 this.$set(this.obj, 'propName', val) // 删除 this.$delete(this.obj, 'propName' ...
- Vue——组件上使用v-model
一.最近在工作过程中要实现一个搜索模糊匹配功能,考虑到组件的复用,就单独把搜索框抽出来作为一个子组件.在以往的开发中,我一般会在input框中的值变化时向父组件emit一个事件,并带上一些父组件中需要 ...
- webAPP如何实现移动端拍照上传(Vue组件示例)?
摘要:使用HTML5编写移动Web应用,主要是为了尝试一下“一套代码多处运行”,一个webapp几乎可以不加修改的运行在PC/Android/iOS等上面运行.但是写到现在觉得虽然这种方式弊大于利,不 ...
- vue里在自定义的组件上定义的事件
事件分为原生事件和自定义事件. vue里在自定义的组件上定义的事件,都被认为是自定义事件,必须用$emit()来触发. 这也是子组件向父传值的原理. 如果想作为原生事件,需要在原生事件后面加上.nat ...
- vue图片上传组件
前言:很多项目中都需要用到图片上传功能,而其多处使用的要求,为了避免重复造轮子,让我决定花费一些时间去深入了解,最终封装了一个vue的图片上传组件.现将总结再次,希望有帮助. Layout <d ...
- vue 学习七 组件上使用插槽
我们有时候可能会在组件上添加元素,就像下面那样 <template> <div id="a"> <com1> <p>我是渲染的值&l ...
- vue 学习六 在组件上使用v-model
其实这个部分应该是属于component,为什么把这玩意单独拿出来呢,原因是它这个东西比较涉及到了vue的事件,以及v-model指令的使用,还是比较综合的.所以就拿出来啦 父组件 <templ ...
- c#版阿里云oss上传——基于layui、pupload的oss 后端web端直传的vue组件
废话前言 去年,做项目用到oss上传,度娘上逛了一圈写了个前后端代码结合的c#版oss上传. 今年又要用到oss上传时发现很难复用,复用改动的范围太大,显然这个轮子不合格.于是想了下,花了一天的时间( ...
- AntDesign VUE:上传组件图片/视频宽高、文件大小、image/video/pdf文件类型等限制(Promise、Boolean)
文件大小限制 - Promise checkFileSize(file, rules) { return new Promise((resolve, reject) => { file.size ...
- AntDesign VUE:上传组件自定义限制的两种方式(Boolean、Promise)
AntD上传组件 AntDesign VUE文档 第一种方式 beforeUpload(file) { let isLt = true if (filesSize) { isLt = file.siz ...
随机推荐
- mgo 的 session 与连接池
简介 mgo是由Golang编写的开源mongodb驱动.由于mongodb官方并没有开发Golang驱动,因此这款驱动被广泛使用.mongodb官网也推荐了这款开源驱动,并且作者在github也表示 ...
- FreeSWITCH 增删模组
今天在尝试FreeSWITCH新功能时,遇到一个问题,就是该功能所需要的模组没有加载,导致写了好久的代码不能看到效果,让人很是忧伤啊! 再此,将FS模组增删的方法记录下,以方便遇到同样问题的童鞋. 具 ...
- bootstrap栅格系统中同行div高度不一致的解决方法
通过div底部的margin和padding实现,缺点:下边框无法完整显示,建议在无边框情况下使用 .row{ overflow: hidden; } [class*="col-" ...
- flex 兼容性写法
flex http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 阮一峰老师详解 box 用于父元素的样式: display: box; 该属 ...
- easyui datagrid 隔行变色
easyui datagrid 隔行变色 一:实现样图 二:实现代码 $('#dataGrid').datagrid({ rowStyler:function(index,row){ if (row ...
- 【ES】学习12-近似聚合
在数据操作中有三个考虑指标:大数据.精确性和实时性.三者难以同时满足. 精确 + 实时 数据可以存入单台机器的内存之中,我们可以随心所欲,使用任何想用的算法.结果会 100% 精确,响应会相对快速. ...
- base64解密
问题 : base64解密 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个 ...
- easyui实现背景图片半透明状态,悬浮在大背景之上
首先是查找素材,使用AI将所需要的图案画出来,切记将图案的背景设置为所需要的透明状态.项目使用的是easyui架构 为啥加两个背景图呢,因为这样的布局最开始是给一个矩形框加上的背景图片,若是还使用矩形 ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- new/new[]和delete/delete[]是如何分配空间以及释放空间的
C++中程序存储空间除栈空间和静态区外,每个程序还拥有一个内存池,这部分内存被称为或堆(heap).程序可以用堆来存储动态分配的对象,即那些在程序运行时创建的对象.动态对象的生存期由程序来控制 ,当动 ...