在组件内嵌套Vue组件时出现问题

时间:2019-01-24 10:03:27

标签: javascript vue.js vuejs2 vue-component

我可以在根App.vue组件中嵌套组件,但是如果我尝试在非根组件中嵌套组件,则不会显示任何内容。如果我改为将不会显示在App.vue的Splash.vue中的Navbar组件嵌套,则它可以工作,同样,如果我将Footer组件移至Splash.vue,它也不会。

App.vue (页脚组件运行正常,路由器随后加载了splash.vue)

<template>
  <div id="app">
      <Footer/>
      <router-view/>
  </div>
</template>

<script>
import Footer from '@/components/Footer'

export default {
  name: 'App',
  components: {
    Footer
  }
}
</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: 0px;
}
</style>

Splash.vue (Navbar组件未加载,文本也已加载,因此我知道路由器工作正常)

<template>
  <div class="test">
    <v-container fluid>
      <Navbar/>
      <p>splash loaded</p>
    </v-container>
  </div>
</template>

<script>
import Navbar from '@/components/layout/Navbar'

export default {
  name: 'Splash',
  data () {
    return {
      components: {
        Navbar
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.landing-card-style {
    border-radius: 4px;
    box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.14);
}
</style>

main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import Vuetify from 'vuetify'
import 'vuetify/dist/vuetify.min.css'

Vue.use(Vuetify)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App) 
})

Navbar.vue

<template>
    <div class="navbar">
        <nav class = "deep-purple">
            <div class="container">
                <h1>navbar component loaded</h1>
            </div>
        </nav>
    </div>
</template>

<script>
export default {
    name: 'Navbar',
    data(){
        return{

        }
    }
}
</script>

<style>

</style>

1 个答案:

答案 0 :(得分:2)

您将组件包含在data()函数中。 尝试以下方法:

   export default {
      name: 'Splash',
      components: {
        Navbar
      } 
    }