Trending October 2023 # How Data() Works Inwith Examples # Suggested November 2023 # Top 18 Popular | Nhunghuounewzealand.com

Trending October 2023 # How Data() Works Inwith Examples # Suggested November 2023 # Top 18 Popular

You are reading the article How Data() Works Inwith Examples updated in October 2023 on the website Nhunghuounewzealand.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 How Data() Works Inwith Examples

Introduction to chúng tôi data()

The chúng tôi provides a chúng tôi data() method to the user. Basically in chúng tôi data() we defined collection of logic and stored in component using chúng tôi we can access data Node.jsassociated with a vue instance. Components are reusable as many times as per requirement. But each time it uses a separate component and also creates a new instance. When we use a data function at that time each instance maintains a separate copy of the return data object. Most of chúng tôi uses API to fetch and post data. It is safe because it does not affect the changes outside the scope.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

data: function () { return { count: 0 } }, })

Syntax:

data: function () { return { count: 0 } } How does data() work in Vue.js?

We must install chúng tôi 10.x and above on your system and you can verify by using node –v command in your terminal.

We must install the latest version of Vue on your system.

We must install the Vue CLI 3.0 version on your system.

We required basic knowledge about Node.js

We required basic knowledge about chúng tôi and components.

Examples of chúng tôi data()

Let’s see how we can implement chúng tôi data() with different examples as follows.

Example #1

A basic example of data method

App.vue:

export default { name: ‘App’, data(){ return{ title: ‘welcome in vue data function’ } } } #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; }

In this example, we only made changes in the chúng tôi file in which we added a data method to access the title. Illustrate the end result of the above declaration by using the use of the following snapshot.

Example #2

For Data with Method

App.vue:

export default { data: function() { return { title: “function with method “ }; }, methods:{ Msg:function(){ return ‘$’ + this.title } } } #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; }

Explanation: In the above example first we created a data function that returns an object. Every property of vue is added to the vue reactivity system so that if we make some changes in the program that is updated. So that we add title property in our template by using {{}} double curly braces. Vue js must require {{}} double curly to pass javaScript Expression.

In the second part of the program we have created a method and the method also has an object. Here we create a Msgmethod which returns a string. Inside the method, we access data object property by using this and finally we added in the template tag. Illustrate the end result of the above declaration by using the use of the following snapshot.

Example #3

chúng tôi

export default { data: function() { return { name1: “Node.js “, name2: “Java Script” }; }, methods:{ Msg:function(){ return ‘Coding done by’ +this.name1 +”+ this.name2 } } } #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; }

Explanation: This is another simple example of data function with methods. Illustrate the end result of the above declaration by using the use of the following snapshot.

Example #4

Emit data in chúng tôi with event

chúng tôi

export default { name: “HelloWorld”, data() { return { StartMsg: “” } }, props: { msg: String }, methods: { changeMsg() { this.$emit(“changeMsg”, this.StartMsg); console.log(‘message emit from child component’) } } }; h3 { margin: 40px 0 0; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; }

Explanation: In the above component, we sent messages from the child component to the parent component. In the above template, we use a button to send the message to the parent component. See in this template we use the changes function whenever we submit a message to the parent component.

chúng tôi

import HelloWorld from “./components/HelloWorld.vue”; export default { name: “app”, data() { return { StartMsg: “Welcome in Data function World” }; }, methods: { setMessage(msg) { this.StartMsg = msg; } }, components: { HelloWorld } }; #app { font-family: “Avenir”, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; left: 20px; }

Explanation: In this file actually we are listing our event. In the first component, we just send messages from the child component to the parent component but in this we actually manipulate them. In this file, we import the chúng tôi component for listening to custom events by using changeMsg function. We also define a setMessage function to handle custom events. The remaining part is the css part this necessary for design purpose. In this example we by default content of vue. Illustrate the end result of the above declaration by using the use of the following snapshot.

After Submit Message:

Conclusion

We hope from this article you have understood about the chúng tôi data. From the above article, we have learned the basic syntax of the chúng tôi data function. We have also learned how we can implement them in chúng tôi with examples. From this article, we have learned how we can handle chúng tôi data as well as components in Node.js.

Recommended Articles

This is a guide to chúng tôi data() Here we also discuss the introduction, syntax, and working of data() in chúng tôi along with different examples and code implementation. You may also have a look at the following articles to learn more –

You're reading How Data() Works Inwith Examples

Update the detailed information about How Data() Works Inwith Examples on the Nhunghuounewzealand.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!