vue中多個頁面公用一個頭部

例如首頁引用頭部
header.vue 頭部

<template>
    <div class="header">
        <p>{{headTitle}}</p>
    </div>
</template>
<script>
export default {
    name: "app-header",
    data(){
        return{
        }
    },
    // 我是子組件,接收來自父組件的信息
    props: ['headTitle'],
    methods:{
    }
}
</script>

App.vue

<template>
  <div id="app">
    <app-header v-if="header_show" :headTitle = "headerTitle"></app-header>
    <router-view v-on:header="header"/>
  </div>
</template>

<script>
//引用組件
import AppHeader from './components/header/header'
export default {
  name: 'App',
  data(){
    return{
      header_show:true,   //判斷頭部是否顯示
      headerTitle: ""
    }
  },
  components:{
    AppHeader
  },
  methods:{
    // 是否顯示頭部
    header:function(bool,title){
      console.log(bool,title);
      this.header_show = bool;
      this.headerTitle = title;
    }
  }
}
</script>

Index.vue 首頁

<template>
  <div class="hello">
    test
  </div>
</template>

<script>
export default {
  name: 'Index',
  components:{
  },
  data () {
    return {
    }
  },
  created:function(){
    //   $emit子組件調用父組件的方法並傳遞數據
    this.$emit('header',true,"首頁");
  }
}
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章