Skip to main content

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? (36 h 00 min)

You also get:

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

Already a member? Login here

Ayenan Alphonse SOSSOU avatar
Ayenan Alphonse SOSSOU

Greate course, thanks

KHALID ABDULMAJEED avatar

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

error Route [dashboard] not defined.

Nerijus avatar

I would guess you left somewhere in the blade route dashboard

KHALID ABDULMAJEED avatar

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

KHALID ABDULMAJEED avatar

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

KHALID ABDULMAJEED avatar

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

Nerijus avatar

Sorry I cannot debug for you without seeing the code

KHALID ABDULMAJEED avatar

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

Schröck avatar

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)

Nerijus avatar

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

Ahmed avatar

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

."

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.