Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,980 --> 00:00:07,610
In this lecture, we're going to learn how to use props in the composition API props haven't changed
2
00:00:07,610 --> 00:00:09,920
that much in the composition API.
3
00:00:10,280 --> 00:00:13,240
The composition API doesn't replace everything.
4
00:00:13,610 --> 00:00:16,600
There are still some options we can define in the component.
5
00:00:17,180 --> 00:00:18,290
Here's what we'll do.
6
00:00:18,500 --> 00:00:21,890
We'll create a component that will output an alert message.
7
00:00:21,890 --> 00:00:27,650
If the name of the user changes, we created a timeout function that will change the name of the user.
8
00:00:28,010 --> 00:00:34,640
It would be nice if we can get alerted of when the change occurs from the parent component in the components
9
00:00:34,640 --> 00:00:35,390
directory.
10
00:00:35,540 --> 00:00:38,180
Create a file called Alert View.
11
00:00:40,690 --> 00:00:44,200
Will scaffold the template and script blocks.
12
00:00:46,570 --> 00:00:52,660
In the template, block will add an empty div tag to prevent you from throwing an error during compilation.
13
00:00:55,190 --> 00:01:00,980
Great, we can import the component into the app component back in the app component file.
14
00:01:01,010 --> 00:01:02,810
Import the alert component.
15
00:01:08,290 --> 00:01:14,020
We can register the component by using the components option, we'll add it to the component with the
16
00:01:14,020 --> 00:01:15,790
alert component passed in.
17
00:01:20,730 --> 00:01:27,120
The composition API doesn't completely replace the options API, there are still some options we can
18
00:01:27,120 --> 00:01:29,580
use for things like registering the component.
19
00:01:30,060 --> 00:01:37,320
The composition API provides functions for creating data methods, computed properties, watchers and
20
00:01:37,320 --> 00:01:38,650
lifecycle functions.
21
00:01:38,970 --> 00:01:41,550
Everything else is still performed with the options.
22
00:01:41,550 --> 00:01:47,760
API will load the component in the template, will place it below the other elements.
23
00:01:50,310 --> 00:01:56,490
The alert message, the component will output will be based on if the user's name changes, we'll need
24
00:01:56,490 --> 00:02:02,290
to provide it with the user object will bindu property onto the component called user.
25
00:02:02,520 --> 00:02:04,860
Its value will be the user object.
26
00:02:07,410 --> 00:02:12,520
This finding will throw an error because the setup function is not returning the user variable.
27
00:02:12,870 --> 00:02:19,050
It won't be accessible in the template, will update the return statement to include the user variable.
28
00:02:21,750 --> 00:02:27,660
Time to work on the alert component now that we have everything we need from the parent component back
29
00:02:27,660 --> 00:02:29,470
in the alert component file.
30
00:02:29,580 --> 00:02:34,410
We're going to accept the user object by adding the prompts option to the component.
31
00:02:37,130 --> 00:02:41,840
The props option is another option that isn't available in the composition API.
32
00:02:42,260 --> 00:02:46,460
We still need to use the options API if we want to accept properties.
33
00:02:47,000 --> 00:02:51,800
The next thing we'll do is use the user prop to create a computer property.
34
00:02:52,310 --> 00:02:59,540
It'll check if the user has changed since will be creating a computed property, will import the computed
35
00:02:59,540 --> 00:03:01,320
function from the View package.
36
00:03:01,820 --> 00:03:04,970
Next, we'll add the setup function to the component.
37
00:03:10,410 --> 00:03:16,740
Afterward, we'll define a variable called flag, its value will be the computed function with an arrow
38
00:03:16,740 --> 00:03:17,790
function Pastan.
39
00:03:20,390 --> 00:03:26,870
Inside the function will want to use the prop, however, here's the problem the setup function runs
40
00:03:26,870 --> 00:03:32,360
before the components, props, data, methods and computed properties are registered.
41
00:03:32,750 --> 00:03:35,490
It runs before any of the lifecycle functions.
42
00:03:35,810 --> 00:03:37,610
We don't have access to anything.
43
00:03:38,030 --> 00:03:41,340
The this keyword won't work in the setup function.
44
00:03:41,780 --> 00:03:47,310
This is a problem because we want to be able to use the props passed down from the parent component.
45
00:03:47,960 --> 00:03:52,060
Luckily, the setup has an optional parameter called props.
46
00:03:52,370 --> 00:03:55,040
We'll add the props parameter to the function.
47
00:03:57,760 --> 00:04:02,740
This parameter will give us access to the props passed down from parent components.
48
00:04:02,980 --> 00:04:07,990
In addition, the second parameter to the setup function is called context.
49
00:04:10,620 --> 00:04:16,230
This parameter will give us access to the other properties and methods in the component, you can think
50
00:04:16,230 --> 00:04:18,740
of it as the equivalent to the this keyword.
51
00:04:19,140 --> 00:04:25,110
We won't be using it for this example, so we won't add it to the function in the callback function
52
00:04:25,110 --> 00:04:27,510
for the computed property will return.
53
00:04:27,510 --> 00:04:29,310
The following prompts.
54
00:04:29,310 --> 00:04:33,360
That user name equals, equals, equals John.
55
00:04:36,000 --> 00:04:42,260
This value is the default value for the main property in the object, if this turns out to be false,
56
00:04:42,270 --> 00:04:44,010
will want to output a message.
57
00:04:44,340 --> 00:04:45,780
We'll handle this in a moment.
58
00:04:46,080 --> 00:04:51,930
Before we do, we'll need to return the flag variable for it to be exposed to the template.
59
00:04:54,620 --> 00:05:00,890
The last thing we'll need to do is add the message on the div tag, we'll add the V if directive.
60
00:05:03,530 --> 00:05:07,120
The condition will check if the flag variable is false.
61
00:05:09,700 --> 00:05:16,210
If it is, we'll display the contents of the elements inside the element, we'll add some text to let
62
00:05:16,210 --> 00:05:18,280
the user know the name has changed.
63
00:05:20,790 --> 00:05:26,850
To make it more noticeable, we'll change the color of the text to red by using the style attribute.
64
00:05:29,410 --> 00:05:32,980
Let's try running things in the browser, refresh the page.
65
00:05:35,420 --> 00:05:40,910
After three seconds, the name will change because we have the time function running, the message will
66
00:05:40,910 --> 00:05:45,780
appear because the parent component sends the updated value to the alert component.
67
00:05:46,160 --> 00:05:52,040
This will cause the function in the computer property to run the main take away from this lecture as
68
00:05:52,040 --> 00:05:54,570
how we're using props in the child component.
69
00:05:54,920 --> 00:05:59,610
It's slightly different because we don't have access to props through the this keyword.
70
00:05:59,990 --> 00:06:04,950
We have to use the prompts parameter in these set of function to be able to use props.
71
00:06:05,210 --> 00:06:08,120
It's a different approach that you'll need to be aware of.
7550
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.