【问题标题】:Laravel Vue — router-view not renderingLaravel Vue — 路由器视图不渲染
【发布时间】:2021-02-02 09:43:45
【问题描述】:

所以我试图通过使用 laravel vue vue-router 和 vuex 构建一个来了解 SPA。

我的问题是无论我做什么,路由器视图都没有呈现,我开始在这里失去希望,它只显示来自 vue 组件的 Hello 消息。

只要我知道代码没有任何错误。

这是我的设置和文件

laravel 路由器页面:web.php

Route::get('/{any}', [App\Http\Controllers\AppController::class, 'index'])->where('any', '.*');

home.blade.php:

@extends('layouts.app')

@section('content')
   <App></App>
@endsection

这是我的 vue 文件

app.js:

import Vue from 'vue';            
import router from './router'     
import App from './components/App'
                                  
require('./bootstrap');           
                                  
const app = new Vue({             
 el: '#app',                   
 components: { App },          
 router                        
});                               

路由器.js

import Vue from 'vue'                                       
import VueRouter from 'vue-router'                          
import ExampleComponent from './components/ExampleComponent'
                                                            
Vue.use( VueRouter )                                        
                                                            
export default new VueRouter( {                             
 mode: 'history',                                          
 router: [                                                 
  { path: '/', component: ExampleComponent }              
 ]                                                         
} )                                                         

App.vue 文件

<template>
 <div class="content">
  <h1>Hello</h1>
  <router-view></router-view>
 </div>
</template>

<script>
 export default {
  name: 'App'
 }
</script>

ExampleComponenet.vue

<template>
 <div>
  <h1>This is the Example Component</h1>
 </div>
</template>

我得到的 OUT PUT 是 App.vue 组件加载,但 &lt;router-view&gt;&lt;/router-view&gt; 呈现为 html 注释 &lt;!----&gt;

Hello
<!---->

【问题讨论】:

    标签: javascript laravel vue.js vuejs2 vue-router


    【解决方案1】:

    我尝试在代码沙箱中重新创建您的代码,结果发现您定义的路线错误。您必须在 router.js 上使用 routes 而不是 router

    https://codesandbox.io/s/proud-leftpad-fwtf7?file=/src/router.js


    在您的app.js 中,您告诉 Vue 应用程序挂载到 id 为 app 的元素上,因此

    {
      el: "#app",
      components: ...
    }
    

    但是在你的home.blade.php 上你没有任何&lt;div id="app"&gt;&lt;/div&gt;

    它可能存在于您的layouts.app 中,但您没有包含它。如果不存在,那就是问题所在。

    我也不确定你是如何在 Laravel 中声明你的路线的:

    Route::get('/{any}', [App\Http\Controllers\AppController::class, 'index'])->where('any', '.*');
    

    通常看起来像:

    Route::get('/{any}', 'SpaController@index')->where('any', '.*');
    

    来源:https://laravel-news.com/using-vue-router-laravel

    【讨论】:

    • 我正在使用 标签,它会渲染 App.vue Hello 消息,但不会渲染 。跨度>
    • 关于 Laravel 路线,我正在使用 Laravel 8 中的新闻语法。
    • 我应该在哪里导入它?在哪个文件中
    • 谢谢你,我在 router.js 文件中使用了router 而不是routes。就是这样。
    【解决方案2】:

    好的,请试试这个。

     router: [                                                 
      { path: '/example', component: ExampleComponent }              
     ]
    

    然后访问 URL“http://localhost/exmaple”,看看它是否正常工作。

    【讨论】:

    • 就我而言,因为我在 web.php 文件中使用 Route::get('/{any}', [App\Http\Controllers\AppController::class, 'index'])-&gt;where('any', '.*'); 它只会在 / 路径中显示页面。
    • 好吧,他是对的。 el:“#app”是正确的。我希望你做得很好。
    猜你喜欢
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 2020-04-09
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2019-02-21
    相关资源
    最近更新 更多