Introduction to this tutorial
Hey gang, welcome to your 18th Vue.js tutorial. And in this video, I want to walk you through Vue files and the root component.
Project structure and the src folder
Okay, so in the last tutorial, we used the Vue CLI to create this brand new project right here. And I said all the meaty stuff was in this source folder. So when we run this in a browser, we see this. This is kind of like the baseline project that Vue.js provides us with when we install a project using the Vue CLI. Now, this is just to show you how things work basically. So, all this stuff in the source folder, this meaty stuff is what we're going to be editing throughout most of this series. And the rest of this we can just kind of leave as is, right? So, first of all, we have this assets folder right here, which contains a logo in it at the minute, just an image. So, any kind of images that we want to use, we can place in this assets folder and reference them later on. Secondly, we have this main.js file. Now, this is the file that kicks in when we run this application. So, this kind of controls everything from the start. So, first of all, we have these two import statements at the top. Now, we can have these because we can now use ES6 syntax. So, new JavaScript such as this import statement. So we can use these ES6 features because we have this Webpack setup which is using Babel somewhere along the line to transpile ES6 code into vanilla JavaScript which is readily understandable in browsers.
main.js and rendering the root component
Okay, so we're importing the Vue library first of all which means we can use Vue in this file and then secondly we're importing this app from App.vue. Now, this app is this thing right here on the left, and I'm going to go through that in a second. But basically, what we're doing is creating a new Vue instance just like we've done from the beginning. So, this new Vue instance is looking for this thing right here, this app, right? And if we go to the index file, this is the file which is served up by the server initially. Okay? And you can see that right here, this app ID. So, this main.js is creating a Vue instance now which is going to control this entire section here. And the way this works is that all of our application code is going to eventually be output in between these two things here. They don't have to be. You can output whatever static content you want outside of this, but all of our kind of Vue code is going to be rendered in here. Our Vue template code, right?
Understanding App.vue as the root component
So basically we have this initial Vue instance right here. Right. And what we're doing is rendering some content. Now, this render we've not seen yet. So, when we're working like this with different Vue files, which I'm going to go through in a second, we tend to use this approach where we use this render method right here. And you don't really need to understand what this h => h function is. Basically all this is doing is taking our root component right here, this app which we import right here. So this is the root component that it's importing and what it's doing is it's rendering this to this. Right? So this right here, this App.vue, this is our root component. And I'm going to go through all of this in a second. But just understand this is our root component, the component that lives at the very top of our application. And we're going to be able to nest components later on in the series. And I'm going to go through that in the next tutorial. But understand for now, this is the very top kind of parent root component, right?
Anatomy of a .vue file: template, script, style
So all of this template right here, all of this code, right? And this functionality here, the root component, all of this is being rendered now because we say in the main.js, render all of this app. We render all of that template in the index right here. Okay. So once again, we've got this root Vue instance here, which is controlling the app. Then we're rendering this root component, this App.vue, which we import right here. We're importing this App.vue and we're rendering that root component right here to this thing here in the index.html file. Okay. So that is why when we view it in the browser, we see all of that HTML from the Vue file from that root component, we see it in the index file, right? So all of this here, not all of this, sorry, all of this here. Cool. So this root component, this is a Vue file. And as we go further into this series, we're going to create various different Vue files, okay? And they'll sit within this root component. But basically, a Vue file is just an extension of a component. And remember, we talked about components a few tutorials ago where we can create reusable template and functionality code which can be used in different parts of an application. Right? That's all this is except this time this component has its own file and that is a .vue file. It's dot Vue. Okay. So this is called App.vue. So this root component has a template right here. And when we registered components previously we added that as a property something like this. If we say like Vue.component and then you know the name and then we took an object we said template and then we passed in some HTML code in here like this. This is the same kind of thing except this time what we're doing is we have our template here in template tags at the top of this component file this Vue file. Okay. So this is where we create our template for this component. So this is all HTML and inside this template we can access data from this object. So this is the component as well. So this is in a script tag, right? Because this is a Vue file, not a script file, a JS file. We have to add a script tag. And what we're doing is exporting an object. So what we're doing now is exporting this object. And this object right here kind of represents that object that we passed through when we created a new component in here a minute ago, when we said Vue.component and then the second parameter was an object. That is this thing right here. Okay. So, we have a couple more things on here. We have the name right here. This name is the name of the component. And later on when we start nesting components, we're going to use this a bit more. We also have this data right here. Now, remember I said when we use components, we have to use a function for data. But I didn't write it like this. I did something like this data: function. Okay. Now this right here is exactly the same thing. This is ES6 code in action. So instead of writing data colon function, we can just do data with the parenthesis on and that is exactly the same. So again we're returning our data object right here like this. And then finally, we have this style tag. And this controls the style, the CSS of this component right here.
Live edit: create your first view file
So, just to kind of demonstrate what we can do here, I'm just going to delete a lot of this out because we don't want the default kind of content they provide us with. We want to make our own content, right? So, let's delete all that crap. And also, I will delete this message right here. We don't want a message. The name we don't need just yet. So we can delete that as well. I want to keep this as simple as possible. And up here, what I want to do is get rid of all of this. We don't need to really give this an id of app either. So we can just have a div. Now in a template, we do have to have a single element surrounding the whole thing. So we can't write div and then another div down here with something else in it. This won't work, right? We have to have everything within one kind of root element. So, we have our div. Now, let's create an h1. And we're going to output a title in there. We've not created this just yet. Underneath the h1, let's have a p tag. And then in here, we're going to have a method, which is going to be a greeting. So, we're doing the same kind of stuff as we did in the earlier tutorials when we didn't have this development workflow and using this the Vue CLI, but now we're just doing it in a separate view file, okay? A separate component file, but all the behavior is still the same. So we have our title which we can add in the data right here. So I'm going to say title and this is going to be "your first view file." Woo. Okay. And then we want a method as well. So we'll come after the data right here. And we'll say methods. And this is going to take an object. And in here we need a method called greeting. And this is going to be a function. Inside this function, I want to greet. So I'm just going to say return and a string. Something we can output here in between the p tags. And I'm going to say, "Hey, cowboy." Okay. So that is what we're returning there. Just going to scoot this in a little bit. And if we save this now and view it in a browser, we can now see "your first view file." Woo! Hey cowboy. So this is now working. Very cool. So we have created our first view file which is the root component which is outputting then to the browser because we're importing this root component into this main.js file which fires up to begin with and we're rendering this root app component right here to this app ID which is in the index file. So, I implore you to take a few minutes just to kind of wander around these files, see where all the moving parts are going. Like I've explained, just get your head around it before you move on to the next tutorial.
Outro / End of video
next tutorial.