Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,270 --> 00:00:01,290
In this lecture.
2
00:00:01,290 --> 00:00:04,260
We're going to get started with the composition API.
3
00:00:04,290 --> 00:00:08,800
Before you start, I recommend downloading the starter files in this lecture.
4
00:00:08,820 --> 00:00:11,110
Upload it anywhere on your machine.
5
00:00:11,130 --> 00:00:13,860
After uploading it, run the NPM.
6
00:00:13,860 --> 00:00:15,210
Install commands.
7
00:00:15,240 --> 00:00:18,780
After installing the packages, you can start the server.
8
00:00:18,810 --> 00:00:21,690
Pause this video if you need to set this up.
9
00:00:22,960 --> 00:00:27,340
The files in this project are the default files in the default preset.
10
00:00:27,370 --> 00:00:30,360
I've made changes to the app component file.
11
00:00:30,370 --> 00:00:34,270
It's been stripped down to an empty object and a basic template.
12
00:00:34,300 --> 00:00:39,310
We'll be keeping things reasonably minimal to focus on the composition API.
13
00:00:39,850 --> 00:00:45,190
The very first thing we'll need to do is add the setup function to the options object.
14
00:00:47,490 --> 00:00:52,260
This function is where we must create our options with the composition API.
15
00:00:52,290 --> 00:00:57,720
This is the first difference between the Options API and Composition API.
16
00:00:57,750 --> 00:01:02,700
Previously, we had to create our options in specific properties.
17
00:01:02,730 --> 00:01:08,340
The composition takes a different approach by allowing you to add options inside this function.
18
00:01:08,370 --> 00:01:14,160
This approach gives you more freedom because you can arrange your options in any order you'd like.
19
00:01:14,550 --> 00:01:20,850
The first thing we'll create is some data inside this function defining a variable called number.
20
00:01:20,880 --> 00:01:22,680
Its value will be zero.
21
00:01:24,910 --> 00:01:28,270
We want this variable to be accessible in the template.
22
00:01:28,300 --> 00:01:35,080
The setup function must return an object of properties and methods we wish to expose to the components.
23
00:01:35,080 --> 00:01:39,280
Template will return an object with the number variable.
24
00:01:41,610 --> 00:01:47,700
The number of variables will be exposed to the template will modify the template to output the number
25
00:01:47,700 --> 00:01:50,310
variable inside the div tags.
26
00:01:50,310 --> 00:01:53,820
Replace the content with a pair of paragraph tags.
27
00:01:56,050 --> 00:01:59,230
We'll add an expression for the number variable.
28
00:02:01,470 --> 00:02:03,930
Let's reload the page in the browser.
29
00:02:06,390 --> 00:02:09,090
The number gets rendered onto the document.
30
00:02:09,090 --> 00:02:15,210
Creating variables in the setup function is the equivalent of defining data properties in the options
31
00:02:15,210 --> 00:02:16,080
API.
32
00:02:16,260 --> 00:02:21,870
If we want the data to be available in the template, we'll need to return it from the setup function.
33
00:02:22,160 --> 00:02:26,260
If you will check if you have the setup function defined in a component.
34
00:02:26,280 --> 00:02:30,180
If it is, it will be called when the component is created.
35
00:02:30,660 --> 00:02:31,980
The number by itself.
36
00:02:31,980 --> 00:02:33,360
Isn't that interesting?
37
00:02:33,360 --> 00:02:38,760
Let's make the application more interactive by adding a button that will increment the number.
38
00:02:38,790 --> 00:02:40,740
Let's move back to the editor.
39
00:02:40,740 --> 00:02:45,450
Inside the setup function, we'll define a function called increments.
40
00:02:47,870 --> 00:02:53,600
Inside the function, we'll increment the number variable with the increment operator.
41
00:02:55,990 --> 00:03:01,800
We want the function to be exposed to the template because we want to call it when a button is pressed,
42
00:03:01,810 --> 00:03:04,390
we can do the same thing we did last time.
43
00:03:04,390 --> 00:03:07,600
We can add it to the object returned by the function.
44
00:03:10,080 --> 00:03:15,720
This is the equivalent of defining a function in the methods object with the options API.
45
00:03:15,750 --> 00:03:18,450
The method will be exposed to the template.
46
00:03:18,480 --> 00:03:22,320
We can call it like we would any other method in the template.
47
00:03:22,320 --> 00:03:24,900
We'll add a button below the expression.
48
00:03:27,330 --> 00:03:32,270
On the button, we'll add the Click Event listener with the Prevent Modifier.
49
00:03:32,280 --> 00:03:36,300
If the Click Event is emitted, we'll call the increment function.
50
00:03:38,900 --> 00:03:41,530
Time to give things a test back in the browser.
51
00:03:41,540 --> 00:03:43,070
Try to click the button.
52
00:03:45,230 --> 00:03:46,460
Nothing happens.
53
00:03:46,460 --> 00:03:49,460
The template does not reflect the variables value.
54
00:03:49,490 --> 00:03:51,150
Why is this the case?
55
00:03:51,170 --> 00:03:55,460
If we look at the console for more information, nothing happens here either.
56
00:03:55,500 --> 00:03:59,900
You will not update the template because the variable is not reactive.
57
00:04:01,970 --> 00:04:05,800
We learned about reactivity in the second section of this course.
58
00:04:05,810 --> 00:04:08,420
Let me recap what reactivity is.
59
00:04:08,450 --> 00:04:11,570
Reactivity is the process of updating the template.
60
00:04:11,570 --> 00:04:17,630
Whenever a change occurs in the data, the properties in the data object are reactive.
61
00:04:17,660 --> 00:04:20,390
We will take the time to make your data reactive.
62
00:04:20,390 --> 00:04:26,030
When the component was initialized, you didn't have to perform this part of the process yourself.
63
00:04:26,360 --> 00:04:29,120
Things are different with the composition API.
64
00:04:29,150 --> 00:04:35,420
We are the ones responsible for telling view which variables in the set of functions should be reactive.
65
00:04:35,720 --> 00:04:38,630
This is what's called reactive reference.
66
00:04:38,630 --> 00:04:42,830
A reactive reference is a variable that you will keep track of.
67
00:04:42,830 --> 00:04:46,760
Any changes to the variable will be reflected in the template.
68
00:04:46,790 --> 00:04:49,280
It adds reactivity to the variable.
69
00:04:51,350 --> 00:04:56,180
We can create a reactive reference by using a function called ref.
70
00:04:56,210 --> 00:05:01,880
At the top of the script block, we're going to import the ref function from view.
71
00:05:06,510 --> 00:05:13,440
The ref function will wrap a value with reactivity back in the setup function will ramp the value for
72
00:05:13,440 --> 00:05:16,260
the number variable with the ref function.
73
00:05:18,500 --> 00:05:22,220
We'll need to make one modification to the increment function.
74
00:05:22,520 --> 00:05:28,340
Instead of accessing the number variable directly, we'll access a property called value.
75
00:05:30,660 --> 00:05:33,600
The ref function returns an object.
76
00:05:33,630 --> 00:05:39,060
If we want to make changes to the value, we'll need to access it via the value property.
77
00:05:39,090 --> 00:05:42,030
This is only for when we want to make changes to it.
78
00:05:42,060 --> 00:05:44,550
When we return, the reactive reference.
79
00:05:44,550 --> 00:05:48,530
We need to return the entire variable in the template.
80
00:05:48,540 --> 00:05:49,800
We don't have to update.
81
00:05:49,800 --> 00:05:56,430
The expression behind the scenes view will detect that the number variable is a reactive reference.
82
00:05:56,430 --> 00:06:01,170
When it finds a reactive reference, it will automatically expose the inner value.
83
00:06:01,200 --> 00:06:05,640
We don't need to access the references value via the value property.
84
00:06:05,640 --> 00:06:07,920
The template can remain unchanged.
85
00:06:08,070 --> 00:06:14,310
One last thing before we test this, we're going to log the number variable to check out what the ref
86
00:06:14,310 --> 00:06:15,600
function returns.
87
00:06:18,000 --> 00:06:20,970
All right, let's give things a test in the browser.
88
00:06:21,000 --> 00:06:22,890
Try clicking on the button again.
89
00:06:25,220 --> 00:06:28,030
The expression in the template gets updated.
90
00:06:28,040 --> 00:06:28,920
This is great.
91
00:06:28,940 --> 00:06:31,100
It's exactly what we were looking for.
92
00:06:31,130 --> 00:06:35,510
Open the console to view the object returned by the ref function.
93
00:06:37,840 --> 00:06:40,510
The object comes with a getter and setter.
94
00:06:40,540 --> 00:06:43,720
This is how you can update the value and retrieve it.
95
00:06:43,750 --> 00:06:48,880
It's important we use the ref function if we want our variables to be reactive.
96
00:06:49,070 --> 00:06:53,380
It'll wrap the value with functions necessary for making it reactive.
97
00:06:53,410 --> 00:06:58,150
Back in the editor, I'm going to remove the long statement from the function.
98
00:07:00,400 --> 00:07:03,720
We've successfully created some data for our component.
99
00:07:03,730 --> 00:07:08,320
We'll look at what else we can do with the composition API in the next lecture.
9661
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.