Routing With Examples

Vue JS Fundamentals for Beginners Navigation And Vue Router
13 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.59
List Price:  €93.70
You save:  €28.11
£56.03
List Price:  £80.05
You save:  £24.01
CA$96.39
List Price:  CA$137.71
You save:  CA$41.31
A$107.98
List Price:  A$154.26
You save:  A$46.28
S$95.50
List Price:  S$136.44
You save:  S$40.93
HK$547.57
List Price:  HK$782.28
You save:  HK$234.70
CHF 64.43
List Price:  CHF 92.05
You save:  CHF 27.61
NOK kr776.82
List Price:  NOK kr1,109.79
You save:  NOK kr332.97
DKK kr489.21
List Price:  DKK kr698.91
You save:  DKK kr209.69
NZ$118.81
List Price:  NZ$169.74
You save:  NZ$50.92
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,729.18
List Price:  ৳11,042.16
You save:  ৳3,312.97
₹5,842.93
List Price:  ₹8,347.41
You save:  ₹2,504.47
RM333.67
List Price:  RM476.70
You save:  RM143.02
₦92,745.84
List Price:  ₦132,499.74
You save:  ₦39,753.90
₨19,599.98
List Price:  ₨28,001.17
You save:  ₨8,401.19
฿2,600.12
List Price:  ฿3,714.62
You save:  ฿1,114.50
₺2,271.31
List Price:  ₺3,244.87
You save:  ₺973.56
B$363.64
List Price:  B$519.51
You save:  B$155.87
R1,308.28
List Price:  R1,869.05
You save:  R560.77
Лв128.30
List Price:  Лв183.29
You save:  Лв54.99
₩96,996.60
List Price:  ₩138,572.52
You save:  ₩41,575.91
₪261.41
List Price:  ₪373.46
You save:  ₪112.04
₱4,045.59
List Price:  ₱5,779.67
You save:  ₱1,734.07
¥11,052.75
List Price:  ¥15,790.32
You save:  ¥4,737.57
MX$1,197.81
List Price:  MX$1,711.23
You save:  MX$513.42
QR257.31
List Price:  QR367.61
You save:  QR110.29
P1,000.11
List Price:  P1,428.79
You save:  P428.68
KSh9,335.98
List Price:  KSh13,337.69
You save:  KSh4,001.70
E£3,349.95
List Price:  E£4,785.86
You save:  E£1,435.90
ብር4,013.63
List Price:  ብር5,734.01
You save:  ብር1,720.37
Kz58,409.45
List Price:  Kz83,445.65
You save:  Kz25,036.20
CLP$67,298.07
List Price:  CLP$96,144.23
You save:  CLP$28,846.15
CN¥506.79
List Price:  CN¥724.02
You save:  CN¥217.23
RD$4,121.81
List Price:  RD$5,888.55
You save:  RD$1,766.74
DA9,423.87
List Price:  DA13,463.25
You save:  DA4,039.38
FJ$159.96
List Price:  FJ$228.52
You save:  FJ$68.56
Q547.38
List Price:  Q782
You save:  Q234.62
GY$14,742.16
List Price:  GY$21,061.13
You save:  GY$6,318.97
ISK kr9,832.89
List Price:  ISK kr14,047.59
You save:  ISK kr4,214.70
DH712.24
List Price:  DH1,017.53
You save:  DH305.29
L1,243.01
List Price:  L1,775.81
You save:  L532.79
ден4,046.68
List Price:  ден5,781.23
You save:  ден1,734.54
MOP$564.08
List Price:  MOP$805.87
You save:  MOP$241.78
N$1,315.96
List Price:  N$1,880.02
You save:  N$564.06
C$2,592.36
List Price:  C$3,703.54
You save:  C$1,111.17
रु9,349.94
List Price:  रु13,357.63
You save:  रु4,007.69
S/263.70
List Price:  S/376.73
You save:  S/113.03
K271.96
List Price:  K388.53
You save:  K116.57
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,886.12
List Price:  ZK2,694.57
You save:  ZK808.45
L326.39
List Price:  L466.29
You save:  L139.90
Kč1,650.20
List Price:  Kč2,357.53
You save:  Kč707.33
Ft25,629.21
List Price:  Ft36,614.73
You save:  Ft10,985.52
SEK kr770.71
List Price:  SEK kr1,101.06
You save:  SEK kr330.35
ARS$61,698.11
List Price:  ARS$88,143.94
You save:  ARS$26,445.82
Bs486.86
List Price:  Bs695.54
You save:  Bs208.68
COP$271,471.23
List Price:  COP$387,832.67
You save:  COP$116,361.43
₡35,819.54
List Price:  ₡51,172.97
You save:  ₡15,353.42
L1,740.31
List Price:  L2,486.27
You save:  L745.95
₲526,285.13
List Price:  ₲751,868.13
You save:  ₲225,582.99
$U2,699.91
List Price:  $U3,857.19
You save:  $U1,157.27
zł284.80
List Price:  zł406.88
You save:  zł122.07
Already have an account? Log In

Transcript

Hello, and welcome again. So today we'll do something interesting. So in the last lecture, we looked into routing, right. So let's look at it from an example standpoint. Since routing is a bigger concept, and it's much more difficult to grasp, I'll create an example here. So we have this website called as Jason generator, Jason hyphen, generator.com.

And it generates some dummy data that we can use for our project, in our case, have copied this data, and have created a new file called as data Jason. And inside this data, pasted everything. So it's basically a JavaScript array consisting of Java JavaScript objects. So each object consists of some parameters like ID, index, balance, age, name, gender, etc. Right. So let's see how we can use this.

So if I go to have our app dot view, we have this router link side, let's remove them. We'll leave the router view as it is. Let's save it and in our router.js file. Let's also remove the routes. Alright, so what we'll do is we'll create two more components. One is people don't view And another one is person don't view.

All right, so people collective and person singular. So inside this can see people and into the script, I can import the data. Jason and let's see if we were able to get that. So what we'll do is instead of outward view, we'll set up our route path as the people component. So we can remove these imports and you can see import people. Let's also import the person component and the root part is the people component.

So component. We will and for our person component is a part slash person. component All right. So instead of the people component, we have imported the data. So now, let's just have a small heading tag. I save it.

And if I go back road link will get the people list. So that's the people component being rendered. So let's see if we are able to get the data. So I can create mounted lifecycle hook, and inside mounted. So let's see the content of the data, right. So for this, we can say created.

For this we can say created. Remember, it's a lifecycle hook that we learned in previous lectures. Instead created, we had a console dot log. And I want to output data. So if I save it, and if I go to console, I'm getting an array, and the array consists of six objects. That's fine.

All right. So now we have the people data coming up. All right, so now we have the people data coming in. Let's create a div that says, person or let's say Now we have the people data coming in, what we'll do is we'll create another component that says, person to file. You'll understand in a minute why I'm creating these three components. So for now, what we'll do is instead of the person component, let's say person.

And for person, let's pick three or four fields. So what we'll do is we'll pick the name. Does the person have a name? Yes, he has a name. Let's also pick the age. Let's pick the company and email a number of friends all right.

So, inside this, so let's say this person component is expecting a prop or person is a type of object. Is it required? Yes, it is required. All right, so now we have the prop as person now what we can do is we can say it's a heading Person dot name, his age, his company and email All right. So, one more thing what we can do is we can create we can get the person's friends count. So, for this what we can do is I consider friends and for the person that computer can create a computed property that calculates the friends count for me and against the friends count.

I can return this person northwind Scotland Alright. So now we can use this computed property and count Okay, so now I have the person component ready and inside the people component i can import person wasn't I can register the component by saying components Listen. Alright, so now what we can do is we can have the person component. And we can, we can replicate the person component by using before. person, people. person in data.

Yeah. And this person can also have index. Let's also put a key towards it. index. So it won't ask us for any errors. And let's also give it a give it the value of the prop was this person.

All right. So if I say this, I should see, okay, so we get an error property or method data is not defined. So, all right, so what we can do is we can create our own data property. We can return, we can, we can create an array that says people and Tella created method we can say this dot people equal to data and should be fine. and here we can say post and in people it makes much more sense now. And if I save it, oh, we get a lot more errors.

So the error is in the dot length property. All right, so we are getting an error that says, person friends length is not a function. So in this sort of function, so we can just say length, and we should be fine. Yeah. And if I refresh, it looks good. So these are all the people we have.

So what I want to do is, whenever we click a person, I want this data to go into the person profile component, and we can have the person profile component as its own page, right. So it's called the person profile component, it will be very much similar to person. So you can just copy paste the person component but it will also have More extra trees. For example, it's at the about section. Let's add the board section in top. So I'll just make some changes, and I'll be right back.

So what I did was I added a few more fields I added about. I also added the friends list. So that's the person profile, nothing really fancy. So what we want to do is whenever the person is clicked, right, so we also have a button that says View profile. And what we'll do is for the person name, we'll help each one. Let's also align it to center so we can know which person is and also will have some margin and for the person competent in now, view Profile button, we say add click numerically listener, click View profile.

So we have a method and inside this method what we want to do is this dot router dot push an object it's also add pose and provide to a router router file you know it's easy to forget to add your conference to router. So make sure you remember that or we can do one thing. Instead of having a new route for the slash person you can just say person profile right so we can also remove the person component from here and we can save it and inside the person when we click the View profile, it's also have give it a name. person. I named this person and do we want to pass in data as it executes expecting a prop that's of type object I its name is person, so Listen, is the water What are we going to send us? Again, getting in the context and with the person, we're going to send VM dot person.

All right, if I save it, so just to explain what's going on, so, there is this little component inside which we are entering the list of people. And each person inside the people object is, is having its own component just for the rendering sake. In this component, we are displaying a short amount of data, just kind of like a instance of all the list we have, like you know, Facebook suggestions, you know, these, these could be your friends, right? So similar to that, we have a few suggestions. And upon clicking this component, we are navigating it to the person profile page, which which has a lot more data. Right.

So we have saved it and if I go back, we have the list. Also, we can do one thing Instead of be blessed. All right, so these are the people, we have just a little bit of styling, and we can click the profile and we're not getting any data. So let's see what's happening inside the console. We are getting missing prop required person, right? So what's the reason?

So instead of person component we are sending in person. Okay, so the reason could be that we did not define prompts to be true. So this is also something which people forget usually, just like I did. And if we go back and if I say this, yep, now it's fine. So here we have the person's name and about his age. I guess his name again or company, I guess.

And the email address the number of desktop friends, and the friends names as well. So this is the person profile. So that's a small example of routing. So one thing I want to touch on is that instead of the people component, if you see, instead of writing this code, the person components code like the class person is there, right? So I could have just copied this code. I could have also written it, instead of like having a separate component.

I could have also written here, and I could have added the V for for this one. But the reason why I didn't do it is, you know, this person component is there, right? So this could be used in future in in more pages. I mean, not in our project, but just in case you're building out a bigger website, right. So if you're creating a social media website, there are several pages where you want to show kind of like a person's small miniature profile. So for that, if I have a component ready, and if it expects a certain type of data, I can use it anywhere where it's required, and I could pass it the data and it would be the same, right?

So that's the reason I created a separate component for person. And I save this and everything should be as good as it. So that's one small example of doubters. So I know router is a complex subject. So that's why I also, I thought there were much more practical examples, it would be much easier to learn. So now we have the people competent person component ready.

So what if I want to send someone a link of just one person? So let's say I want to send someone the link of Simpson house. And if I say view profile, I have the link, I have the page for Simpson house. So I want to share share this to a friend. So if I copy this, and if I open it in incognito and paste it, I get nothing. And if I go to the console, it says missing required prop person.

So the reason is, we are getting the data from the people component, which is being passed on to the person component and which in turn passes it to the person profile component. So how do I send someone a link of a particular person? So that's what we'll be looking at the next lecture. All right. That's it for this one. I'll see you the next one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.