Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,120 --> 00:00:06,000
In this lecture, we're going to learn how to create washers and computed properties with the Composition
2
00:00:06,000 --> 00:00:06,870
API.
3
00:00:06,900 --> 00:00:11,490
The concepts we learned with the previous API will remain the same.
4
00:00:11,490 --> 00:00:17,940
Washers and computed properties function the same in the composition API as they did in the options
5
00:00:17,940 --> 00:00:18,720
API.
6
00:00:18,810 --> 00:00:20,820
The syntax is just different.
7
00:00:20,850 --> 00:00:27,510
We'll start by learning how to write washers watchers are a way to watch properties on components.
8
00:00:27,510 --> 00:00:32,250
If a property changes, we can run a function to execute further logic.
9
00:00:32,250 --> 00:00:37,380
The watcher will create will reverse a string if an input field is changed.
10
00:00:37,410 --> 00:00:40,260
View has two functions for creating washers.
11
00:00:40,260 --> 00:00:41,790
Let's explore them both.
12
00:00:41,790 --> 00:00:44,910
We'll continue working in the app component file.
13
00:00:44,940 --> 00:00:51,150
In the setup function, we'll create two variables called phrase and reversed phrase.
14
00:00:51,150 --> 00:00:54,630
Both will be set to the ref function with an empty string.
15
00:00:58,450 --> 00:01:01,360
Next, we'll add them to the return statement.
16
00:01:03,720 --> 00:01:09,810
We'll be able to use them in the template below the other paragraph tags will add another one with an
17
00:01:09,810 --> 00:01:10,380
input.
18
00:01:15,840 --> 00:01:20,640
We can bind the input to the data by using the V model directive.
19
00:01:20,670 --> 00:01:24,510
Its expression will be the phrase reference we created.
20
00:01:26,680 --> 00:01:30,490
Below this will want to output the reversed phrase as well.
21
00:01:30,520 --> 00:01:33,970
We'll create a pair of paragraph tags with the expression.
22
00:01:36,170 --> 00:01:41,180
This won't work yet because the reversed phrase variable will never be updated.
23
00:01:41,210 --> 00:01:45,380
We want it to store a copy of the phrase variable, but in reverse.
24
00:01:45,380 --> 00:01:49,580
This issue can be resolved by using a watcher in the script.
25
00:01:49,580 --> 00:01:54,500
BLOCK We'll update the import statement for a function called Watch Effect.
26
00:01:56,460 --> 00:02:00,630
The watch function allows us to watch dependencies in the component.
27
00:02:00,660 --> 00:02:04,350
We'll want to use it to update the reversed phrase variable.
28
00:02:04,350 --> 00:02:09,840
If the phrase variable changes down in the setup function, we'll call the watch function.
29
00:02:11,990 --> 00:02:15,020
The watch function can be used in various ways.
30
00:02:15,050 --> 00:02:20,210
We'll start by learning the most straightforward way of using it, which is to pass in a function.
31
00:02:22,490 --> 00:02:30,230
In the function, we'll set the reversed phrase value property to the following phrase dot value dot
32
00:02:30,230 --> 00:02:30,920
split.
33
00:02:30,920 --> 00:02:32,090
Dot reverse.
34
00:02:32,090 --> 00:02:33,200
Dot join.
35
00:02:35,560 --> 00:02:41,040
This chain of function will reverse the string view, will look at the contents of the function.
36
00:02:41,050 --> 00:02:45,210
It'll notice we're using the phrase variable as a dependency.
37
00:02:45,220 --> 00:02:48,370
It'll start watching the phrase variable for us.
38
00:02:48,370 --> 00:02:51,790
If it ever changes, it'll run the function we passed in.
39
00:02:51,820 --> 00:02:57,970
This will cause the reversed phrase variable to update whenever the user inputs a string.
40
00:02:58,060 --> 00:03:00,640
Let's try testing this out in the browser.
41
00:03:02,260 --> 00:03:05,800
Any time we fill in the input, the variable is reversed.
42
00:03:05,830 --> 00:03:08,630
This approach is one way of using watchers.
43
00:03:08,650 --> 00:03:12,100
Let's explore the second function for creating watchers.
44
00:03:12,460 --> 00:03:17,680
We can make the watcher more efficient by telling it what it should watch instead of letting it guess
45
00:03:17,680 --> 00:03:18,700
what to watch.
46
00:03:21,250 --> 00:03:22,420
Back in the editor.
47
00:03:22,420 --> 00:03:26,740
Let's update the import statement to include a function called watch.
48
00:03:28,860 --> 00:03:33,930
Next, let's replace the watch effect function with the watch function.
49
00:03:36,080 --> 00:03:39,020
The first argument will be different from before.
50
00:03:39,050 --> 00:03:45,830
Instead of passing in a function pass in the phrase variable, this argument will tell the watch function
51
00:03:45,830 --> 00:03:48,230
to only watch the phrase variable.
52
00:03:48,260 --> 00:03:53,870
If the contents of the function has other dependencies, they will not trigger the function to run.
53
00:03:54,110 --> 00:04:00,140
If we decide to tell the watch function what dependencies to watch, we are passed in two arguments
54
00:04:00,140 --> 00:04:01,610
to the callback function.
55
00:04:01,640 --> 00:04:05,720
There the new and old values of the dependency we're watching.
56
00:04:07,400 --> 00:04:11,960
In some scenarios, we'll want to watch multiple dependencies.
57
00:04:11,960 --> 00:04:18,290
We can watch for multiple dependencies by changing the first argument of the watch function to an array,
58
00:04:18,320 --> 00:04:21,950
the array will contain a list of dependencies to watch.
59
00:04:21,950 --> 00:04:25,730
We'll wrap the frames variable with square brackets.
60
00:04:27,970 --> 00:04:34,360
If we decide to launch multiple dependencies, the parameters for the callback function will be arrays
61
00:04:34,360 --> 00:04:35,140
as well.
62
00:04:35,170 --> 00:04:38,920
We'll wrap the parameters with square brackets like so.
63
00:04:41,270 --> 00:04:46,820
This form is probably the best way to use the watch function if you want to be specific on what you
64
00:04:46,820 --> 00:04:47,900
want to watch.
65
00:04:47,900 --> 00:04:50,540
The watcher we created is very simple.
66
00:04:50,630 --> 00:04:56,720
Therefore, all comments output the function and recall the watch effect function with the original
67
00:04:56,720 --> 00:04:57,590
solution.
68
00:05:01,750 --> 00:05:04,050
This is how we can create watchers.
69
00:05:04,060 --> 00:05:06,160
The concept remains the same.
70
00:05:06,190 --> 00:05:08,900
All that's changed is the syntax.
71
00:05:08,920 --> 00:05:12,340
Let's move on to learning about computed properties.
72
00:05:12,580 --> 00:05:18,910
Computed properties function the same in the composition API as it did in the Options API.
73
00:05:19,030 --> 00:05:22,000
They are properties that will store a cached value.
74
00:05:22,030 --> 00:05:27,160
If the dependencies inside the function get updated, the function will be called again.
75
00:05:27,310 --> 00:05:31,540
The property will be updated with the value returned by the function.
76
00:05:31,750 --> 00:05:37,090
They're similar to watchers, but the function for computed properties must return a value.
77
00:05:37,120 --> 00:05:39,700
Let's start writing a computed property.
78
00:05:39,700 --> 00:05:44,290
The computed property will create will double the number in the component.
79
00:05:44,710 --> 00:05:48,970
Computed properties can be created by using the computed function.
80
00:05:48,970 --> 00:05:53,350
We'll need to import it if we want to use it at the top of the script.
81
00:05:53,350 --> 00:05:59,260
BLOCK We'll update the import statement for the view package to include the computed function.
82
00:06:01,520 --> 00:06:06,680
Then below the increment function will create a variable called double.
83
00:06:06,710 --> 00:06:09,200
It'll be sent to the computed function.
84
00:06:11,450 --> 00:06:17,540
The computed function expects a function to be passed in will pass in an arrow function.
85
00:06:19,900 --> 00:06:21,250
The function must return.
86
00:06:21,250 --> 00:06:26,740
A value will return the num dot value property multiplied by two.
87
00:06:29,150 --> 00:06:33,140
The computed and watch functions are similar to one another.
88
00:06:33,170 --> 00:06:36,530
They'll watch the dependencies in the function you passed in.
89
00:06:36,560 --> 00:06:42,610
One big difference between the two is the computed function will return a reactive reference.
90
00:06:42,620 --> 00:06:47,900
We'll be able to use the reference in the template if we decide to add it to the return statement.
91
00:06:47,900 --> 00:06:49,730
In fact, let's do that.
92
00:06:52,120 --> 00:06:53,830
Something to keep in mind.
93
00:06:53,830 --> 00:07:00,220
Since the computed function returns a reactive reference, the value of the computed property is hidden
94
00:07:00,220 --> 00:07:02,140
inside the value property.
95
00:07:02,380 --> 00:07:08,620
If we're returning the computed property view will do what it does for other reactive references.
96
00:07:08,650 --> 00:07:13,750
It will unwrap the reference to allow you to access the value directly through the name.
97
00:07:14,080 --> 00:07:18,790
Inside the template, we'll add a paragraph tag below the numb expression.
98
00:07:18,790 --> 00:07:23,590
Inside it we'll have an expression that outputs the double computed property.
99
00:07:25,920 --> 00:07:28,620
Let's switch to the browser to view the result.
100
00:07:30,860 --> 00:07:36,830
Every time we press the increment function, the double computed property will be double the number.
101
00:07:36,860 --> 00:07:40,790
The computed function will notice we're using the num variable.
102
00:07:40,820 --> 00:07:45,980
It will keep track of it to know when to run the function we passed in the computed function.
103
00:07:46,370 --> 00:07:50,120
We're finished with learning about watchers and computed properties.
104
00:07:50,120 --> 00:07:54,260
They allow us to watch the dependencies and variables in a component.
105
00:07:54,290 --> 00:08:00,800
The main difference between the two is the computed function will return a reactive reference, whereas
106
00:08:00,800 --> 00:08:02,720
the watch function will not.
10641
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.