如下项目:

App.vue 父组件
<template>
<div id="app">
<Vheader/> <! -- 使用组件 将子组件Vheader.vue作为父组件App.vue的头 -->
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{res}}</h1> <! -- res是父组件自己的数据 -->
</div>
</template>
<script>
import Vheader from './components/Vheader.vue' // 导入组件,引入文件Vheader.vue
export default {
name: 'App', // 父组件名字
data(){ // data一定要是个函数
return {
res: "res"
}
},
components: { // 挂载组件,子组件关联到父组件
Vheader
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
components/Vheader.vue 子组件
<template> <! -- 创建子组件Vheader.vue -->
<div class="header">
<h1>{{ msg }}</h1>
</div>
</template>
<script>
export default {
name: 'Vheader', // 子组件的名字
data(){ // data一定要是一个函数
return {
msg: "Vheader"
}
}
}
</script>
<style>
</style>
main.js
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
要将组件Vheader.vue嵌套在App.vue里面。
本文详细介绍了如何在Vue中实现组件的嵌套,通过具体的代码示例,展示了父组件App.vue如何引入并使用子组件Vheader.vue,以及它们之间的数据交互。
1482

被折叠的 条评论
为什么被折叠?



