Search⌘ K
AI Features

Building the UI for Category API: Adding a Category

Explore how to build an admin panel UI in Vue.js by adding a category feature. Learn to configure router paths, create forms, integrate Axios for HTTP requests, and use SweetAlert for user-friendly alerts within your e-commerce app front end.

Below is what our front-end code looks like:

   ¨( @ ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A3ƒ¸A3ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A±ƒ¸A±ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AZƒ¸A÷ƒ¸A÷ƒ¸AZƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AɃ¸Aÿƒ¸Aÿƒ¸AɃ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Azƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸Azƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A.ƒ¸A߃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A߃¸A.ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ašƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ašƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AHƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AHƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A¸ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¸ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Afƒ¸Aúƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aúƒ¸Afƒ¸Aƒ¸Aƒ¸Aƒ¸A ƒ¸A҃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A҃¸A ƒ¸Aƒ¸Aƒ¸Aƒ¸A†ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¤?ÿ|¤?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A†ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A7ƒ¸A惸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚´Aÿjl9ÿjl9ÿ‚´Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A惸A7ƒ¸Aƒ¸Aƒ¸Aƒ¸A	ƒ¸A¦ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿw“=ÿ_L5ÿ_L5ÿw“=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¦ƒ¸A	ƒ¸Aƒ¸Aƒ¸Aƒ¸ARƒ¸Aôƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€®@ÿfa8ÿ^H5ÿ^H5ÿfa8ÿ€®@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aôƒ¸ARƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿr…<ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿr…<ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aqƒ¸Aüƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ}¦?ÿcW7ÿ^H5ÿ^I5ÿ^I5ÿ^H5ÿcW7ÿ}¦?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aüƒ¸Aqƒ¸Aƒ¸Aƒ¸Aƒ¸A(ƒ¸Aڃ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ·Aÿmv:ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿmv:ÿƒ·Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aڃ¸A(ƒ¸Aƒ¸Aƒ¸Aƒ¸A’ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿy›>ÿ`P6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿ`P6ÿy›>ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A’ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A@ƒ¸A샸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ²@ÿii9ÿ]G5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ]G5ÿii9ÿ²@ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A샸A@ƒ¸Aƒ¸Aƒ¸Aƒ¸A
ƒ¸A±ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹AÿuŽ=ÿ_K5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5ÿuŽ=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A±ƒ¸A
ƒ¸Aƒ¸Aƒ¸Aƒ¸A]ƒ¸Aøƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ¬@ÿe]7ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿe]7ÿ¬@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aøƒ¸A]ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ã¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿp;ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ü_L5n_L5n^I5ü^I5ÿ^I5ÿ^I5ÿ^I5ÿp;ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ã¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A}ƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¢?ÿbT6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ_J5ÁaP6aP6_J5Á^I5ÿ^I5ÿ^I5ÿ^H5ÿbT6ÿ|¢?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸A}ƒ¸Aƒ¸Aƒ¸Aƒ¸A/ƒ¸AჸAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚µAÿlq9ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ó_L5P^H5^H5_L5P^I5ó^I5ÿ^I5ÿ^I5ÿ^H5ÿlq9ÿ‚µAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸AჸA/ƒ¸Aƒ¸Aƒ¸Aœƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ„ºAÿx–=ÿ`N6ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5¤bT7aQ6aQ6bT7_K5¤^I5ÿ^I5ÿ^I5ÿ^I5ÿ`N6ÿx–=ÿ„ºAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aœƒ¸Aƒ¸AWƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€¯@ÿgd8ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å`M65_K5jf9jf9_K5`M65^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿgd8ÿ€¯@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AWƒ¸Ašƒ¸Aȃ¸Aƃ¸Aƃ¸AƄºAÆv‘=Æ_K5Æ^I5Æ^I5Æ^I5Æ^I5É_K5uha9bS7bS7ha9_K5u^I5É^I5Æ^I5Æ^I5Æ_K5Æv‘=ƄºAƃ¸Aƃ¸Aƃ¸Aƃ¸Aȃ¸Ašƒ¸Aƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¹A
lr:
\D4
^I5
^I5
^I5
^I5
`M6^H5^H5`M6^I5
^I5
^I5
^I5
\D4
lr9
ƒ¹A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸Aÿÿÿÿÿÿÿÿÿþÿÿü?ÿÿü?ÿÿøÿÿøÿÿðÿÿàÿÿàÿÿÀÿÿÀÿÿ€ÿÿÿÿÿþþü?ü?øððààÀÀ€€€ÀÀàÿÿÿÿ
Setup code

Router

We’ll add a router to our project. To do that, we’ll run the above code. We’ll press “ctrl + C” to stop the terminal.

Next, we’ll run the vue add router command.

It will create a router directory with an index.js file inside.

It will also create a views directory, with two files inside:

  1. About.vue
  2. Home.vue

Next, we’ll run the npm run serve command again.

When we run the code, we’ll see two links on the homepage. We can also directly run the preconfigured router in the bottom code widget:

   ¨( @ ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A3ƒ¸A3ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A±ƒ¸A±ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AZƒ¸A÷ƒ¸A÷ƒ¸AZƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AɃ¸Aÿƒ¸Aÿƒ¸AɃ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Azƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸Azƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A.ƒ¸A߃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A߃¸A.ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ašƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ašƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AHƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AHƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A¸ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¸ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Afƒ¸Aúƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aúƒ¸Afƒ¸Aƒ¸Aƒ¸Aƒ¸A ƒ¸A҃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A҃¸A ƒ¸Aƒ¸Aƒ¸Aƒ¸A†ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¤?ÿ|¤?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A†ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A7ƒ¸A惸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚´Aÿjl9ÿjl9ÿ‚´Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A惸A7ƒ¸Aƒ¸Aƒ¸Aƒ¸A	ƒ¸A¦ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿw“=ÿ_L5ÿ_L5ÿw“=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¦ƒ¸A	ƒ¸Aƒ¸Aƒ¸Aƒ¸ARƒ¸Aôƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€®@ÿfa8ÿ^H5ÿ^H5ÿfa8ÿ€®@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aôƒ¸ARƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿr…<ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿr…<ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aqƒ¸Aüƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ}¦?ÿcW7ÿ^H5ÿ^I5ÿ^I5ÿ^H5ÿcW7ÿ}¦?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aüƒ¸Aqƒ¸Aƒ¸Aƒ¸Aƒ¸A(ƒ¸Aڃ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ·Aÿmv:ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿmv:ÿƒ·Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aڃ¸A(ƒ¸Aƒ¸Aƒ¸Aƒ¸A’ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿy›>ÿ`P6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿ`P6ÿy›>ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A’ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A@ƒ¸A샸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ²@ÿii9ÿ]G5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ]G5ÿii9ÿ²@ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A샸A@ƒ¸Aƒ¸Aƒ¸Aƒ¸A
ƒ¸A±ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹AÿuŽ=ÿ_K5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5ÿuŽ=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A±ƒ¸A
ƒ¸Aƒ¸Aƒ¸Aƒ¸A]ƒ¸Aøƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ¬@ÿe]7ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿe]7ÿ¬@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aøƒ¸A]ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ã¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿp;ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ü_L5n_L5n^I5ü^I5ÿ^I5ÿ^I5ÿ^I5ÿp;ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ã¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A}ƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¢?ÿbT6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ_J5ÁaP6aP6_J5Á^I5ÿ^I5ÿ^I5ÿ^H5ÿbT6ÿ|¢?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸A}ƒ¸Aƒ¸Aƒ¸Aƒ¸A/ƒ¸AჸAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚µAÿlq9ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ó_L5P^H5^H5_L5P^I5ó^I5ÿ^I5ÿ^I5ÿ^H5ÿlq9ÿ‚µAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸AჸA/ƒ¸Aƒ¸Aƒ¸Aœƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ„ºAÿx–=ÿ`N6ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5¤bT7aQ6aQ6bT7_K5¤^I5ÿ^I5ÿ^I5ÿ^I5ÿ`N6ÿx–=ÿ„ºAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aœƒ¸Aƒ¸AWƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€¯@ÿgd8ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å`M65_K5jf9jf9_K5`M65^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿgd8ÿ€¯@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AWƒ¸Ašƒ¸Aȃ¸Aƃ¸Aƃ¸AƄºAÆv‘=Æ_K5Æ^I5Æ^I5Æ^I5Æ^I5É_K5uha9bS7bS7ha9_K5u^I5É^I5Æ^I5Æ^I5Æ_K5Æv‘=ƄºAƃ¸Aƃ¸Aƃ¸Aƃ¸Aȃ¸Ašƒ¸Aƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¹A
lr:
\D4
^I5
^I5
^I5
^I5
`M6^H5^H5`M6^I5
^I5
^I5
^I5
\D4
lr9
ƒ¹A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸Aÿÿÿÿÿÿÿÿÿþÿÿü?ÿÿü?ÿÿøÿÿøÿÿðÿÿàÿÿàÿÿÀÿÿÀÿÿ€ÿÿÿÿÿþþü?ü?øððààÀÀ€€€ÀÀàÿÿÿÿ
After adding router

Let’s delete the Helloworld component. We’ll also delete lines 4, 10, and 15 from the Home.vue, containing the Vue logo image and references to HelloWorld.

C++
<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png"> // delete the line
<HelloWorld msg="Welcome to Your Vue.js App"/> // delete the line
</div>
</template>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue' // delete the line
export default {
name: 'Home',
components: {
HelloWorld // delete the line
}
}
</script>

Style the application

Open the file, index.html, and replace the content with the following code:

Note: We’ve added some basic fonts, as well as Bootstrap CSS and jQuery.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>Demo E-Commerce App</title>
<!-- Google fonts -->
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Courgette&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;1,300;1,400&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@100;300;400&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans&display=swap" rel="stylesheet">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<script type="module" src="https://unpkg.com/ionicons@5.2.3/dist/ionicons/ionicons.esm.js"></script>
<!-- Bootstrap JS Scripts -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- built files will be auto injected -->
</body>
</html>

The AddCategory.vue file

In this lesson, we’ll learn how to create a form for adding categories:

  1. Create a Category directory inside the views directory.

  2. Create a file labeled AddCategory.vue.

HTML
<template>
<div class="container">
<div class="row">
<div class="col-12 text-center">
<h3 class="pt-3">Add new Category</h3>
</div>
</div>
<div class="row">
<div class="col-3"></div>
<div class="col-md-6 px-5 px-md-0">
<form>
<div class="form-group">
<label>Category Name</label>
<input type="text" class="form-control" v-model="categoryName" required>
</div>
<div class="form-group">
<label>Description</label>
<input type="text" class="form-control" v-model="description" required>
</div>
<div class="form-group">
<label>ImageURL</label>
<input type="url" class="form-control" v-model="imageURL" required>
</div>
<button type="button" class="btn btn-primary">Submit</button>
</form>
</div>
<div class="col-3"></div>
</div>
</div>
</template>
<script>
export default {
data(){
return {
categoryName : null,
description : null,
imageURL : null,
}
},
props : [],
methods : {
}
}
</script>
<style scoped>
h4 {
font-family: 'Roboto', sans-serif;
color: #484848;
font-weight: 700;
}
</style>

Update router paths

For our single-page application to run properly, we need to create router paths for our newly created components. Go to the src/router/index.js file and update it, as demonstrated below.

Javascript (babel-node)
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import AddCategory from "../views/Category/AddCategory";
const routes = [
{
path: "/",
name: "Home",
component: Home,
},
{
path: "/about",
name: "About",
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () =>
import(/* webpackChunkName: "about" */ "../views/About.vue"),
},
{
path: "/admin/category/add",
name: "AddCategory",
component: AddCategory,
},
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router

Preview

Following is the preview of the form required to add a category.

Axios integration

Axios is a popular, promise-based HTTP client that supports an easy-to-use API, and can be used in both the browser and Node.

  1. Run the npm install --save axios command .
  2. Add the line in the main.js, window.axios = require('axios').

Sweet alert integration

Sweet alert integration is commonly referred to as a beautiful replacement for JavaScript’s alert method. It does the same job as an alert.

Install the package using the command, npm install --save sweetalert.

Also, we’ll add the following lines:

import swal from 'sweetalert';
window.Swal = swal

We can now access axios and sweetalert in the entire application.

API call

  1. First, we’ll trigger the addCategory function by clicking the submit button. We’ll do this by adding the code, @click="addCategory", into the submit button.
<button type="button" @click="addCategory">Submit</button>

We’ll define a function in the methods section inside the script tag.

HTML
<script>
var axios = require('axios')
import swal from 'sweetalert';
export default {
data(){
return {
categoryName : null,
description : null,
imageURL : null,
}
},
methods : {
async addCategory() {
const newCategory = {
categoryName : this.categoryName,
description : this.description,
imageUrl : this.imageURL,
};
const baseURL = "https://limitless-lake-55070.herokuapp.com/";
await axios({
method: 'post',
url: baseURL+"category/create",
data : JSON.stringify(newCategory),
headers: {
'Content-Type': 'application/json'
}
})
.then(() => {
swal({
text: "Category Added Successfully!",
icon: "success",
closeOnClickOutside: false,
});
})
.catch(err => console.log(err));
}
},
mounted(){
}
}
</script>

Click the button to view the page.

Final code

   ¨( @ ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A3ƒ¸A3ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A±ƒ¸A±ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AZƒ¸A÷ƒ¸A÷ƒ¸AZƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AɃ¸Aÿƒ¸Aÿƒ¸AɃ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Azƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸Azƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A.ƒ¸A߃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A߃¸A.ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ašƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ašƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸AHƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AHƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A¸ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¸ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Afƒ¸Aúƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aúƒ¸Afƒ¸Aƒ¸Aƒ¸Aƒ¸A ƒ¸A҃¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A҃¸A ƒ¸Aƒ¸Aƒ¸Aƒ¸A†ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¤?ÿ|¤?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A†ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A7ƒ¸A惸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚´Aÿjl9ÿjl9ÿ‚´Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A惸A7ƒ¸Aƒ¸Aƒ¸Aƒ¸A	ƒ¸A¦ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿw“=ÿ_L5ÿ_L5ÿw“=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A¦ƒ¸A	ƒ¸Aƒ¸Aƒ¸Aƒ¸ARƒ¸Aôƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€®@ÿfa8ÿ^H5ÿ^H5ÿfa8ÿ€®@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aôƒ¸ARƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿr…<ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿr…<ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Aqƒ¸Aüƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ}¦?ÿcW7ÿ^H5ÿ^I5ÿ^I5ÿ^H5ÿcW7ÿ}¦?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aüƒ¸Aqƒ¸Aƒ¸Aƒ¸Aƒ¸A(ƒ¸Aڃ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ·Aÿmv:ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿmv:ÿƒ·Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aڃ¸A(ƒ¸Aƒ¸Aƒ¸Aƒ¸A’ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿy›>ÿ`P6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^H5ÿ`P6ÿy›>ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A’ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A@ƒ¸A샸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ²@ÿii9ÿ]G5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ]G5ÿii9ÿ²@ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A샸A@ƒ¸Aƒ¸Aƒ¸Aƒ¸A
ƒ¸A±ƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹AÿuŽ=ÿ_K5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5ÿuŽ=ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸A±ƒ¸A
ƒ¸Aƒ¸Aƒ¸Aƒ¸A]ƒ¸Aøƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ¬@ÿe]7ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿe]7ÿ¬@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aøƒ¸A]ƒ¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸Ã¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿp;ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ü_L5n_L5n^I5ü^I5ÿ^I5ÿ^I5ÿ^I5ÿp;ÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Ã¸Aƒ¸Aƒ¸Aƒ¸Aƒ¸A}ƒ¸Aþƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ|¢?ÿbT6ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ_J5ÁaP6aP6_J5Á^I5ÿ^I5ÿ^I5ÿ^H5ÿbT6ÿ|¢?ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aþƒ¸A}ƒ¸Aƒ¸Aƒ¸Aƒ¸A/ƒ¸AჸAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ‚µAÿlq9ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^I5ó_L5P^H5^H5_L5P^I5ó^I5ÿ^I5ÿ^I5ÿ^H5ÿlq9ÿ‚µAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸AჸA/ƒ¸Aƒ¸Aƒ¸Aœƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿ„ºAÿx–=ÿ`N6ÿ^I5ÿ^I5ÿ^I5ÿ^I5ÿ_K5¤bT7aQ6aQ6bT7_K5¤^I5ÿ^I5ÿ^I5ÿ^I5ÿ`N6ÿx–=ÿ„ºAÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aœƒ¸Aƒ¸AWƒ¸Aðƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¹Aÿ€¯@ÿgd8ÿ^H5ÿ^I5ÿ^I5ÿ^I5ÿ^J5å`M65_K5jf9jf9_K5`M65^J5å^I5ÿ^I5ÿ^I5ÿ^H5ÿgd8ÿ€¯@ÿƒ¹Aÿƒ¸Aÿƒ¸Aÿƒ¸Aÿƒ¸Aðƒ¸AWƒ¸Ašƒ¸Aȃ¸Aƃ¸Aƃ¸AƄºAÆv‘=Æ_K5Æ^I5Æ^I5Æ^I5Æ^I5É_K5uha9bS7bS7ha9_K5u^I5É^I5Æ^I5Æ^I5Æ_K5Æv‘=ƄºAƃ¸Aƃ¸Aƃ¸Aƃ¸Aȃ¸Ašƒ¸Aƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¹A
lr:
\D4
^I5
^I5
^I5
^I5
`M6^H5^H5`M6^I5
^I5
^I5
^I5
\D4
lr9
ƒ¹A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸A
ƒ¸Aÿÿÿÿÿÿÿÿÿþÿÿü?ÿÿü?ÿÿøÿÿøÿÿðÿÿàÿÿàÿÿÀÿÿÀÿÿ€ÿÿÿÿÿþþü?ü?øððààÀÀ€€€ÀÀàÿÿÿÿ
Final code