2

I develop with laravel9 and vue3.

My problem is simple, But path setting is not go well.

When I access url localhost:8080/tasks

This url return 404 not found and I get the following type error

GET http://localhost:8000/tasks 404 (Not Found)

I didn't know the reason , but When I rewrite path: /tasks to path /, localhost:8080 return Component that I want to need.

I have following files.

router.js

import { createRouter, createWebHistory } from "vue-router";
import TaskListComponent from "./components/TaskListComponent.vue";


const router = createRouter({
    history: createWebHistory(),
    routes: [

        {
            path: '/tasks',
            name: 'tasks.list',
            component: TaskListComponent
        }
    ]
})

export default router

App.vue

<script setup>
import HeaderComponent from "./components/HeaderComponent.vue";
</script>

<template>
    <HeaderComponent />
    <router-view></router-view>
</template>

bootstrap.js

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router.js"

const app = createApp(App);

app.use(router);

app.mount("#app");
3
  • Please focus on how to properly highlight your code rather than writing spam in your message.
    – kissu
    Commented Oct 30, 2022 at 9:42
  • I don't have experience with Laravel but this kind of problem can usually be solved by routing all the paths in Laravel to index.html and letting Vue do the remaining job
    – Duannx
    Commented Oct 31, 2022 at 1:57
  • 1
    I found the solution. add the code below in web.php Route::get('{any?}', function () { return view('welcome'); })->where('any', '.*'); Commented Nov 2, 2022 at 5:31

2 Answers 2

3

The following in web.php fixed the issue

Route::get('{any?}', function () {
    return view('welcome');
})->where('any', '.*');
1

I created a project using Vue's CLI, then proceeded to check all the relevant parts.

I took your code and applied the various changes:

  • my entry point is main.js, rather than bootstrap.js but no changes code-wise
  • in App.vue, I don't have any HeaderComponent but it's should not be an issue anyway
  • in router/index.js, I only changed the following for the component since it's better to use an alias than a relative path anyway
import TaskListComponent from "@/components/TaskListComponent.vue"

Launching the server with

pnpm dev

gives me some port, once followed to the /tasks path, I can see the component as expected.

enter image description here

The route is properly defined too

enter image description here

Here is my project directory

enter image description here

And I don't have any errors in the console.


Here is public github repo: https://github.com/kissu/so-v3-working-router

2
  • Thank you so much! but actually, I tried this code in create vue and vue CLI and did well. I 'm trying with laravel9 and vue3. perhaps, This may happen the problem. stackoverflow.com/questions/71856260/vue-3-router-with-laravel. this post is the same problem as mine. Thank you for going out of your way to implement vue development with my code. Commented Oct 30, 2022 at 11:57
  • @sorasakamoto oh, I guess you're using Vue inside of Laravel here. I don't know how to debug that one unfortunately since I don't have any experience with Laravel, sorry.
    – kissu
    Commented Oct 30, 2022 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.