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 ...
随机推荐
- mysql 5.6升级到5.7.22
下载对应的包 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar 备份数据 ...
- 解决log4j和self4j日志报错Could NOT find resource [logback.groovy]及Could NOT find resource [logback-test.xml]问题
事件背景: 我的log4j和self4j按照网上的配置,配置成功了,但是报错如下: 让我很是郁闷,于是找了一大圈........ 解决方案: 总结来说就是:log4j.properties和logba ...
- oracle 新增一条数据时设置id自增
CREATE SEQUENCE test_seq --创建一个test_seq序列INCREMENT BY 1 --每次加1START WITH 1 --从1开始NOMAXVALUE ...
- VUE 生成二维码(qrcodejs)
1. 概述 1.1 引入二维码生成模块 npm install qrcodejs2 --save 注意:此处安装qrcodejs2,安装依赖后可在main方法中进行全局引用设置,也可单独某个页面中进行 ...
- vscode开发c#
转载自: http://www.cnblogs.com/lxhbky/p/6673230.html http://www.cnblogs.com/lxhbky/p/6692065.html 一.环境安 ...
- mysql 中实现多条数据同时更新
有时间我们需要对一张表进行批量数据的更新.首先我们想的是update 语句. 比如对一张订单表order_info 多条数据更新, update order_inifo set order_cod ...
- 对象Object
功能分类 1. 创建对象 把各对数自身拥有的可枚举属性复制到第一个对象并返回:obj = Object.assign(o1, o2, o3),o1=obj ...
- 安卓易学,爬坑不易—腾讯老司机的RecyclerView局部刷新爬坑之路
前言 安卓开发者都知道,RecyclerView比ListView要灵活的多,但不可否认的里面的坑也同样埋了不少人.下面让我们看看腾讯开发工程师用实例讲解自己踩坑时的解决方案和心路历程. 话说有图有真 ...
- 3、SourceTree通过PUTTY连接GitLab
一.生成公钥和私钥 使用命令行生成(两种生成方式选择一种即可) 1.安装SourceTree打开SourceTree,点击“命令行模式”. 2.输入如下命令生成key“example@example. ...
- Confluence 6 恢复一个空间
你可以导出一个空间 – 包括页面,评论和附件到一个压缩的 XML 文件中,可选的你可以在 XML 文件中包括所有空间使用的附件.希望导入空间到其他的 Confluence 站点中,请按照下面的方法进行 ...