• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >vue > 分分钟玩转Vue.js组件

分分钟玩转Vue.js组件

作者:NextStand 字体:[增加 减小] 来源:互联网 时间:2017-05-30

本文主要包含男主分分钟黑化,分分钟需要你,分分钟追剧,分分钟,分分钟需要你歌词等相关知识,NextStand 希望在学习及工作中可以帮助到您

组件简介

组件系统是Vue.js其中一个重要的概念,它提供了一种抽象,让我们可以使用独立可复用的小组件来构建大型应用,任意类型的应用界面都可以抽象为一个组件树:

这里写图片描述 

那么什么是组件呢?
组件可以扩展HTML元素,封装可重用的HTML代码,我们可以将组件看作自定义的HTML元素。

组件的创建和注册

基本步骤

Vue.js的组件的使用有3个步骤:创建组件构造器、注册组件和使用组件。

这里写图片描述 

下面的代码演示了这3个步骤:

<!DOCTYPE html>
<html>
 <body>
 <div id="app">
 <!-- 3. #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>

</div>

运行结果如下:

这里写图片描述 

可以看到,使用组件和使用普通的HTML元素没什么区别。

理解组件的创建和注册

我们用以下几个步骤来理解组件的创建和注册:
1. Vue.extend()是Vue构造器的扩展,调用Vue.extend()创建的是一个组件构造器,而不是一个具体的组件实例。
2. Vue.extend()构造器有一个选项对象,选项对象的template属性用于定义组件要渲染的HTML。
3. 使用Vue.component()注册组件时,需要提供2个参数,第1个参数时组件的标签,第2个参数是组件构造器。
4. Vue.component()方法内部会调用组件构造器,创建一个组件实例。
5. 组件应该挂载到某个Vue实例下,否则它不会生效。

请注意第5点,以下代码在3个地方使用了my-component标签,但只有#app1和#app2下的my-component标签才起到作用。

<!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>
</div>

这里写图片描述

全局注册和局部注册

调用Vue.component()注册组件时,组件的注册是全局的,这意味着该组件可以在任意Vue示例下使用。
如果不需要全局注册,或者是让组件使用在其它组件内,可以用选项对象的components属性实现局部注册。

上面的示例可以改为局部注册的方式:

<!DOCTYPE html>
<html>
 <body>
 <div id="app">
 <!-- 3. my-component只能在#app下使用-->
 <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>'
 })

 new Vue({
 el: '#app',
 components: {
 // 2. 将myComponent组件注册到Vue实例下
 'my-component' : myComponent
 }
 });
 </script>
</html>

由于my-component组件是注册在#app元素对应的Vue实例下的,所以它不能在其它Vue实例下使用。

<div id="app2">
 <!-- 不能使用my-component组件,因为my-component是一个局部组件,它属于#app-->
 <my-component></my-component>
</div>

<script>
 new Vue({
 el: '#app2'
 });
</script>
</div>

如果你这样做了,浏览器会提示一个错误:

这里写图片描述

父组件和子组件

我们可以在组件中定义并使用其他组件,这就构成了父子组件的关系。

<!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>

</div>

这段代码的运行结果如下:

这里写图片描述 

我们分几个步骤来理解这段代码:

我们分几个步骤来理解这段代码:

var Child = Vue.extend(…)定义一了个Child组件构造器
var Parent = Vue.extend(…)定义一个Parent组件构造器
components: { ‘child-component': Child },将Child组件注册到Parent组件,并将Child组件的标签设置为child-component。
template :'<p>This is a Parent component</p><child-component></child-component>',在Parent组件内以标签的形式使用Child组件。
Vue.component(‘parent-component', Parent) 全局注册Parent组件
在页面中使用标签渲染Parent组件的内容,同时Child组件的内容也被渲染出来

这里写图片描述 

Child组件是在Parent组件中注册的,它只能在Parent组件中使用,确切地说:子组件只能在父组件的template中使用。

请注意下面两种子组件的使用方式是错误的:

1. 以子标签的形式在父组件中使用

<div id="app">
 <parent-component>
 <child-component></child-component>
 </parent-component>
</div>
</div>

为什么这种方式无效呢?因为当子组件注册到父组件时,Vue.js会编译好父组件的模板,模板的内容已经决定了父组件将要渲染的HTML。

parent-component相当于运行时,它的一些子标签只会被当作普通的HTML来执行,child-component不是标准的HTML标签,会被浏览器直接忽视掉。

2. 在父组件标签外使用子组件

<div id="app">
 <parent-component>
 </parent-component>
 <child-component>
 </child-component>
</div>

</div>

运行这段代码,浏览器会提示以下错误

这里写图片描述

组件注册语法糖

以上组件注册的方式有些繁琐,Vue.js为了简化这

您可能想查找下面的文章:

  • 分分钟玩转Vue.js组件(二)
  • 分分钟玩转Vue.js组件

相关文章

  • 2017-05-30Vue监听数据对象变化源码
  • 2017-05-30Vue.js第二天学习笔记(vue-router)
  • 2017-05-30Vue.js实现模拟微信朋友圈开发demo
  • 2017-05-30Vue实现双向数据绑定
  • 2017-09-05vue-source的实例
  • 2017-05-30Vue数据驱动模拟实现1
  • 2017-05-30vue2.0数据双向绑定与表单bootstrap+vue组件
  • 2017-05-30vue组件中点击按钮后修改输入框的状态实例代码
  • 2017-05-30Vue.js 60分钟快速入门教程
  • 2017-05-30Vue.2.0.5过渡效果使用技巧

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • 详解Vue2 无限级分类(添加,删除,修改)
    • 使用vue编写一个点击数字计时小游戏
    • Vue.directive自定义指令的使用详解
    • 解析Vue2.0双向绑定实现原理
    • vue项目中做编辑功能传递数据时遇到问题的解决方法
    • VUE axios 实现自定义下载功能
    • vue2的todolist入门小项目的详细解析
    • 最新Vue技术栈开发实战
    • vue+axios实现登录拦截的实例代码
    • vue.js入门教程之基础语法小结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有