Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Routing: Nav Links and Posts Create Page

Premium
7 min read

So we have built our first page: list of the posts. Now let's build the second page to add a new post. For this lesson, I will introduce the vue-router.

create posts page


Installing and Enabling vue-router

First, we need to install the vue-router package.

npm install vue-router@latest

Then, we need to import vue-router.

resources/js/app.js:

import './bootstrap';
 
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import PostsIndex from './components/Posts/Index.vue'
 
createApp({})
.component('PostsIndex', PostsIndex)
.mount('#app')

Then, we need to build a list of routes, initialize the router, and enable that router inside createApp().

Also, we don't need to define components in the createApp() anymore, as they are already defined in the routes.

resources/js/app.js:

import './bootstrap';
 
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import PostsIndex from './components/Posts/Index.vue'
 
const routes = [
{ path: '/', component: PostsIndex },
]
 
const router = createRouter({
history: createWebHistory(),
routes
})
 
createApp({})
.component('PostsIndex', PostsIndex)
.use(router)
.mount('#app')

Vue Component for Second Page

Now, let's create a Vue component for the create post page...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

AA
Ayenan Alphonse SOSSOU ✓ Link copied!

Greate course, thanks

KA
KHALID ABDULMAJEED ✓ Link copied!

everything is good but when I try to use vue routes instead of web.php file not working

error Route [dashboard] not defined.

N
Nerijus ✓ Link copied!

I would guess you left somewhere in the blade route dashboard

KA
KHALID ABDULMAJEED ✓ Link copied!

even though I checked this https://www.youtube.com/watch?v=L7dr3Zdq0vA but still not clear can you help :) ?

KA
KHALID ABDULMAJEED ✓ Link copied!

do I need to change all extension files inside this resources/views/.. into .vue ??

KA
KHALID ABDULMAJEED ✓ Link copied!

Nerijus, Can you explain in detail, plz ? I need help

N
Nerijus ✓ Link copied!

Sorry I cannot debug for you without seeing the code

KA
KHALID ABDULMAJEED ✓ Link copied!

should I send you api.php , vue file and controller file ?

S
Schröck ✓ Link copied!

Hi Khalid,

according to https://v3.router.vuejs.org/guide/essentials/named-routes.html the links are replaced by <router-link :to=".... please note the ":" (which is missing above)

N
Nerijus ✓ Link copied!

Isn't : (colon) used to when using routes with names and parameters?

A
Ahmed ✓ Link copied!

i think in "Changing Layout: From Blade to Vue" point

we need to change resources/views/layouts/app.blade.php not in resources/views/dashboard.blade.php the correct one is "First, we will use the resources/views/layouts/app.blade.php file as a main HTML file where we set id="app" in the tag, instead of the previous

."