vue-父子组件嵌套的示例
组件注册:
// 注册组件
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
注册局部组件
var childComponent = Vue.extend({
template: '<p>this is child template</p>'
});
Vue.component("parent",{
template: '<div><p>this is parent template</p><child></child></div>',
components: {
'child': childComponent,//child只能在父组件里使用
}
});
完整的html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<title>vue-demo</title>
</head>
<body>
<h1>vue父子组件嵌套示例</h1>
<div id='app'>
<my-component></my-component>
<parent></parent>
</div>
</body>
<script>
// 注册组件
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
// 子组件
var childComponent = Vue.extend({
template: '<p>this is child template</p>'
});
// 父组件
Vue.component("parent",{
template: '<div><p>this is parent template</p><child></child></div>',
components: {
'child': childComponent,
}
});
new Vue({
el: '#app',
data: {
}
})
</script>
</html>
注意,组件只能有一个根元素,所以最好使用一个div元素包裹组件模板,否则会提示错误:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.
以下是错误的:
Vue.component("parent",{
template: '<div><p>this is parent template</p></div><child></child>',//组件有多个根元素
components: {
'child': childComponent,
}
});
也可以使用非字符串模板注册组件,如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<title>vue-demo</title>
</head>
<body>
<h1>vue父子组件嵌套示例</h1>
<template id="child">
<p>this is child template</p>
</template>
<template id="parent">
<div>
<p>this is parent template</p>
<child></child>
</div>
</template>
<div id="app">
<parent></parent>
</div>
<script src="vue.js"></script>
<script>
var childComponent = Vue.extend({
template: '#child'
});
Vue.component("parent",{
template: '#parent',
components: {
'child': childComponent,
}
});
var app = new Vue({
el: '#app'
});
</script>
</body>
</html>
效果是一样的。
(完)
vue-父子组件嵌套的示例的更多相关文章
- vue父子组件嵌套的时候遇到 - Component template should contain exactly one root element. If you are using v-i
转自:https://blog.csdn.net/yangyiboshigou/article/details/72084619
- vue父子组件
vue父子组件 新建 模板 小书匠 为什么要厘清哪个是父组件,哪个是子组件? 一开始浏览器接收和要显示的数据非常少,此时无需划分区域进行布局.随着页面数据量的增加,如果单纯一个窗口来加载和显示数据, ...
- vue父子组件之间传值
vue父子组件进行传值 vue中的父子组件,什么是父组件什么是子组件呢?就跟html标签一样,谁包裹着谁谁就是父组件,被包裹的元素就是子组件. 父组件向子组件传值 下面用的script引入的方式,那种 ...
- vue父子组件的传值总结
久违的博客园我又回来了.此篇文章写得是vue父子组件的传值,虽然网上已经有很多了.写此文章的目的就是记录下个人学习的一部分.接下来我们就进入主题吧! 在开发vue项目中,父子组件的传值是避免不掉的. ...
- 【转】vue父子组件之间的通信
vue父子组件之间的通信 在vue组件通信中其中最常见通信方式就是父子组件之中的通性,而父子组件的设定方式在不同情况下又各有不同.最常见的就是父组件为控制组件子组件为视图组件.父组件传递数据给子组件使 ...
- Vue父子组件生命周期
转载自:https://blog.csdn.net/a8725585/article/details/79092505 vue父子组件钩子函数触发顺序 beforeMount后mounted前构造子组 ...
- vue父子组件传值加例子
例子:http://element-cn.eleme.io/#/zh-CN/component/form 上进行改的 父传子:用prop:子组件能够改变父组件的值,是共享的,和父操作是 ...
- vue 父子组件通信详解
这是一篇详细讲解vue父子组件之间通信的文章,初始学习vue的时候,总是搞不清楚几个情况 通过props在父子组件传值时,v-bind:data="data",props接收的到底 ...
- vue组件定义方式,vue父子组件间的传值
vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...
随机推荐
- window.setInterval与window.setTimeout使用实例
<script type="text/javascript"> var arrived = false; var num = 0; var timer = window ...
- 》》QQ-注册
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Nexys3学习手记1:写在前面的话
偶然的机会,结识了xilinx的几位大牛,便毫不客气的从他们的手中接过了基于Spartan-6的由Digilent公司研发的Nexys3开发板(如图1所看到的).记得非常久非常久曾经初识FPGA的时候 ...
- 关于Mac终端故障一直出现 [进程已完毕]
终端已打开就出现以下信息.无法输入不论什么的命令 Last login: Mon Aug 18 10:00:36 on ttys000 [进程已完毕] 原因:不知谁改动了 终端->偏好设置-&g ...
- iKcamp团队制作|基于Koa2搭建Node.js实战(含视频)☞ 路由koa-router
路由koa-router--MVC 中重要的环节:Url 处理器
- js实现刷新
Javascript刷新页面的几种方法: 代码如下:1,history.go(0) 2,location.reload() 3,location=location 4,location.assign( ...
- jquery实现ajax提交表单
一般情况下,我们提交form表单采用的是submit的方法,典型的场景如下. <form id="thisForm" method="post" acti ...
- mybatis 之 占位符#{} 和 ${}
#{}占位符用来设置参数,参数的类型可以有3种,基本类型,自定义类型,map基本类型作为参数,参数与占位符中的名称无关. <select id="findById" para ...
- JSON Schema 校验实例
JSON Schema 简介 JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. ...
- 微信小程序教学第三章第四节(含视频):小程序中级实战教程:下拉更新、分享、阅读标识
下拉更新.分享.阅读标识 本文配套视频地址: https://v.qq.com/x/page/h0554i4u5ob.html 开始前请把 ch3-4 分支中的 code/ 目录导入微信开发工具 这一 ...