【问题标题】:Vue <router-view/> loads header components twiceVue <router-view/> 两次加载头部组件
【发布时间】:2019-06-16 06:24:32
【问题描述】:

我正在构建一个简单的 vue 应用程序,我遇到了一个奇怪的问题,我的标签中的组件在标签内第二次呈现。

最初,和组件不在标题标签中,但它们出现在组件下方。

App.vue

<template>
  <div id="app">
    <header><app-logo/><nav-bar/></header>
    <section class="container">
    <router-view/>
    </section>
  </div>
</template>
<script>
import AppLogo from './components/AppLogo.vue'
import Home from './components/Home.vue'
export default {
  name: 'App',
  components: {
    Home,
    AppLogo
  }
}
</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: 60px;
}
.container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.title {
  font-family: "Quicksand", "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; /* 1 */
  display: block;
  font-weight: 300;
  font-size: 100px;
  color: #35495e;
  letter-spacing: 1px;
}
.subtitle {
  font-weight: 300;
  font-size: 42px;
  color: #526488;
  word-spacing: 5px;
  padding-bottom: 15px;
}
.links {
  padding-top: 15px;
}
</style>

导航栏组件:

<template>
    <div>
        <h1> TFJS/Vue Examples </h1>
        <div v-for="page in pages" v-bind:key="page">
            <div>
                <div>
                    <router-link to='/basic-logistic-regression'> {{ page }} </router-link>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            pages: ['/BasicLogisticRegression']
        }
    }
}
</script>

我想让导航栏组件始终出现在路由器视图上方。

【问题讨论】:

  • 你能分享你的script标签和你的header组件吗?
  • 我什至还没有构建导航栏,但它只是一个链接列表,我这样做主要是为了试验 tf.js 和 d3,但我对 vue 很感兴趣,并认为它很适合我的想法

标签: javascript html vue.js


【解决方案1】:

我通过使用名为 loadHome() 的挂载方法解决了这个问题,该方法使用 this.$router.push('/home') 以编程方式强制路由器视图加载主页组件。

这里是代码

App.vue

<template>
  <div id="app">
    <div class="routing">
      <header><app-logo/><nav-bar/></header>
    </div>
    <section class="container">
    <router-view/>
    </section>
  </div>
</template>

<script>
import AppLogo from './components/AppLogo.vue'
import NavBar from './components/NavBar.vue'
import Home from './components/Home.vue'

export default {
  name: 'App',
  components: {
    Home,
    AppLogo,
    NavBar
  },
  methods: {
    loadHome: function(){
      this.$router.push('/home')
    }
  },
  mounted: function(){
    this.loadHome()
  }
}
</script>

【讨论】:

  • 最后这个问题是完全可以避免的。路由器应该自动告诉路由器视图加载该路由的页面。我的路由器一定是关机了。我从头开始重做,并且效果很好。
猜你喜欢
  • 2019-04-21
  • 2017-05-09
  • 2019-04-27
  • 2021-06-11
  • 1970-01-01
  • 2020-05-19
  • 2016-06-28
  • 2021-05-26
  • 2019-03-13
相关资源
最近更新 更多