在父组件中定义子组件
我们除了定义独立的组件之外,Vue
还支持在父组件中定义子组件。
示例
源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件</title>
</head>
<body>
<template id="parent">
<h1>父组件</h1> <children></children>
</template>
<template id="children">
<h2>子组件</h2>
</template>
<div id="app">
<parent></parent>
</div>
<script src="../../../js/vue/vue/1.0/vue.js"></script>
<script type="application/javascript">
//定义父组件
Vue.component('parent',{
template:'#parent'
//在父组件中定义子组件
,components:{
'children':{
template:'#children'
}
}
});
new Vue({
el:'#app'
});
</script>
</body>
</html>
结果
分析
如上面所示,我们在父组件parent
中定义子组件children
,由于子组件children
是在父组件中定义的,所以说要想使用子组件,则子组件<children></children>
必须在父组件中