一、定义

组件:应用界面上一个个的区块。 
自定义的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 组件(上)转载的更多相关文章

  1. vue组件上动态添加和删除属性

    1.vue组件上动态添加和删除属性 // 添加 this.$set(this.obj, 'propName', val) // 删除 this.$delete(this.obj, 'propName' ...

  2. Vue——组件上使用v-model

    一.最近在工作过程中要实现一个搜索模糊匹配功能,考虑到组件的复用,就单独把搜索框抽出来作为一个子组件.在以往的开发中,我一般会在input框中的值变化时向父组件emit一个事件,并带上一些父组件中需要 ...

  3. webAPP如何实现移动端拍照上传(Vue组件示例)?

    摘要:使用HTML5编写移动Web应用,主要是为了尝试一下“一套代码多处运行”,一个webapp几乎可以不加修改的运行在PC/Android/iOS等上面运行.但是写到现在觉得虽然这种方式弊大于利,不 ...

  4. vue里在自定义的组件上定义的事件

    事件分为原生事件和自定义事件. vue里在自定义的组件上定义的事件,都被认为是自定义事件,必须用$emit()来触发. 这也是子组件向父传值的原理. 如果想作为原生事件,需要在原生事件后面加上.nat ...

  5. vue图片上传组件

    前言:很多项目中都需要用到图片上传功能,而其多处使用的要求,为了避免重复造轮子,让我决定花费一些时间去深入了解,最终封装了一个vue的图片上传组件.现将总结再次,希望有帮助. Layout <d ...

  6. vue 学习七 组件上使用插槽

    我们有时候可能会在组件上添加元素,就像下面那样 <template> <div id="a"> <com1> <p>我是渲染的值&l ...

  7. vue 学习六 在组件上使用v-model

    其实这个部分应该是属于component,为什么把这玩意单独拿出来呢,原因是它这个东西比较涉及到了vue的事件,以及v-model指令的使用,还是比较综合的.所以就拿出来啦 父组件 <templ ...

  8. c#版阿里云oss上传——基于layui、pupload的oss 后端web端直传的vue组件

    废话前言 去年,做项目用到oss上传,度娘上逛了一圈写了个前后端代码结合的c#版oss上传. 今年又要用到oss上传时发现很难复用,复用改动的范围太大,显然这个轮子不合格.于是想了下,花了一天的时间( ...

  9. AntDesign VUE:上传组件图片/视频宽高、文件大小、image/video/pdf文件类型等限制(Promise、Boolean)

    文件大小限制 - Promise checkFileSize(file, rules) { return new Promise((resolve, reject) => { file.size ...

  10. AntDesign VUE:上传组件自定义限制的两种方式(Boolean、Promise)

    AntD上传组件 AntDesign VUE文档 第一种方式 beforeUpload(file) { let isLt = true if (filesSize) { isLt = file.siz ...

随机推荐

  1. I/O 模型

    5种I/O模型的基本区别: 阻塞式I/O 非阻塞式I/O I/O复用 信号异步模型 异步I/O 1. 阻塞 I/O 最流行的I/O模型是阻塞I/O模型,缺省情形下,所有套接口都是阻塞的.我们以数据报套 ...

  2. laravel sql复杂语句,原生写法----连表分组

    ### 使用了临时表.又分组又连表的感觉好难写,使用拉 ravel 但是现在越来也相信,没解决一个新的难题,自己又进步了一点点 ### 原生的sql: select user_code, realna ...

  3. Confluence 6 Oracle 连接问题解决

    如果 Confluence 提示没有 class 文件,你可能将你的 JDBC 驱动放置到了错误的文件夹. 下面的页面包含了一些你在使用 Oracle 数据库连接的时候可能会遇到的常见问题,请参考: ...

  4. Confluence 6 色彩选择器展开的页面

    色彩选择器展开的页面中对色彩的选择配置. https://www.cwiki.us/display/CONFLUENCEWIKI/Customising+Colour+Schemes

  5. iOS运行时使用(动态添加方法)

    1 举例  我们实现一个Person类 然后Person 其实是没得对象方法eat:的 下面调用person的eat方法 程序是会奔溃的 那么需要借助运行时动态的添加方法 Person *p = [[ ...

  6. Swift中 @objc 使用介绍

    在swift 中 如果一个按钮添加点击方法 如果定义为Private  或者 定义为 FilePrivate 那么会在Addtaget方法中找不到私有方法 但是又不想把方法暴露出来,避免外界访问 ,那 ...

  7. Java的小实验——各种测试以及说明

    日期:2018.10.07 星期五 博客期:014 一.Java中的位运算 代码如下: package Morts107; public class Test107 { public static v ...

  8. dubbo源码之服务消费

    消费端启动初始化过程: 消费端的代码解析也是从配置文件解析开始的,服务发布对应的<dubbo:service,解析xml的时候解析了一个ServiceBean,并且调用ServiceConfig ...

  9. 1706: 神奇的编码(zzuli)

    题目描述 假如没有阿拉伯数字,我们要怎么表示数字呢 小明想了一个方法如下: 1 -> A 2 -> B 3 -> C .... 25 -> Y 26 -> Z 27 -& ...

  10. bzoj 3529

    非常好的一道莫比乌斯反演题,对提升自己的能力有很大帮助. 首先我们分析一下题意:题意让我们求,其中 那么我们首先对后面的式子进行一下变形,变形过程详见https://blog.csdn.net/lle ...