You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在Laravel中使用Vue.js:如何以Vue方式引入页面头部、侧边栏等部件?

Absolutely! You can absolutely migrate your Blade-included parts like headers and sidebars into Vue components, and using named router views is a great approach to organize your layout. Let me walk you through how to do this step by step:

Migrating Blade Includes to Vue Components with Named Router Views
  • 1. Convert Blade Partials to Vue Components
    Take your existing admin.parts.header Blade file and rewrite it as a Vue component (e.g., Header.vue). You’ll need to replace Blade-specific syntax with Vue’s equivalent, and handle any Laravel data you were using:

    <template>
      <header class="admin-header">
        <!-- Replace Blade's {{ $user->name }} with Vue's interpolation -->
        <div class="greeting">Welcome back, {{ userName }}</div>
        <!-- Add your header UI elements here -->
      </header>
    </template>
    
    <script>
    export default {
      name: 'AdminHeader',
      props: {
        userName: {
          type: String,
          required: true
        }
      }
      // Add methods, computed properties, or lifecycle hooks as needed
    }
    </script>
    
    <style scoped>
    /* Scoped styles to avoid conflicting with other parts of the app */
    .admin-header {
      background: #1a202c;
      color: white;
      padding: 1rem 2rem;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    </style>
    

    For Laravel-specific data (like authenticated user info), you can pass it to Vue in two common ways:

    • Attach data to the window object in your root Blade template:
      <script>
        window.currentUser = {!! Auth::user()->toJson() !!};
      </script>
      
      Then access it in your Vue component via window.currentUser
    • Pass it as a prop from your root Vue component (more clean for larger apps)
  • 2. Configure Named Views in Vue Router
    Instead of using a single component property in your route config, use components (plural) to map named views to your new Vue components. Here’s an example router setup:

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    import AdminHeader from './components/Header.vue'
    import AdminSidebar from './components/Sidebar.vue'
    import Dashboard from './views/Dashboard.vue'
    import UserManagement from './views/UserManagement.vue'
    
    Vue.use(VueRouter)
    
    const routes = [
      {
        path: '/admin',
        components: {
          default: Dashboard, // Main content (unnamed router view)
          header: AdminHeader,
          sidebar: AdminSidebar
        }
      },
      {
        path: '/admin/users',
        components: {
          default: UserManagement,
          header: AdminHeader,
          sidebar: AdminSidebar
        }
      }
    ]
    
    const router = new VueRouter({ routes })
    export default router
    
  • 3. Add Named Router Views to Your Root Layout
    In your main App.vue (or root layout component), place the <router-view> tags with their corresponding name attributes to render the components:

    <template>
      <div id="app">
        <router-view name="header"></router-view>
        <div class="admin-layout">
          <router-view name="sidebar"></router-view>
          <main class="main-content">
            <router-view></router-view> <!-- Renders the default route component -->
          </main>
        </div>
      </div>
    </template>
    
    <style>
    .admin-layout {
      display: flex;
      min-height: calc(100vh - 64px); /* Account for header height */
    }
    .main-content {
      flex: 1;
      padding: 2rem;
      background: #f7fafc;
    }
    </style>
    
  • Optional: Simplify with Static Layout Components
    If your header/sidebar doesn’t change across admin routes, you can skip named views entirely and just include the components directly in your root layout. This is often simpler for consistent layouts:

    <template>
      <div id="app">
        <AdminHeader :user-name="currentUser.name" />
        <div class="admin-layout">
          <AdminSidebar />
          <main class="main-content">
            <router-view></router-view>
          </main>
        </div>
      </div>
    </template>
    
    <script>
    import AdminHeader from './components/Header.vue'
    import AdminSidebar from './components/Sidebar.vue'
    
    export default {
      components: { AdminHeader, AdminSidebar },
      data() {
        return {
          currentUser: window.currentUser
        }
      }
    }
    </script>
    

内容的提问来源于stack exchange,提问作者mafortis

火山引擎 最新活动