前面的话


组件(component)是Vue最强大的功能之一。组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现、功能和样式。每个页面,根据自己的需要,使用不同的组件来拼接页面。这种开发模式使得前端页面易于扩展,且灵活性高,而且组件之间也实现了解耦。本文将详细介绍Vue组件基础用法。

概述

在Vue里,一个组件本质上是一个拥有预定义选项的一个Vue实例。组件是一个自定义元素或称为一个模块,包括所需的模板、逻辑和样式。在HTML模板中,组件以一个自定义标签的形式存在,起到占位符的功能。通过Vue.js的声明式渲染后,占位符将会被替换为实际的内容。

注册组件

组件注册包括全局注册和局部注册两种

【全局注册】

要注册一个全局组件,可以使用Vue.component(tagName,option)

Vue.component("my-component",{
//选项
})

组件在注册之后,便可以在父实例的模块中以自定义元素<my-component></my-component>的形式使用

[注意]要确保在初始化根实例之前注册了组件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div> <script>
Vue.component("my-component",{
template:"<div>组件化开发</div>"
})
var vm = new Vue({
el: "#app"
})
</script>
</body>
</html>

【局部注册】

通过使用组件实例选项component注册,可以使组件仅在另一个实例或者组件的作用域中可用

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div> <script>
//注册
var child = {
template:"<div>局部组件化开发</div>"
};
var vm = new Vue({
el: "#app",
components:{
//<my-component> 将只在父模板可用
"my-component":child
}
})
</script>
</body>
</html>

组件树

使用组件实例选项components注册,可以实现组件树的效果

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div> <script>
//注册
var headerTitle = {
template:"<h1>标题</h1>"
};
var headerContent = {
template:"<p>段落</p>"
};
var header = {
template:`
<div class="hd">
<header-content></header-content>
<header-title></header-title>
</div>
`,
components:{
"header-content":headerContent,
"header-title":headerTitle
}
};
//创建实例
var vm = new Vue({
el: "#app",
components:{
"my-component":header
}
})
</script>
</body>
</html>

对于大型应用来说,有必要将整个应用程序划分为组件,以便开发可管理。一般的组件应用模板如下所示:

<div id="app">
<app-nav></app-nav>
<app-view>
<app-sidebar></app-sidebar>
<app-content></app-content>
</app-view>
</div>

【v-once】

尽管在Vue中渲染HTML很快,不过当组件中包含大量静态内容时,可以考虑使用v-once将渲染结果缓存起来。

Vue.component('my-component', {
template: '<div v-once>hello world!...</div>'
})

模板分离

在组件注册中,使用template选项中拼接HTML元素比较麻烦,这也导致了HTML和JavaScript的高耦合性。庆幸的是,Vue.js提供了两种方式将定义在JavaScript中的HTML模板分离出来。

【script】

<script type="text/x-template" id="hello-world-template">
<p>Hello hello hello</p>
</script> Vue.component('hello-world', {
template: '#hello-world-template'
})

上面的代码等价于

Vue.component('hello-world',{
template:'<p>Hello hello hello</p>'
})

下面是一个简单的示例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div> <script type="text/x-template" id="hello-world-template">
<div>hello world</div>
</script>
<script>
//注册
Vue.component("my-component",{
template:"#hello-world-template"
});
//创建实例
var vm = new Vue({
el: "#app"
})
</script>
</body>
</html>

【template】

如果使用template标签,则不需要指定type属性

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div> <template id="hello-world-template">
<div>hello world</div>
</template>
<script>
//注册
Vue.component("my-component",{
template:"#hello-world-template"
});
//创建实例
var vm = new Vue({
el: "#app"
})
</script>
</body>
</html>

命名约定

对于组件的命名,W3C规范是字母小写且包含一个中划线(-),虽然Vue没有强制要求,但是最好遵循规范

<!-- 在HTML模版中始终使用 kebab-case -->
<kebab-cased-component></kebab-cased-component>
<camel-cased-component></camel-cased-component>
<pascal-cased-component></pascal-cased-component>

当注册组件时,使用中划线、小驼峰和大驼峰这三种任意一种都可以

// 在组件定义中
components: {
// 使用 中划线 形式注册
'kebab-cased-component': { /* ... */ },
// 使用 小驼峰 形式注册
'camelCasedComponent': { /* ... */ },
// 使用 大驼峰 形式注册
'PascalCasedComponent': { /* ... */ }
}

嵌套限制

并不是所有的元素都可以嵌套模板,因为要受到HTML元素嵌套规则的限制,尤其像<ul>、<ol>、<table>和<select>等限制了能被它包裹的元素,而一些像<option>这样的元素只能出现在某些其他元素内部。在自定义组件中使用这些受限制的元素时会导致一些问题,例如:

<table id="app">
<my-row>...</my-row>
</table>

自定义组件<my-row>被认为是无效的内容,因此在渲染的时候会导致错误

<script>
//注册
var header = {
template:'<div class="hd">我是标题</div>'
};
//创建实例
new Vue({
el:'#app',
components:{
'my-row' :header
}
})
</script>

【is属性】

变通的方案是使用特殊的is属性

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<table id="app">
<tr is="my-row"></tr>
</table> <script>
//注册
var header = {
template:"<div class='hd'>我是标题</div>"
}
//创建实例
var vm = new Vue({
el: "#app",
components:{
"my-row":header
}
})
</script>
</body>
</html>

根元素

Vue强制要求每一个Vue实例(组件的本质上就是一个Vue实例)需要有一个根元素,如下所示,则会出现异常(只出现“第一段”。小火柴这里有点错误,估计是vue版本更新导致)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div> <script>
// 注册
Vue.component('my-component', {
template: `
<p>第一段</p>
<p>第二段</p>
`,
})
// 创建根实例
new Vue({
el: '#app'
})
</script>
</body>
</html>

需要改写成如下所示

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
</div> <script>
// 注册
Vue.component('my-component', {
template: `
<div>
<p>第一段</p>
<p>第二段</p>
</div>
`,
})
// 创建根实例
new Vue({
el: '#app'
})
</script>
</body>
</html>

data数据

一般的,我们在Vue实例对象或Vue组件对象中,我们通过data来传递数据

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
</div> <script>
// 注册
Vue.component('my-component', {
template: '<div>{{message}}</div>',
data:{
message:'hello'
}
})
// 创建根实例
new Vue({
el: '#app'
})
</script>
</body>
</html>

执行上面的代码,会使Vue停止运行,并在控制台发出错误提示

可以使用下面方式来绕开Vue的错误提示

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
</div> <script>
// 注册
Vue.component('my-component', {
template: '<div>{{message}}</div>',
data:function(){
return {message:'hello'}
}
})
// 创建根实例
new Vue({
el: '#app'
})
</script>
</body>
</html>

【data数据共享问题】

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
</div> <script>
// 注册
var data = {counter:0}
Vue.component('my-component', {
template: '<button v-on:click="counter +=1">{{counter}}</button>',
data:function(){
return data
}
})
// 创建根实例
new Vue({
el: '#app'
})
</script>
</body>
</html>

由于这三个组件共享了同一个data,因此增加一个counter会影响所有组件。当一个组件被定义,data需要声明为返回一个初始数据对象的函数,因为组件可能被用来创建多个实例。如果data仍然是一个纯粹的对象,则所有的实例将共享引用同一个数据对象,通过提供data函数,每次创建一个新实例后,能够调用data函数,从而返回初始数据的一个全新副本数据对象。因此,可以通过为每个组件返回全新的data对象来解决这个问题

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component></my-component>
<my-component></my-component>
<my-component></my-component>
</div> <script>
// 注册
Vue.component('my-component', {
template: '<button v-on:click="counter +=1">{{counter}}</button>',
data:function(){
return {counter:0};
}
})
// 创建根实例
new Vue({
el: '#app'
})
</script>
</body>
</html>

原生事件

有时候,可能想在某个组件的根元素上监听一个原生事件。直接使用v-bind指令是不生效的

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component @click="doSomething"></my-component>
<p>{{message}}</p>
</div> <script>
// 注册
Vue.component('my-component', {
template: '<button>按钮</button>',
})
// 创建根实例
new Vue({
el: '#app',
data:{
message:0
},
methods:{
doSomething(){
this.message++
}
}
})
</script>
</body>
</html>

要实现这个效果,使用.native 修饰v-on指令即可

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="app">
<my-component @click.native="doSomething"></my-component>
<p>{{message}}</p>
</div> <script>
// 注册
Vue.component('my-component', {
template: '<button>按钮</button>',
})
// 创建根实例
new Vue({
el: '#app',
data:{
message:0
},
methods:{
doSomething(){
this.message++
}
}
})
</script>
</body>
</html>

Vue组件的基础用法(火柴)的更多相关文章

  1. Vue组件的继承用法

    Vue组件的继承用法 点击打开视频讲解 vue组件的继承适用于UI几乎一样,只是数据不一样的情况下,但是这种情况通过统一封装组件也是能实现的,小功能建议用封装,大功能建议用组件继承,因为大功能在结合搜 ...

  2. Vue组件使用基础

    这篇博文用来记录 .vue 组件的使用方法. 可以把组件代码按照 template.style.script 的拆分方式,放置到对应的 .vue 文件中. 模板(template).初始数据(data ...

  3. react入门----组件的基础用法

    1.组件 <!-- React 允许将代码封装成组件(component),然后像插入普通 HTML 标签一样,在网页中插入这个组件.React.createClass 方法就用于生成一个组件类 ...

  4. Vue 组件的基础介绍

    1.组件定义 1.定义组件并引用 2.父组件向子组件传值 3.子组件向父组件传值 # 组件间传值:vuex (https://www.cnblogs.com/xiaonq/p/9697921.html ...

  5. vue组件中 IS 用法

    //html <link rel="stylesheet" href="http://www.jq22.com/demo/animate-141106223642/ ...

  6. Vue组件基础用法

    前面的话 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己所需, ...

  7. 【Vue】详解组件的基础与高级用法

    Vue.js 最核心的功能就是组件(Component),从组件的构建.注册到组件间通信,Vue 2.x 提供了更多方式,让我们更灵活地使用组件来实现不同需求. 一.构建组件 1.1 组件基础 一个组 ...

  8. vue学习-第三个DEMO(计算属性和监视) v-model基础用法

    <div id="demo"> 姓:<input type="text" placeholder="First Name" ...

  9. 第四节:Vue表单标签和组件的基本用法,父子组件间的通信

    vue表单标签和组件的基本用法,父子组件间的通信,直接看例子吧. <!DOCTYPE html> <html> <head> <meta charset=&q ...

随机推荐

  1. Spring Boot Debug调试

    在使用spring-boot:run进行启动的时候,如果设置的断点进不去,要进行以下的设置. 1.添加jvm参数配置 在spring-boot的maven插件加上jvmArguments配置. < ...

  2. 前端下载excel文件功能的三种方法

    1 从后端接收json数据,前端处理生成excel下载 JsonExportExcel的github地址:https://github.com/cuikangjie/JsonExportExcel 这 ...

  3. VMware12 安装 Ubuntu18.04

    安装Ubuntu18.04虚拟机 Ubuntu获取地址: 官网:https://www.ubuntu.com/download/server 清华镜像站:https://mirrors.tuna.ts ...

  4. 移动端真机调试终极利器-BrowserSync(使用方法)

    1. 安装 Node.js BrowserSync是基于Node.js的, 是一个Node模块, 如果您想要快速使用它,也许您需要先安装一下Node.js 安装适用于Mac OS,Windows和Li ...

  5. Kafka实战-KafkaOffsetMonitor

    1.概述 前面给大家介绍了Kafka的背景以及一些应用场景,并附带上演示了Kafka的简单示例.然后,在开发的过程当中,我们会发现一些问题,那就是消息的监控情况.虽然,在启动Kafka的相关服务后,我 ...

  6. Ext.Direct最新版源码下载地址

    以前的地址用不了,现在地址更新为: 全平台: http://www.sencha.com/forum/showthread.php?67992-Ext.Direct-Server-side-Stack ...

  7. 前端错误监控,sentry入门配置详细教程

    一.前言 最近经理说要把公司项目结合sentry用起来,然后组长不想做,老员工也不想做,分配任务就这么分配给我了,很荣幸接锅,摸索了几天,了解了一些基本配置,深一点的玩法可能还得实践一段时间,这里对于 ...

  8. python实现定时任务

    定时任务的实现方式有很多种,如windows服务,借助其他定时器jenkins运行脚本等方式.本文介绍的是python中的一个轻量级模块schedule. 安装 pip命令:pip install s ...

  9. 年终培训关于磁盘冗余阵列、热备、群集、负载均衡、云计算、F5、Nginx等的概念和基本原理

    在系统部署实施过程中,客户往往会关注系统的可用性方面的指标. 对于一个具备高可用性的系统来说, 多机部署方案是必不可少的. 我们这个知识分享,就从多个不同层面来介绍多机部署方案. ---------- ...

  10. 在Android项目中使用AspectJ

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/8888459.html 什么是AOP AOP是 Aspec ...