{{ message }}
A link with e.preventDefault()
First, we reference the data message that we will eventually render.
We also reference constant variable isRed to the boolean true. Function toggleRed() is used to toggle this referenced value to either true or false.
We also reference constant variable color to the string \\'green\\'. Function toggleColor() is used with a ternary operator, toggling the reference value to a string of either \\'green\\' or \\'blue\\'.
The references declared in these scripts enables a developer to initialize the state, and then the functions declared therein may also be used to manage/modify the state of the page.
Now that we have our state initialized, in addition to the tools necessary to modify it, we can begin working on our template. This is where \\'fusion\\' occurs; at this stage we implementing our state references and functions to actually modify what is rendered!
Hover your mouse over me for a few seconds to see my dynamically bound title!
This should be red... but click me to toggle it.
This should be green, and should toggle between green and blue on click.
Let\\'s break down what is happening here; each paragraph listed above is denoted with a corresponding number:
In the first paragraph, we target the \\'title\\' property of the current HTML element.
In the second paragraph, we are targeting the \\'red\\' class property and we are setting it equal to the current value of isRed.
In the third paragraph, we are targeting the \\'style\\' property and we are setting it equal to the current value of color.
Let\\'s take our approach closer to the real-world, so that we can start understanding what the actual workflow would look like when we are working on our own. First, let\\'s start by creating a template of everything we might want to render to the DOM.
For now, we have created a few buttons that will be statically rendered to the DOM. We also have created an unordered list, but right now we don\\'t know how long our list will be and we don\\'t even know what we want it to display. At this point, we need to understand what our objectives are.
I think it is best that we start with displaying each item of our list, so that way we have a way to visualize the DOM before we start implementing scripts that will change what is rendered. In this case, we want our unordered list to have a new list entry for every item in a collection of data.
- {{listEntry}}
Great, now we have established a reference to a collection within our scripts! Let\\'s see if we can break down what we did here into something that we can understand.
Now that we have a way to dynamically render these items, we can review the functionality of our buttons. We will start with the Toggle List button for the time being. In this case, we want the display of our list to switch between two states \\'shown\\' and \\'hidden\\'. Because there are only two states, we can think of this as a true or false expression.
- {{listEntry}}
We are close to implementing this functionality, but now we need to account for some edge conditions. What if our list is empty? What if our list is hidden, but it isn\\'t empty? Our current design establishes a way to toggle whether something is rendered to the DOM, but there are no conditional expressions that rely on the state of the show variable. Let\\'s fix that.
- {{listEntry}}
First, we have added a Vue directive known as v-if to our unordered list. When the DOM attempts to render the list, it first checks the following conditions:
If either of these conditions fail, with our current implementation, it would refuse to render the list to the DOM. However, we want to be a bit clearer with our implementation. In this case, we want to expand our conditional expressions so that they will inform the user about the current state of the list.
- {{listEntry}}
List is not empty, but hidden.
List is empty.
At this point, we should now have three conditions which are capable of informing the end-user about the state of the list that they are interacting with. This is great progress, but we still need to implement the rest of our buttons. You might notice that, in the previous examples, we are using the properties and methods native to the JavaScript Array prototype. Because we have this functionality, we can interact with our list in the same way.
- {{listEntry}}
List is not empty, but hidden.
List is empty.
At this point, our DOM should have all of the functionality that we originally set out for it to have! This is what is so powerful about Vue; despite the fact that many similar frameworks and libraries can accomplish this, the approach with Vue feels more natural and developer-friendly.
In the beginning, we starting with a basic and static HTML page. We increased our scale slightly when we made our page somewhat dynamic; we added a reference to list and we also added a directive that helped dynamically render items from that list.
After we added this somewhat dynamic feature, we expanded upon it so that other items could change the state of the DOM. In this case, we increased our scale again as we began handling more states.
If we began increasing our scale further, such as in the case were the list reference pointed to data that is received from the server, then we already have a solid foundation that can be tweaked to reflect data appropriately.
So far, we have only looked at how an individual component can change what is rendered to the DOM. However the reality is that, in most cases, your components should be interacting with one another so that you can reuse them in other parts of the DOM. Let\\'s take a look at an example with a simple component.
We import the ref() function from Vue, but we are also importing the component TodoItem so that we can use it inside of this component. Because subcomponents must be imported and implemented, this creates a separation of concerns.
In separations of concerns, the system\\'s components are organized in a way where each component addresses a single concern; the concern could be simple or it could address a (larger) specific functionality for the DOM. This means that each component only interacts with other components when necessary; this is especially beneficial for ensuring that the overall state of our DOM is not affected when we only want changes to occur within a specific component\\'s scope.
Before we move onto our template for this component, we should understand how the subcomponent that it has imported will work. After all, if we don\\'t know what it is doing then we don\\'t know how to use that component.
{{ todo.text }}
It looks like when this component is being setup, it is defining its own properties props to include a key todo that will be assigned to the value of an object. In this case, the properties are looking back to see which data was passed to it as an attribute. Specifically, it is looking for an attribute name todo and it is expecting it to be an Object instance. More documentation on the defineProps() function can be found here.
After the object has been received, the TodoItem component tries to render the text property from the object that has been passed into it. If we have passed the values to it properly, then this should render whatever the value of the text property is for that object. Now we can take a look at how our main App component will try to use the TodoItem component when it renders.
Inside of our template we are using the Vue directive v-for to iterate through the groceryList collection, which is an array of objects. However, in this case, we are referencing the component TodoItem which we imported earlier. In this case, we are saying the following:
TodoItem receives two attributes during each iteration:
We expect that, for every item in our collection, the TodoItem component will return something to be rendered to the DOM.
I hope that you found this introduction to Vue somewhat helpful, it was a pleasure to research into how this framework can be used to make developers more efficient and effective. If you are familiar with similar frameworks and libraries, you might still be wondering whether or not Vue is something that you would want to use for your own projects. If you feel that way, I recommend that you read through Vue\\'s FAQ to see if its benefits fit the needs of your team, project, or organization.
According to the 2023 StackOverflow Developer Survey, Vue ranked eighth for one of the most popular web frameworks and technologies at 16.38%. In comparison, similar frameworks and libraries such as React ranked second at 40.58% whereas Angular ranked fifth at 17.46%.
","image":"http://www.luping.net/uploads/20241021/17294778066715bcaee8848.jpg","datePublished":"2024-11-06T13:47:04+08:00","dateModified":"2024-11-06T13:47:04+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}Vue is one of the most widely used JavaScript frameworks in production today, with over 1.5 million users worldwide. It is downloaded approximately 10 million times a month on Node Package Manager (NPM)!
- Wikimedia Foundation
- National Aeronautics and Space Administration (NASA)
- Apple
- Microsoft
- GitLab
- Zoom
- Tencent
- Bilibili
- Kuaishou
Vue is a "progressive" JavaScript framework for building user interfaces. It works by building on top of standard HTML, CSS, and JavaScript. It is a component-based programming model similar to that of Anuglar, AngularJS, and React. As a framework, Vue aims to be flexible so that it can adapt to the projects of any scale. It can be implemented for any purpose ranging from creating a static HTML page, embedding web components, and dynamic DOM rendering based on server interactions.
- Single-File Components (SFC) provide a modularized development model that allows different parts of an application to be developed in isolation.
- Composition API enables clean patterns for organizing, extracting, and reusing complex logic.
- Comprehensive tooling support ensures a smooth development experience as the application grows, which can translate to a more efficient development cycle.
- Lower barrier to entry and excellent documentation translates to lower onboarding/training costs for new developers.
{{ message }}
{{ message }}
A link with e.preventDefault()
First, we reference the data message that we will eventually render.
We also reference constant variable isRed to the boolean true. Function toggleRed() is used to toggle this referenced value to either true or false.
We also reference constant variable color to the string 'green'. Function toggleColor() is used with a ternary operator, toggling the reference value to a string of either 'green' or 'blue'.
The references declared in these scripts enables a developer to initialize the state, and then the functions declared therein may also be used to manage/modify the state of the page.
Now that we have our state initialized, in addition to the tools necessary to modify it, we can begin working on our template. This is where 'fusion' occurs; at this stage we implementing our state references and functions to actually modify what is rendered!
Hover your mouse over me for a few seconds to see my dynamically bound title!
This should be red... but click me to toggle it.
This should be green, and should toggle between green and blue on click.
Let's break down what is happening here; each paragraph listed above is denoted with a corresponding number:
In the first paragraph, we target the 'title' property of the current HTML element.
In the second paragraph, we are targeting the 'red' class property and we are setting it equal to the current value of isRed.
In the third paragraph, we are targeting the 'style' property and we are setting it equal to the current value of color.
Let's take our approach closer to the real-world, so that we can start understanding what the actual workflow would look like when we are working on our own. First, let's start by creating a template of everything we might want to render to the DOM.
For now, we have created a few buttons that will be statically rendered to the DOM. We also have created an unordered list, but right now we don't know how long our list will be and we don't even know what we want it to display. At this point, we need to understand what our objectives are.
I think it is best that we start with displaying each item of our list, so that way we have a way to visualize the DOM before we start implementing scripts that will change what is rendered. In this case, we want our unordered list to have a new list entry for every item in a collection of data.
- {{listEntry}}
Great, now we have established a reference to a collection within our scripts! Let's see if we can break down what we did here into something that we can understand.
Now that we have a way to dynamically render these items, we can review the functionality of our buttons. We will start with the Toggle List button for the time being. In this case, we want the display of our list to switch between two states 'shown' and 'hidden'. Because there are only two states, we can think of this as a true or false expression.
- {{listEntry}}
We are close to implementing this functionality, but now we need to account for some edge conditions. What if our list is empty? What if our list is hidden, but it isn't empty? Our current design establishes a way to toggle whether something is rendered to the DOM, but there are no conditional expressions that rely on the state of the show variable. Let's fix that.
- {{listEntry}}
First, we have added a Vue directive known as v-if to our unordered list. When the DOM attempts to render the list, it first checks the following conditions:
If either of these conditions fail, with our current implementation, it would refuse to render the list to the DOM. However, we want to be a bit clearer with our implementation. In this case, we want to expand our conditional expressions so that they will inform the user about the current state of the list.
- {{listEntry}}
List is not empty, but hidden.
List is empty.
At this point, we should now have three conditions which are capable of informing the end-user about the state of the list that they are interacting with. This is great progress, but we still need to implement the rest of our buttons. You might notice that, in the previous examples, we are using the properties and methods native to the JavaScript Array prototype. Because we have this functionality, we can interact with our list in the same way.
- {{listEntry}}
List is not empty, but hidden.
List is empty.
At this point, our DOM should have all of the functionality that we originally set out for it to have! This is what is so powerful about Vue; despite the fact that many similar frameworks and libraries can accomplish this, the approach with Vue feels more natural and developer-friendly.
In the beginning, we starting with a basic and static HTML page. We increased our scale slightly when we made our page somewhat dynamic; we added a reference to list and we also added a directive that helped dynamically render items from that list.
After we added this somewhat dynamic feature, we expanded upon it so that other items could change the state of the DOM. In this case, we increased our scale again as we began handling more states.
If we began increasing our scale further, such as in the case were the list reference pointed to data that is received from the server, then we already have a solid foundation that can be tweaked to reflect data appropriately.
So far, we have only looked at how an individual component can change what is rendered to the DOM. However the reality is that, in most cases, your components should be interacting with one another so that you can reuse them in other parts of the DOM. Let's take a look at an example with a simple component.
We import the ref() function from Vue, but we are also importing the component TodoItem so that we can use it inside of this component. Because subcomponents must be imported and implemented, this creates a separation of concerns.
In separations of concerns, the system's components are organized in a way where each component addresses a single concern; the concern could be simple or it could address a (larger) specific functionality for the DOM. This means that each component only interacts with other components when necessary; this is especially beneficial for ensuring that the overall state of our DOM is not affected when we only want changes to occur within a specific component's scope.
Before we move onto our template for this component, we should understand how the subcomponent that it has imported will work. After all, if we don't know what it is doing then we don't know how to use that component.
{{ todo.text }}
It looks like when this component is being setup, it is defining its own properties props to include a key todo that will be assigned to the value of an object. In this case, the properties are looking back to see which data was passed to it as an attribute. Specifically, it is looking for an attribute name todo and it is expecting it to be an Object instance. More documentation on the defineProps() function can be found here.
After the object has been received, the TodoItem component tries to render the text property from the object that has been passed into it. If we have passed the values to it properly, then this should render whatever the value of the text property is for that object. Now we can take a look at how our main App component will try to use the TodoItem component when it renders.
Inside of our template we are using the Vue directive v-for to iterate through the groceryList collection, which is an array of objects. However, in this case, we are referencing the component TodoItem which we imported earlier. In this case, we are saying the following:
TodoItem receives two attributes during each iteration:
We expect that, for every item in our collection, the TodoItem component will return something to be rendered to the DOM.
I hope that you found this introduction to Vue somewhat helpful, it was a pleasure to research into how this framework can be used to make developers more efficient and effective. If you are familiar with similar frameworks and libraries, you might still be wondering whether or not Vue is something that you would want to use for your own projects. If you feel that way, I recommend that you read through Vue's FAQ to see if its benefits fit the needs of your team, project, or organization.
According to the 2023 StackOverflow Developer Survey, Vue ranked eighth for one of the most popular web frameworks and technologies at 16.38%. In comparison, similar frameworks and libraries such as React ranked second at 40.58% whereas Angular ranked fifth at 17.46%.
Vue is one of the most widely used JavaScript frameworks in production today, with over 1.5 million users worldwide. It is downloaded approximately 10 million times a month on Node Package Manager (NPM)!
- Wikimedia Foundation
- National Aeronautics and Space Administration (NASA)
- Apple
- Microsoft
- GitLab
- Zoom
- Tencent
- Bilibili
- Kuaishou
Haftungsausschluss: Alle bereitgestellten Ressourcen stammen teilweise aus dem Internet. Wenn eine Verletzung Ihres Urheberrechts oder anderer Rechte und Interessen vorliegt, erläutern Sie bitte die detaillierten Gründe und legen Sie einen Nachweis des Urheberrechts oder Ihrer Rechte und Interessen vor und senden Sie ihn dann an die E-Mail-Adresse: [email protected] Wir werden die Angelegenheit so schnell wie möglich für Sie erledigen.
Copyright© 2022 湘ICP备2022001581号-3