Vue 组件嵌套(vue-cli)

1.全局注册组件

main.js
全局注册组件后,在任何地方都可以直接使用

import Header from "./components/Header";
import Footer from "./components/Footer";
import Content from "./components/Content";

Vue.component("Header", Header)
Vue.component("Footer", Footer)
Vue.component("Content", Content)

components-->Header.vue

<template>
  <div class="header">
    <h1>Header</h1>
  </div>
</template>
<style scoped>
.header {
  text-align: center;
  background: #30b031;
  padding: 0;
}
</style>

components-->Content.vue

<template>
  <div class="content">
    <h2>title</h2>
    <p>content</p>
  </div>
</template>
<style scoped>
.content {
  text-align: center;
  background: #f5c700;
  padding: 0;
}
</style>

components-->Footer

<template>
  <div class="footer">
    <h1>Footer</h1>
  </div>
</template>
<style scoped>
.footer {
  text-align: center;
  background: #f34a37;
  padding: 0;
}
</style>

App.vue

<template>
  <div id="app">
    <Header />
    <Content />
    <Footer />
  </div>
</template>

效果图:


image.png

2.局部注册

HeaderContentFooter
不变

main.js
删除全局注册代码

App.vue
这里将组件注册到App.vue中,只能App.vue中使用

<script>
import Header from "./components/Header";
import Footer from "./components/Footer";
import Content from "./components/Content";

export default {
  name: "app",
  components: {
    Header,
    Footer,
    Content
  }
};
</script>

效果与上述相同

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ## Vue进阶 ### 一、vue实例 #### 1.一个基本的vue的实例 ```html {{title}}...
    千锋陈老师阅读 4,014评论 0 1
  • 一、什么组件 组件 (Component) 是 Vue.js 的重要组成部分。能够让你在复杂的应用中拆分成独立模块...
    唯老阅读 3,012评论 0 1
  • Vue简介 2014年诞生,2013年react,09年angularjs 作者: 尤雨溪 核心概念: 组件化 双...
    简单就好wfc阅读 5,691评论 0 1
  • 1.功能技术: vue-resource : 后端 Vue-router : 前端 第三方库 : better-s...
    TTTXTTT阅读 4,181评论 0 2
  • 病了很久的手机 上午一下子入了膏肓 浑身响动无法声张 我,那一刻 有了慌张 我被世界孤独 都没有关系 但要告诉你我...
    深夜书馆阅读 1,270评论 0 0

友情链接更多精彩内容