Tag Archives: vue router

Vue || How To Build & Deploy A Vue Site/App For Production Under A Web Hosting Environment

The following page demonstrates how to build and deploy a Vue site/app for production under a shared hosting environment.

Note: This page assumes you already have a domain and hosting plan setup. It also assumes your project is completed and ready to be deployed. For an overview on how to get started with Vue, visit the official documentation.

Contents

1. Build The Project
2. Deploy Site
3. Deploy Site - Subfolder
4. Server Configuration - HTML5 History Mode


1. Build The Project

The first step is to build the project for production. This can be achieved by a simple command.

First, navigate to the project directory in the terminal, then type one of the following commands depending on your package manager.

NPM:


npm run build

Yarn:


yarn build

Once this process is complete, in your project directory you will see a new folder named ‘dist‘. This dist folder will contain the files that are needed to upload to the server.

The ‘dist‘ folder should contain something like the following:


• css - [folder]
• img - [folder]
• js - [folder]
• favicon.ico - [file]
• index.html - [file]


2. Deploy Site

The files generated in Step 1 contained in the ‘dist‘ folder can be uploaded to your server using any method familiar to you.

On your server, the public_html directory is typically where you would place the files, which is the document root folder for your primary domain name.

Once the files are uploaded, your site should be live!


3. Deploy Site – Subfolder

If you wish to deploy your site into a subfolder (i.e: site.com/your-subfolder/), this can be done by making only a few changes!

Before you build your project (as highlighted in Step 1), it needs to be updated to point to the subfolder as the ‘base’ path. This is configured in the vue.config.js file.

If this file doesn’t already exist in your project, you can create it. It should be placed in the root directory of your project.

Next, add the following to the vue.config.js file:

If you are using Vue Router in your project, make sure to update the related base property of the router.

This can be done by updating the router index.js like so:

After the changes above are applied, follow the steps highlighted in Step 1 to build your project.

Next, upload the files generated in the ‘dist‘ folder into the subfolder directory on your server. The files can be uploaded using any method familiar to you.

Once the files are uploaded, your site should be live!


4. Server Configuration – HTML5 History Mode

When using Vue Router with HTML5 history mode, without a proper server configuration, users will get a 404 server error if they access ‘http://yoursite.com/user/id’ directly in their browser.

To fix the issue, all you need to do is add a simple catch-all fallback route to your server. If the URL doesn’t match any static assets, it should serve the same index.html page that your app lives in.

For example, if using Apache, this is configured in the .htaccess file.

If this file doesn’t already exist, you can create it. It should be placed in the same directory as your apps index.html file.

Next, add the following to the .htaccess file:

If your app is deployed in a subfolder, using Apache, this is also configured in the .htaccess file.

If this file doesn’t already exist, you can create it. It should be placed in the same directory as your apps index.html file.

Next, add the following to the .htaccess file:

Note: Replace ‘[[[your-subfolder]]]‘ with the subfolder name where your app resides.

For more example configurations, visit the official documentation for more information!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Vue || How To Create A Simple Store With Shopping Cart Using Vuex, Vue Router & Vue

The following is an implementation which demonstrates how to create a simple store with a shopping cart using Vuex, Vue Router and Vue.

This store allows you to add and remove items from a cart, filter items by category, create a new user, log in and log out (with automatic redirect), and “purchase” the items in the cart on the checkout page.


1. Get Started

To get started, make sure you have a package manager like Node.js installed. Node.js is used to install packages.

You’ll also want to have vue-cli installed. Visit the official documentation for more information on how this is done.

Next, navigate to the project directory in the terminal, then run the following commands (Node.js):

Project Setup


npm install

Compiles and hot-reloads for development


npm run serve


2. Features

This project implements a simple store with the following features:


1. Login / Create account (with automatic redirect)
2. Retrieve products from a mock 'API'
3. Add/Remove item to shopping cart
4. Simulate purchasing items on checkout
5. Custom 404 page
6. Display products per category
7. Hamburger 'options' menu


3. Screenshots

Home Page

Sample

Log In Page

Sample

Check Out Page

Sample

Hamburger Options Menu

Sample

Create Account Page

Sample

Log In Page

Sample

Home Page

Sample

Check Out Page

Sample


4. Download

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Vue || How To Set, Add & Update Query Params With Vue Router Using Vue

The following is a module which demonstrates how to set, add and update query params with Vue using Vue Router.

Note: The examples below assumes your project is already set up and configured. For an overview on how to get started with Vue and Vue Router, visit the official documentation.


1. Add & Update Query

The example below is a simple page with a login link. The link has a redirect parameter to navigate back to the referring page after successful login.

Clicking on the link results in the following:


/login?[existing query + computed result]

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Vue || How To Set Up Fade In & Fade Out Router View Transitions With Vue Router Using Vue

The following is a module which demonstrates how to set up fade in and fade out router view transitions with Vue using Vue Router.

Note: The examples below assumes your project is already set up and configured. For an overview on how to get started with Vue and Vue Router, visit the official documentation.


1. Router Index File

The example below is a sample router index.js file. This file sets up the routes in the project.

There are 2 routes: a ‘Home‘ page, and a ‘About‘ page.

The layout of the routes above are not important. They are declared here simply to demonstrate a project with multiple routes set up.


2. App.vue

The example below is the ‘App.vue‘ page. This is the root of the application. In this example, this is where the fade in and fade out router view transitions will take effect.

When a route is clicked, its contents is loaded inside the router-view. The example below demonstrates how to set up the fade in and fade out router view transition effect.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Vue || How To Set Up Vue Smooth Scroll Behavior With Vue Router Using Vue

The following is a module which demonstrates how to set up smooth anchor scroll behavior when navigating to a new route with Vue using Vue Router.

Note: The examples below assumes your project is already set up and configured. For an overview on how to get started with Vue and Vue Router, visit the official documentation.


1. Router Index File

The example below is a sample router index.js file. This file sets up the routes in the project. Its also where you set up the scrolling behavior when navigating to a new route.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Vue || How To Create A 404 Not Found Route With Dynamic Nested Routes With Vue Router Using Vue

The following is a module which demonstrates how to create a 404 not found route with dynamic nested routes via Vue Router using Vue.

Routes can be checked using Navigation Guards. This page will demonstrate verifying nested routes using the Per Route Guard.

This will demonstrate how to create a route that catches all non existing routes with Vue Router. It will also demonstrate how to use navigation guards to make sure nested dynamic routes exists.

Note: The examples below assumes your project is already set up and configured. For an overview on how to get started with Vue and Vue Router, visit the official documentation.


1. Router Index File

The example below is a sample router index.js file. This file sets up the routes in the project.

There are 4 routes: a ‘Home‘ page, a ‘DestinationDetails‘ page, a ‘ExperienceDetails‘ page, and a ‘NotFound‘ page.

The ‘ExperienceDetails‘ page is a nested child route.

The DestinationDetails route is a dynamic route that accepts props. In this example, the route.params will be set as the component props. In this component, the ‘slug‘ prop is used as the dynamic route path identifier.

The ExperienceDetails nested child route is also a dynamic route. In this component, the ‘experienceSlug‘ prop is used as the dynamic route path identifier.

Using the beforeEnter navigation guard, we can check if the route is valid by checking the dynamic route path identifier. If the route is not valid, the ‘NotFound‘ route is navigated to.

The NotFound route catches all non existing routes.


2. Home Page

The example below is the ‘Home‘ route page. This demonstrates how to navigate to the DestinationDetails route using the dynamic props.

It uses router-link to navigate to the specific route.


3. Destination Details Page

The example below is the ‘DestinationDetails‘ route page. This demonstrates how to display information about the destination and navigate to the ExperienceDetails route using the dynamic props passed to it.

It uses router-link to navigate to the specific route, and uses router-view to display the nested content from the navigated child route.

When calling the child component, it will automatically receive the parent ‘slug‘ dynamic route path identifier as a parameter.


4. Experience Details Page

The example below is the ‘ExperienceDetails‘ route page. This demonstrates how to display information about the destination experience using the dynamic props passed to it.


5. Not Found (404) Page

The example below is the ‘NotFound‘ route page. This page catches all non existing routes.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Vue || How To Set Up Vue Login Authentication & Route Handling With Vue Router Using Vue

The following is a module which demonstrates how to set up Vue login authentication and route handling using Vue Router.

Note: The examples below assumes your project is already set up and configured. For an overview on how to get started with Vue and Vue Router, visit the official documentation.


1. Router Index File

The example below is a sample router index.js file. This file sets up the routes in the project.

There are 3 routes: a ‘Home‘ page, a ‘User‘ page, and a ‘Login‘ page.

The ‘User‘ route has a meta property that says that it requires authentication.

Next, the route records are iterated over to check if the requested page requires authentication. If it does, the route is redirected to the ‘Loginnamed route, with a query string parameter of the original page to redirect to after successful login.


2. Login Route Page

The example below is the ‘Login‘ route page. It doesn’t do anything special, it just demonstrates how to accept user input and redirect to the specified path after successful login is complete.

Accessing the route redirect path can be done by using the ‘query’ property on the route object.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.