首页 文章

多个带vue的路由器

提问于
浏览
0

我正在运行一个PHP应用程序,我需要一些页面才能完全交换视图 . 我需要一个完全成熟的路由器,因为我需要利用历史模式API .

使用Vue路由器很容易做到这样的事情:

const router = new VueRouter({
    routes: [
        { path: '/', component: DefaultView },
        { path: '/map', component: MapView }
    ]
});

在PHP中我只是像这样加载路由器并将PHP生成的后端数据传递给两个视图:

<router-view data="<?= $data ?>"></router-view>

这适用于一个页面,但是如果我想让另一个页面具有完全不同的路由集呢?路由器需要知道它所在的页面,以便区分不同的路由集 .

我要么需要检查支柱中的传递URL,就像我已经在处理我的数据一样 . 我不确定如何从路由器读取道具数据 .

处理这个问题的好方法是什么?

1 回答

  • 1

    你只需要两个名为 router-views

    并将不同的数据传递给这些路由器视图 .

    <router-view class="view one"></router-view> //default
    <router-view class="view two" name="a"></router-view>
    <router-view class="view three" name="b"></router-view>
    
    routes: [
        {
          path: '/',
          components: {
            default: Foo,
            a: Bar,
            b: Baz
          }
        },
        {
          path: '/map',
          components: {
            default: Foo2,
            a: Bar2,
            b: Baz2
          }
        }
      ]
    

    或者使用嵌套路线视图//儿童

    routes: [
            { path: '/page1', component: page1,
              children: [
            {
    
              path: 'route1',
              component: route1
            },
            {
    
              path: 'route2',
              component: route2
            }
          ]
    
           },
            { path: '/page2', component: page2, ... }
        ]
    

相关问题