一、定义

组件:应用界面上一个个的区块。 
自定义的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. SSH框架搭建笔记

    1.建立一个web项目,设置编码格式,建立src下的包,建立资源文件夹 2.加入Spring运行必须的jar包(5个jar包) spring-beans-4.1.4.RELEASE.jar sprin ...

  2. Golang的单引号、双引号与反引号

    Go语言的字符串类型string在本质上就与其他语言的字符串类型不同: Java的String.C++的std::string以及Python3的str类型都只是定宽字符序列 Go语言的字符串是一个用 ...

  3. 【进阶3-1期】JavaScript深入之史上最全--5种this绑定全面解析(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记  https://github.com/yygmind/blog/issues/20 this的绑定规则总共有下面5种. 1.默认绑定(严格/非严 ...

  4. vue v-show绑定

    在Vue中使用v-show指令来选择性的显示内容.它的属性值可以是布尔值.属性名称以及函数名称.如果使用函数来控制的话,无论函数内容如何运算判断,最终返回布尔值true或者false就可以了 < ...

  5. MSChart的研究(转)

    介绍MSChart的常用属性和事件 MSChart的元素组成 最常用的属性包括 ChartAreas:增加多个绘图区域,每个绘图区域包含独立的图表组.数据源,用于多个图表类型在一个绘图区不兼容时. A ...

  6. HttpListener通讯成功案例

    1.创建WindowsService,如下代码 using System;using System.Net;using System.Net.Sockets;using System.ServiceP ...

  7. Confluence 6 白名单表达式类型

    表达式类型 当添加一个 URL 到白名单列表中的时候,你可以选择采取下面的表达式进行添加. 域名名称(Domain name) 允许 URL 为一个指定的域名. http://www.example. ...

  8. python之线程同步

    lock与rlock 使用lock不能连续两次获取锁,获取锁必须先释放锁.但是在一个线程中调用另一个函数时,在该函数中要继续操作共享的数据,这时获取锁就相当于连续执行两次获取锁,所以lock就不适用该 ...

  9. windows与mac下安装nginx

    window下 下载链接,自己选一个版本下载 nginx官网下载 本人放在D盘: 启动nginx 有很多种方法启动nginx (1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过 (2)打开 ...

  10. sqlalchemy 的设置及使用

    FLASK之数据库设置 数据库 知识点 Flask-SQLALchemy安装 连接数据库 使用数据库 数据库迁移 邮件扩展 4.1 数据库的设置 Web应用中普遍使用的是关系模型的数据库,关系型数据库 ...