【问题标题】:Problem nesting Vue components within components在组件中嵌套 Vue 组件的问题
【发布时间】:2019-01-24 10:03:26
【问题描述】:

我可以很好地将组件嵌套在根 App.vue 组件中,但是如果我尝试将组件嵌套在非根组件中,则什么都不会显示。如果我改为将 Navbar 组件嵌套在 App.vue 中的 Splash.vue 中,它可以工作,同样,如果我将 Footer 组件移动到 Splash.vue,它就不会。

App.vue(Footer 组件工作正常,路由器然后加载 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(导航栏组件不加载,文本加载所以我知道路由器工作正常)

<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>

ma​​in.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>

【问题讨论】:

  • 您是否尝试将Navbar 嵌套在v-container 组件之外?还有,用Vue Tools调试的时候会出现吗?
  • @niklaz,我认为 v-container 是由 Vue.use(Vuetify) 注册的。
  • 如示例所示,它位于 Splash.vue 的 v-container 中,它也显示为 Vue Tools 中的组件,只是不显示。谢谢。编辑:是的,我在 v-container 内外都试过了,结果是一样的。
  • 你能分享Navbar的定义吗?
  • 我已经更新了主帖

标签: javascript vue.js vuejs2 vue-component


【解决方案1】:

你在 data() 函数中有你的组件。 试试这个:

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

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 2018-07-16
    • 2015-09-15
    • 1970-01-01
    • 2017-12-31
    • 2017-03-15
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多