Vue Instance and Data Properties

8 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 to your first lecture on Vue js. Like we discussed in the last lecture uJs is very flexible, right. So we can use Vue JS to add a small functionality into your existing website, or we can build your whole complete website around Vue js. So before we go further and build a complete website, or web around uJs, we look into how to establish or integrate Vue JS into an existing website. For example, if you already have an HTML website, and you want to add Vue JS onto it, you can do that as so. So now let's open up a folder.

I'm going to create a new directory that says View project. I am going to go into that directory. And inside this directory, I'm going to open up Visual Studio code. So if you don't know Visual Studio code is a code editor. So you can use Sublime Text or atom so these are some other code editors that you can use to write and edit code. So we are in the directory.

What I can do is I can create a new index dot HTML file is before We go any further, you might want to install the polling plugins. Instead, with the Studio code, you have this kind of like a small option for extensions inside extensions, I want you to install the live server extension, this one is by Rick de, if you can install this, what it will do is it will kind of run like a small local server in your system. So whatever the changes, you're doing it in code, you can automatically see them on your website without having to refresh your website every every now and then. Alright, so that's one handy extension. You don't have to install it. It's not It's not something compulsory, but it's a handy tool to have.

All right. So in our index dot HTML, let's have the template up. So we can have the template by saying the exclamation mark and tab. So this will get the small template. So before we get into Vue js, let's look at a small example. Right?

Let's say I have an h1 tag, and I won't pass the name. So now we can run this website or we can run this. It's also in this view course. Lecture one Now, what I can do is I can run this by right clicking, and I'll get an option saying open with live server. And if I click it, it should open up in Chrome. Yes.

So now we can see name here, right? I'll just zoom it up. Alright. So now, for example, let's say I want to change the content of this h1 tag, right? I don't want to have name inside it. So what I'll do is I'll add a script file script that says, has a source of protests and has and since we don't have a project, we are going to create a.js.

What I can do is I can give the hedge fund ID and I can say name. And I can save it. And instead of app.js, what I can do is I can say document dot get element by ID. I can say name, dot inner HTML, and I can change the name to my actually. So if I save it and go back, I'll get my actual name. Right.

So this is something how you do in plain JavaScript or in vanilla JavaScript. Alright, so now that is the plain JavaScript or the vanilla JavaScript way of doing things right. Now let's see how we can implement this same thing with view. So first thing, what we are going to do is we are going to bring Vue JS CDN into our application or into our website. If you don't know what a CDN is, it usually means a content delivery network. A content delivery network sounds complex, but trust me, it's not.

It's very, very simple. We can go to the bjs.org official website, and we can copy this link. So we can take the script link, and we can come back and we can just paste it below or above our script tag. And with that, we have implemented view on to our website that said, Now we need to know how to use it. So instead of the hedge fund, let's create a div that says main and inside the div, let's have the h1 Let's leave it blank for now. And instead remain inside of the app dot j s, what we need to do is we need to create a new view instance that we can do by saying new view.

And inside that it accepts an object and that object consists of multiple properties. So the first main important property which we need to know about Eazy E l, e, l stands for element, right. So basically what we are doing is we are binding this view instance to a div. So, in our case, we want to bind it to this div with a class of main. So, usually for class, we're going to say dot main, but if we have an ID of main, we would say hash mean, but since we have class, we're going to go with.so dot L is the first property of this view instance. And the next property would be the data property.

So the data property is kind of like the property which holds in all the variables. So these variables are reactive in nature, right. So what is essentially means is that so these variables when using the template, observe the changes within the template. So, if there are any changes with respect to these variables, these variables update themselves, we don't have to specifically attach any event listeners to them, they update themselves. Hence, these are called as reactive variables. And all these variables are declared inside the data properties.

So, the first variable which we are going to declare inside the data property is name and that will contain so it will consist of my name, so, you need to understand in view that most of the things are in object format. So, you need to get used to it, but as you go through you will automatically get used to it. So, see how all the variables are declared like this. So, if I save it, now, I can use this variable name or the data property name inside our HTML. Now, keep in mind that this variables, I mean, all the all the properties that were declared inside this instance, will work only under this div whatever the element The view is attached to, so, If I go in and insert the main, I can see name. So this is called as interpolation syntax.

So when you do this, what happens is that a view essentially looks for a data property name. And inside a view instance, we have added a property called last name, and hence, it substitutes the value of this name on to this. So, if I save it and go back, we should see my name and we can change it just so we can understand it's coming from the view instance. All right. So, now let's look at what happens if I try to use the data property outside the div. Right, we'll have another h1 we'll say the same thing and lose the name.

If I save it. I'm going to get the curly braces as well. Because view is not bound with this element. It's only bound with the elements under this div. That is the reason why view needs to have a root div. In the upcoming lectures, you'll see that view needs to have a specific We could do that can bind itself to.

And under the road day, whatever elements are there, it will work on those. So just to recap what we did, let's create another view instance. So in this case, what we'll do is we'll create a div with an ID of new main. And inside the ID, let's say h1. Let's say my name is. And what we'll do is instead of ab.js, we'll create a new view instance again.

So notice that we can create multiple view instances for multiple divs. We can say element again. And this time we're going to use hash. Because we have an ID, Id inside this we're going to declare two data properties. We say first name, and last name. All right.

So if I save it and go back, I can use it to names I can see First Name, I can give a small space I can see last name. So if I save it and go back, my name is from Shri Krishna. So that is more electron view instance and how data properties work. So in the next lecture we will look into views the Li I'll see you in 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.