0
0
Vueframework~10 mins

Pages and file-based routing in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pages and file-based routing
Start: App loads
Router reads URL
Look for matching file in /pages
File found
Load component
Render page component
User navigates -> Router updates URL -> Repeat
The router reads the URL, finds the matching page file, loads its component, and renders it. If no file matches, it shows a 404 page.
Execution Sample
Vue
pages/
  index.vue
  about.vue

// URL: /about
// Router loads about.vue component and renders it
This code shows how the router picks the about.vue file to render the About page when the URL is /about.
Execution Table
StepURLRouter ActionFile MatchedComponent LoadedRendered Output
1/Reads URL '/'index.vueIndex componentHome page content
2/aboutReads URL '/about'about.vueAbout componentAbout page content
3/contactReads URL '/contact'No matching file404 component404 Not Found page
4User clicks link to '/'Reads URL '/'index.vueIndex componentHome page content
💡 Routing stops when a matching page file is found or 404 page is shown if no match.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
currentURL'''/''/about''/contact''/'
matchedFilenullindex.vueabout.vuenullindex.vue
loadedComponentnullIndexAbout404Index
renderedPagenullHome pageAbout page404 pageHome page
Key Moments - 2 Insights
Why does the router show a 404 page when the URL does not match any file?
Because the router looks for a file matching the URL in the /pages folder. If it finds none, it loads the 404 component as shown in step 3 of the execution_table.
How does the router know which component to load for a URL?
It matches the URL path to a file name in the /pages folder. For example, '/about' matches about.vue, so it loads the About component as in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table. What component is loaded when the URL is '/'?
A404 component
BAbout component
CIndex component
DContact component
💡 Hint
Check the 'Component Loaded' column for Step 1 where URL is '/'.
At which step does the router show the 404 page?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Rendered Output' column to find when '404 Not Found page' appears.
If a new file 'contact.vue' is added to /pages, what will happen at Step 3?
ARouter will still show 404 page
BRouter will load Contact component
CRouter will load About component
DRouter will load Index component
💡 Hint
Refer to how the router matches URL to file names in the execution_table steps.
Concept Snapshot
Pages and file-based routing in Vue:
- URL path matches a file in /pages folder
- Matching file's component loads and renders
- No match shows 404 page
- Navigation updates URL and repeats routing
- Simple, automatic page management
Full Transcript
When the app starts, the router reads the current URL. It looks for a file in the /pages folder that matches the URL path. If it finds a matching file, it loads that file's component and renders it as the page. If no file matches, the router loads a 404 Not Found page. When the user clicks links, the URL changes and the router repeats this process to load the correct page component. This system makes routing simple by using the file names as routes automatically.