Vue組件的引用

父子組件使用

在工程目錄/src下創(chuàng)建component文件夾,并在component文件夾下創(chuàng)建一個 firstcomponent.vue并寫仿照 App.vue 的格式寫一個組件。

<template>
  <div id="firstcomponent">
    <h1>I am a title</h1>
    <a> {{ author }} </a>
  </div>
</template>
 
<script type="text/javascript">
export default {
  data () {
    return {
      author: "第一個子組件"
    }
  }
}
</script>
 
<style>
</style>
image
然后在 App.vue 使用組件

第一步,引入。在<script></script>標簽內的第一行寫:

import firstcomponent from './component/firstcomponent.vue'

第二步,注冊。在<script></script>標簽內的 data 代碼塊后面加上 components: { firstcomponent }

export default {
  data () {
    return {
      msg: 'Hello Vue!'
    }
  },
  components: { firstcomponent }
}

第三步,使用。在<template></template>內加上<firstcomponent></firstcomponent>

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <h1>{{ msg }}</h1>
    <firstcomponent></firstcomponent>
  </div>
</template>

全局組件

  • 使用更為方便,不需要聲明,直接用
  • 在main.js中引入一次并使用Vue.component('組件名', 組件對象)
  • 所有的組件通過組件名使用
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內容,還有我對于 Vue 1.0 印象不深的內容。關于...
    云之外閱讀 5,177評論 0 29
  • Vue 實例 屬性和方法 每個 Vue 實例都會代理其 data 對象里所有的屬性:var data = { a:...
    云之外閱讀 2,372評論 0 6
  • 1.安裝 可以簡單地在頁面引入Vue.js作為獨立版本,Vue即被注冊為全局變量,可以在頁面使用了。 如果希望搭建...
    Awey閱讀 11,298評論 4 129
  • 第一章 初識javaJAVA 第一講:什么是程序?:為了讓計算機執(zhí)行某些操作或解決某個問題而編寫的一系列有序指令的...
    人子日月幾點閱讀 596評論 0 1
  • 剛才寫完感賞導師那篇文,突然就想吃壽司了,而且就想沖這家店的!這個店比較貴,但是好吃!真好吃! 上網(wǎng)隨便一搜,...
    貓公主喵閱讀 178評論 0 1

友情鏈接更多精彩內容