Troubleshooting: Issue with displaying router-view in Laravel Vue

Currently, I am endeavoring to enhance my understanding of Single Page Applications by constructing one with Laravel, Vue, Vue Router, and Vuex.

Unfortunately, I have encountered an issue where the router-view is not rendering at all, no matter what troubleshooting steps I take. As a result, I am beginning to feel disheartened, especially since only the "Hello" message from the Vue components is being displayed.

To the best of my knowledge, there are no errors in the code.

Below are the setup details and files involved:

Laravel router page: 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 files:

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                        
});                               

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 file

<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>

The current challenge lies in the fact that while the App.vue component loads successfully, the

<router-view></router-view>
tag renders as an HTML comment <!---->.

Hello
<!---->

Answer №1

I attempted to recreate the code in codesandbox and it seems like there is an issue with how you're defining the routes. Instead of using router, make sure to use routes in your router.js

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


In your app.js, you are instructing the Vue app to mount on an element with an id of app. However, in your home.blade.php file, there doesn't seem to be a

<div id="app"></div>
.

It's possible that this div is included in your layouts.app but not being properly included. If it's missing, that could be the problem.

Additionally, I'm unsure about how you defined your route in Laravel:

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

A standard declaration would typically look like:

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

Reference:

Answer №2

Sure, give this a go.

 routes: [                                                 
  { path: '/test', component: TestComponent }              
 ]

After that, navigate to the address "http://localhost/test" and check if everything is functioning properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am experiencing difficulties connecting my webRTC app to the remote system

I've set up my webRTC app on an HTTPS server and it's working fine. However, I'm facing an issue where the Remote system details (SDP) are not being captured. Both the Local system (PC1) and Remote system (PC2) are unable to establish connec ...

Ways to retrieve an element from an array

I need help finding the right way to retrieve the value of the message "Meeting 9943...is not found or has expired". Here's what I tried: if (response.data.status == 404) { angular.element(document.getElementById("msg")).css("color", "red"); ...

What is the procedure for initiating a POST request when the payload is ready for submission?

After a successful payment, my success page is displayed with a URL that includes a query parameter. I want to make a POST request to my API endpoint when the page loads, but I'm encountering an issue where the first POST request is empty (router.quer ...

Generate a table in MongoDB using NestJs without the need to create a new collection

I am facing a challenge with my app where I need to create an order with multiple attributes, one of which is an array of ordered products. Each object in the orderedProduct array must include the productId and the amount. However, I do not want to create ...

Verify if the username is already in use

Is it possible to validate the existence of a username while the user is entering it in a textbox or immediately after they finish typing? Should I use Jquery or Ajax for this task? Does anyone have any examples demonstrating how this can be done? ...

Ways to provide instruction for all offspring of a tree node

When using d3js in my tree layout, I am looking to change certain properties of a selected node (choix1) and all its descendants. My goal is to assign a specific class (choix2) to all descendants. .on("click", function() { d3.select(this).classed("c ...

Alert and console.log continue to show the error message: 'Uncaught TypeError: Cannot read property ' ' of undefined

Encountering a Uncaught TypeError: Cannot read property 'valeurs' of undefined, however the alert() and console.log() functions are successfully displaying the data. Below is the code snippet: var item = document.getElementById('inputData&a ...

Discovering identical objects by property and combining them with the help of JavaScript or UnderscoreJS

Below is an array that I have: var somevalue = [{ code: 1, name: 'a1' }, { code: 2, name: 'b1' }, { code: 1, name: 'a2' }, { code: 1, name: 'a3' }, { code: 2, name ...

Tips for utilizing temporary variables in JavaScript

Is there a way to set a variable based on the result of a function call without calling the function multiple times or adding unnecessary temporary variables? Even though it might be seen as premature optimization, I prefer to avoid that. One approach is ...

Is it possible to use a component created in the newest version of Angular in apps developed with older versions of Angular?

I am interested in developing a component using the most recent version of Angular. However, my intention is to use this component in two distinct Angular applications - one created with Angular 6 and another with Angular 10. Is it feasible to achieve this ...

Show the value of a session variable without needing to reload the page

Within an ajax file, I am setting a session variable like this: $_SESSION['id_essai']=$ligne[1];. This variable's value should change each time the form is submitted. Without refreshing the page, I am trying to retrieve the value of this va ...

Tips for running Scrapy and Selenium on a webpage that utilizes angular JavaScript to serve data

I have been working on a web scraper that follows this process: Visit site A -> click on the buy now button -> redirected to Amazon -> scrape data -> return to site A The issue I am facing is that the site is built using AngularJS, and I am h ...

The performance of Node.js Knex SQL insert queries leaves much to be desired

I have created a simple login system that performs the following actions: Verifies if the username exists (works fine) Adds the username to the database After adding the username, I retrieve the id of the added username. Everything works smoothly on the ...

Storing form datepicker data into MongoDB using Node.js

Having an issue with the date formatting between Angular Material 6's Datepicker and Node.js. When displaying the date in the browser, it shows as 06/20/1992, but in the console output, it appears as Sat Jun 20 1992 00:00:00 GMT+0800 (Philippine Stand ...

Vue.js: V-Model input fails to update with JS virtual keyboard

I have integrated JSkeyboard into my webpage and am using v-model to dynamically update the text based on user input. The issue I am facing is that when using a physical keyboard, everything works as expected. However, when using the on-screen JS keyboard, ...

What are the steps to incorporate a jquery-ui checkbox into a widget?

I have encountered an issue where using a jquery-ui checkbox within a container that has the class ui-widget-content is causing a problem with the CSS rules. Specifically, the ".ui-widget-content .ui-state-hover" rule is overriding the .ui-icon-check rule, ...

The vue-msal encountered issues resolving endpoints. Please verify your network connection and try again. Additional information: function toArrayString() { [native code] }

Error: Unable to resolve endpoints. Please verify your network connection and try again. Details: function toString() { [native code] } at ClientAuthError.AuthError [as constructor] (webpack-internal:///./node_modules/msal/lib-es6/error/AuthError.js:26 ...

Node.js can significantly reduce the time complexity when it comes to downloading large files

I am in the process of developing a file downloader with high performance capabilities using node.js. The files I need to download can be as large as 10GB, and so far, I have experimented with various in-built node modules. Below is the code snippet I have ...

Encountering TypeScript error TS2345 while attempting to reject a Promise with an error

I recently encountered a perplexing TypeScript error message that I am struggling to comprehend. The specific error reads as follows: error TS2345: Argument of type '(error: Error) => void | Promise' is not assignable to parameter of type & ...

Turn only one bracket on the accordion

When clicking on a specific header, I want only one chevron to rotate instead of all the chevrons rotating. I am currently unsure how to specify which chevron should rotate individually. My project is in ASP.NET MVC 5 and I am using razor view to loop th ...