I'm having trouble figuring out how to have multiple pages in a Vue CLI project. Right now I have my home page with a few components and I want to create another page but I do not know how to do that. Am I supposed to create multiple html files where the index.html by default is? In a simple file structure with css js img folder and html files as pages I know that creating another html file means making another page. But I don't understand how this works with Vue CLI project.
I saw stuff like vue-router and "pages" in Vue documentation but I do not understand them very well. What are my alternatives? Is there a guide that explains that in detail, because I wasn't able to find any, let alone detailed. Would be very happy if you could help! Thank you!
This may not be relevant to the question, but bear with me, maybe my answer can help someone. I use webpack+vue, and I have figured out how to build multiple pages applications. Here my webpack.config.js:
And here's my directory structure:
https://i.stack.imgur.com/uFvKx.png
And you can jump pages:
First: always read the official documentation. With Vue , you can build a SPA, as well: a MPA it no problem. Just follow the guide:
Using Vue CLI 3, you can create a new project with vue create youProject, and set to manually configure it. Then, don't choose the SPA option. Vue will create a nice "start" project using a MPA approach. After that, just repeat the config on vue.config.js.
Updated #1
It seems that some updates on Vue Cli, have changed the way to build a MPA app, so:
vue create test
The boilerplate created, will be a SPA one. But, make this changes:
Create a folder under
src
namedpages
(optional)Into this folder create your own pages: Home, About, etc.
App.vue
into this folders, at your likeBelow, I have three image demonstrating this:
You don't need to create the
pages
folder, just get the idea.Link to GitHub: Building a MPA App
Note pointing users to what should be the accepted answer
At the moment of posting my initial answer I wasn't aware of the possibility of actually building MPAs in VueJS. My answer doesn't address the question asked therefore I will recommend to take a look at the answer provided by PJ.Wanderson bellow which should be the accepted answer
Inital Answer
Vue.js projects are SPAs(single page applications). You only have one
.html
file in the entire project which is theindex.html
file you mentioned. The "pages" you want to create, in vue.js are referred to as components. They will be plugged into theindex.html
file and rendered in the browser. A vue.js component comprises 3 parts:You can check this tutorial out for a quick-start Vue.js 2 Quickstart Tutorial 2017
It explains vue.js project structure and how the various files relate to each other
One easy solution
EDIT: Vue has this built-in. Skip to the bottom for more.
Original answer:
There are two ways to interpret your question, and therefore to answer it.
The first interpretation is: "how can I support routing to different pages within the same single-page app, e.g. localhost:8080/about and localhost:8080/report etc?". The answer to this is to use the router. It's reasonably straightforward and works well.
The second interpretation is: "my app is complex, and I have multiple single-page applications, e.g. one app for the 'website' part, one app for consumers to log in and do work, one app for admins, etc - how can vue do this, without making three entirely separate repositories?"
The answer to the latter is a single repository with multiple single-page apps. This demo looks like exactly what you're after:
https://github.com/Plortinus/vue-multiple-pages/
Look in particular at: https://github.com/Plortinus/vue-multiple-pages/blob/master/vue.config.js
Updated answer:
It turns out that vuejs has the idea of multiple top-level pages built-in. I mean, it makes sense - it's going to be really common, despite what many incorrect answers are saying about "no, it's for single page apps"!
You want the
pages
option in thevue.config.js
file:https://cli.vuejs.org/config/#pages
If your project doesn't have that file in the root directory, create it and vuejs will discover it.
There is a long and a short way to define each page. I used the short form here:
You don't have to put your work under "pages". It could be "/src/apps/index/index.ts" or whatever.
After moving code around and changing some imports from:
to
The app works - but the "experiment" app in my repo had to be loaded like this:
Pretty ugly, and even worse because it uses the router which resulted in URLs like:
Ugh.
Fortunately, this stackoverflow answer solved it. Update the
vue.config.js
file to includedevServer
options (make sure this is at the top level of the exported object:Then also modify the
router.ts
file to append the extra path (in my case "experiment/":Then URLs resolve nicely, e.g.: http://localhost:8080/experiment/about