【问题标题】:how to load the component on loading the page using vue-router如何使用 vue-router 在加载页面时加载组件
【发布时间】:2020-08-18 10:44:55
【问题描述】:

我的这段代码只有在你点击“/”时才有效,但我希望这些组件在我登陆页面时加载而不必点击它

const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Baz = { template: '<div>baz</div>' }

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/',
      components: {
        default: Foo,
        a: Bar,
        b: Baz
      }
    },
    {
      path: '/other',
      components: {
        default: Baz,
        a: Bar,
        b: Foo
      }
    }
  ]
})
    
  

      new Vue({
            router,
          el: '#app'
        })

我的路由器文件

<div id="app">
  <h1>Named Views</h1>
  <ul>
    <li>
      <router-link to="/">/</router-link>
    </li>
    <li>
      <router-link to="/other">/other</router-link>
    </li>
  </ul>
  <router-view class="view one"></router-view>
  <router-view class="view two" name="a"></router-view>
  <router-view class="view three" name="b"></router-view>
</div>

我希望加载 Foo 和 Bar 和 Baz 而无需单击它

这是jsfiddle

【问题讨论】:

    标签: vue.js vue-router


    【解决方案1】:

    将路由器中的/other路由推入挂载的钩子内:

    new Vue({
        router,
      el: '#app',
      
      mounted(){
      this.$router.push('/other')
      }
    })
    

    【讨论】:

    • 感谢您的回答,它在这种情况下有效,但是如果我有一个 vue 页面并且我想在那里做同样的事情怎么办。我试过挂载的,它不起作用
    • 请解释更多问题*
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 2022-07-12
    • 2019-04-21
    • 2019-04-27
    • 2017-10-15
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多