Introduction to Vue Project Structure
In the last video, we saw how simple it is to create and run a basic Vue application using the Vue CLI. Now, it's also really important that you understand the files and the folders present in the generated project and how the control flows when you run the application. Let's take a look at that in this video.
Root Level Files Explained
I have opened the hello world project in VS code and you can see that at the root level we have three folders and five files. To begin with, let's start with the important bits in package.json.
This file contains the dependencies and the scripts required for the project. You can see that we are using Vue version 3 and that is listed as a dependency. You can also see that there are a few Vue CLI and eslint dev dependencies. We also have three scripts: serve to run the application, build to build the application, and lint to simply fix any linting errors in the app. The configuration for eslint is present as well. A straightforward package.json file as you can see.
Next, we have the yarn lock file. Based on whether you prefer npm or yarn as a package manager, you're going to see yarn lock or package lock files. They simply ensure consistent installation of your dependencies, and you don't really have to worry much about them. We also have a git ignore and a readme file.
The last file at the top level is babel.config.js, which is the babel configuration file. Babel is a tool which transforms modern javascript features being used in development code into older syntax that is more cross-browser compatible in production code. The configuration file here has just the one entry for the Vue CLI babel preset.
Exploring the node_modules and public Folders
All right, next let's talk about the folders. The first one is node_modules. This is the folder in which all the dependencies are installed. It is generated when you run the vue create command or when you run npm install.
The next folder is the public folder. This folder contains static assets that are published when you want to go live with your application. It contains two files. We have the fav icon, which you see in the browser tab and is nothing Vue specific. So as a beginner you only have to concentrate on the index.html file.
The index.html is the only HTML file you're going to have in your application. We are building single page applications, and this is it. The view might dynamically change in the browser, but it is this HTML file that gets served. Typically you're not going to add any code in this file, maybe some changes in the head tag, but definitely not in the body tag. You want Vue to control the UI and for that purpose, we have one div tag with id is equal to app. At runtime, the Vue application takes over this div tag and is ultimately responsible for the UI. Please make a note of this div as we will come back to it shortly. Let me also quickly increase the font size.
The src Folder: Components and Entry Point
All right, that is about the public folder. The next folder is the source folder, which is the folder we will be working with the most during development. The starting point for our Vue application is main.js.
In main.js, we specify the root component, which is App component, and the DOM element which will be controlled by Vue. The DOM element in our example is an element with id attribute equal to app. And if you can recollect, that is the element in our index.html file. We call this div element as a root DOM node because everything inside it will be managed by Vue. For the hello world application, the App component is rendered inside the root DOM node.
That brings us to the App component, which is present in App.vue. Now, the .vue file extension, I bet is something new to you if you're learning about Vue for the very first time. We will talk more about .vue files in the next video, but for now let me tell you that it is a file where you specify HTML, CSS, and JavaScript corresponding to a portion of the UI you see in the browser. You can see here we have an image which corresponds to the Vue logo you see in the browser, and then we have this custom HTML tag called HelloWorld. This HelloWorld is just another .vue file which contains more HTML, CSS, and JavaScript. This file is present in the components folder. It contains a bunch of HTML links which you again see in the browser.
We call these .vue files as components. Components is a topic which we will deal with in great detail later on in the series, but for now, you just have to know that we have two components: App component and HelloWorld component. The two components represent the view which we see in the browser. Now within the source folder, we also have an assets folder which contains the Vue logo that you see in the browser. The folder can be used to store images, SVGs, etc., as the name indicates.
Understanding the Application Control Flow
So that is about the folder structure of a Vue application created using Vue CLI. Next, let's understand the control flow when you run this application in the terminal.
When you run the command yarn serve, index.html file is served in the browser. index.html contains the root DOM node.
Next, the control enters main.js. Here, the createApp function imported from the Vue library will mount the App component onto the DOM node with id attribute equal to app, which is the div tag in our index.html file. The App component contains its own HTML and the HTML from the HelloWorld component, which is ultimately displayed in the browser. And the yarn serve command that you run in the terminal, of course, corresponds to this particular script.
What's Next: .vue Files
Now then, in our source folder, we see this new file type which is the .vue file type. But what exactly is such a file? Let's understand more about it in the next video. Thank you guys for watching. Don't forget to subscribe. I'll see you guys in the next one.