Vue 创建组件的两种方法
来源:互联网 发布:单片机用汇编还是c语言 编辑:程序博客网 时间:2024/06/06 00:18
创建组件的两种方法
1.全局注册
2.局部注册
var child=Vue.extend({})var parent=Vue.extend({})
Vue.extend() 全局方法 生成构造器,创建子类
使用基础 Vue 构造器,创建一个“子类”。
这样写非常繁琐。于是vue进行了简化
使用Vue.component()直接创建和注册组件:
Vue.component(id,options) 全局方法 用来注册全局组件
id 是string类型,即是注册组件的名称
options 是个对象
// 全局注册,my-component1是标签名称Vue.component('my-component1',{ template: '<div>This is the first component!</div>'})var vm1 = new Vue({ el: '#app1'})
Vue.component()的第1个参数是标签名称,第2个参数是一个选项对象,使用选项对象的template属性定义组件模板。
使用这种方式,Vue在背后会自动地调用Vue.extend()。
在选项对象的components属性中实现局部注册:
var vm2 = new Vue({ el: '#app2', components: { // 局部注册,my-component2是标签名称 'my-component2': { template: '<div>This is the second component!</div>' }, // 局部注册,my-component3是标签名称 'my-component3': { template: '<div>This is the third component!</div>' } }})
==局部注册都放在选项对象中创建==
注意:这里是components,里面可以定义多个组件。
简化后是这样的写法
<body> <div id='box'> <parent> </parent> </div> <script src='js/vue.js'></script> <script> Vue.component('parent',{ template:`<div><h1>我是父组件</h1><child></child></div>`, components:{ 'child':{ template:`<h1>我是子组件</h1>` } } }) new Vue({ el:'#box' }) </script></body>
注册一个parent的父组件。然后在父组件的选项对象中注册一个child的子组件。将子组件child的标签写到父组件parent的template里面。
页面上的样式结构就是
<div> <h1>我是父组件</h1> <h1>我是子组件</h1></div>
注意:用局部注册的子组件不能单独直接使用!
标签挂在div里,会报错
<div id='box'> <child></child></div>结果会报错
阅读全文
0 0
- Vue 创建组件的两种方法
- vue初探-组件注册的两种方式
- vue向子组件传递参数的两种方式
- vue开发:vue全局组件的方法
- 注册ASP组件的两种方法
- 两种注册COM组件的方法
- vue阻止事件冒泡的两种方法
- Vue.js 中,7种定义组件模板的方法
- Vue组件传输数据的二种方法
- Vue.js 中,7种定义组件模板的方法
- Vue父子组件创建
- vue组件之间的多种通信方法
- vue组件之间的多种通信方法
- 多线程的创建的两种方法
- Javascript 创建对象的两种方法
- 创建XML文件的两种方法
- 创建XML文件的两种方法
- Java创建线程的两种方法
- ORA-00604
- 微信公众帐号开发教程第4篇-----开发模式启用及接口配置Java
- spring ——注解
- jboss在jdk下webservice报setProperty must be overridden by all subclasses of SOAPMessage
- -bash: ./shellEq.sh: bin/bash: bad interpreter: 没有那个文件或目录
- Vue 创建组件的两种方法
- 利用css清除浮动的方法
- 剑指Offer----旋转数组的最小数字
- thinkphp5整合最新ueditor,支持图片加水印
- [LeetCode] Peeking Iterator
- openstack bilean的简单使用
- ReentrantLock备忘
- mac 文件管理系统
- C#面试题1~100