Skip to content

Commit aa0bf51

Browse files
authored
Merge pull request #1920 from sivaprasath2004/sivaprasath-closes-issue-1892
[Feature]: Add New Tutorials Vue.js Added
2 parents e3d8f12 + 95eea43 commit aa0bf51

10 files changed

+807
-0
lines changed

docs/Vue.js/ComponentBased.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
id: vue.js-Component-Based
3+
title: Component-Based in Vue.Js
4+
sidebar_label: Component-Based in Vue.Js
5+
sidebar_position: 3
6+
tags: [Vue.js,JavaScript,Vue.js Introduction,Component-Based,Framework,Vue.Js Concepts]
7+
description: Vue.Js Component-Based.
8+
---
9+
10+
11+
Component-based architecture is a fundamental concept in Vue.js (and many other modern JavaScript frameworks) that promotes building applications by breaking them down into reusable, self-contained components. Each component encapsulates a piece of UI, including its HTML structure, CSS styles, and JavaScript logic, making it easier to manage and maintain complex applications.
12+
13+
### Key Aspects of Component-Based Architecture in Vue.js:
14+
15+
1. **Reusability**: Components can be reused throughout your application. For example, a button component or a user profile component can be used multiple times across different parts of your application.
16+
17+
2. **Encapsulation**: Each Vue component is encapsulated, meaning its internal implementation is hidden from other components. This reduces the complexity of your application by allowing you to focus on the specific functionality of each component.
18+
19+
3. **Composition**: Components can be composed together to build larger components or pages. This enables a hierarchical structure where smaller, simpler components can be combined to create more complex components.
20+
21+
4. **Separation of Concerns**: Components encourage separation of concerns by separating the UI (HTML template) from the business logic (JavaScript) and styling (CSS). This separation improves maintainability and allows different team members (designers, developers) to work more independently.
22+
23+
5. **Communication Between Components**: Vue.js provides mechanisms for components to communicate with each other, such as props (for parent-to-child communication) and events (for child-to-parent communication). This ensures that components remain loosely coupled while still being able to interact with each other.
24+
25+
### Example of Vue.js Component:
26+
27+
Here's a simple example of a Vue component that displays a user profile:
28+
29+
```html
30+
<!-- UserProfile.vue -->
31+
<template>
32+
<div class="user-profile">
33+
<img :src="user.avatar" alt="User Avatar">
34+
<h2>{{ user.name }}</h2>
35+
<p>{{ user.bio }}</p>
36+
</div>
37+
</template>
38+
39+
<script>
40+
export default {
41+
name: 'UserProfile',
42+
props: {
43+
user: {
44+
type: Object,
45+
required: true
46+
}
47+
}
48+
};
49+
</script>
50+
51+
<style scoped>
52+
.user-profile {
53+
border: 1px solid #ccc;
54+
padding: 10px;
55+
margin: 10px;
56+
}
57+
.user-profile img {
58+
width: 100px;
59+
height: 100px;
60+
border-radius: 50%;
61+
}
62+
</style>
63+
```
64+
65+
In this example:
66+
- The `UserProfile` component encapsulates the structure of a user profile, including an image, name, and bio.
67+
- It receives a `user` object as a prop, which allows it to display dynamic data based on the parent component's data.
68+
- The component's template (`<template>`), JavaScript logic (`<script>`), and styles (`<style>`) are all encapsulated within the component file.
69+
70+
By using components in Vue.js, you can modularize your application's UI, improve code reusability, and enhance maintainability. This approach is particularly beneficial for large-scale applications where managing complexity and scaling development efforts are crucial.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
id: vue.js-computed-properties-and-watchers
3+
title: Computed Properties and Watchers in Vue.Js
4+
sidebar_label: Computed Properties and Watchers in Vue.Js
5+
sidebar_position: 6
6+
tags: [Vue.js,JavaScript,Vue.js Introduction,Computed Properties and Watchers,Framework,Vue.Js Concepts]
7+
description: Vue.Js Computed Properties and Watchers.
8+
---
9+
10+
Computed properties and watchers are advanced features in Vue.js that allow you to perform reactive computations and respond to changes in data. They are crucial for handling complex logic and maintaining the reactivity of your application.
11+
12+
### Computed Properties:
13+
14+
Computed properties are functions defined in Vue components that compute a value based on other data properties. They are cached based on their dependencies and only re-evaluate when one of those dependencies changes. Computed properties are especially useful when you need to derive one value from another or perform complex calculations.
15+
16+
#### Syntax and Usage:
17+
18+
```html
19+
<template>
20+
<div>
21+
<p>{{ fullName }}</p>
22+
</div>
23+
</template>
24+
25+
<script>
26+
export default {
27+
data() {
28+
return {
29+
firstName: 'John',
30+
lastName: 'Doe'
31+
};
32+
},
33+
computed: {
34+
fullName() {
35+
return `${this.firstName} ${this.lastName}`;
36+
}
37+
}
38+
};
39+
</script>
40+
```
41+
42+
In this example:
43+
- The `fullName` computed property concatenates `firstName` and `lastName`.
44+
- It is accessed in the template as `{{ fullName }}`.
45+
- Vue.js automatically handles updating `fullName` whenever `firstName` or `lastName` changes.
46+
47+
**Key Features of Computed Properties:**
48+
- **Caching**: Computed properties are cached based on their reactive dependencies. They only recompute when necessary, optimizing performance.
49+
- **Dependency Tracking**: Vue.js automatically tracks dependencies (like `this.firstName` and `this.lastName` in the example) and updates the computed property when these dependencies change.
50+
51+
### Watchers:
52+
53+
Watchers are functions that Vue.js provides to watch for changes in data properties and perform asynchronous or expensive operations in response. Unlike computed properties, watchers do not return a value but allow you to perform side effects, such as making Ajax requests or performing animations.
54+
55+
#### Syntax and Usage:
56+
57+
```html
58+
<template>
59+
<div>
60+
<input type="text" v-model="searchQuery">
61+
<p v-if="searchResults.length">Results: {{ searchResults }}</p>
62+
<p v-else>No results found.</p>
63+
</div>
64+
</template>
65+
66+
<script>
67+
export default {
68+
data() {
69+
return {
70+
searchQuery: '',
71+
searchResults: []
72+
};
73+
},
74+
watch: {
75+
searchQuery(newQuery, oldQuery) {
76+
// Perform search operation or API call
77+
this.fetchSearchResults(newQuery);
78+
}
79+
},
80+
methods: {
81+
fetchSearchResults(query) {
82+
// Simulated API call
83+
setTimeout(() => {
84+
this.searchResults = ['Result 1', 'Result 2']; // Update search results
85+
}, 500);
86+
}
87+
}
88+
};
89+
</script>
90+
```
91+
92+
In this example:
93+
- The `watch` option is used to define a watcher for `searchQuery`.
94+
- When `searchQuery` changes, the `searchQuery` watcher function is triggered.
95+
- Inside the watcher function, we call `fetchSearchResults(newQuery)` to update `searchResults` based on the new query.
96+
97+
**Key Features of Watchers:**
98+
- **Imperative Logic**: Watchers allow you to perform asynchronous operations, such as API calls or data manipulation, in response to data changes.
99+
- **Access to Previous Value**: Watchers provide access to both the new and old values of the watched property (`newQuery` and `oldQuery` in the example).
100+
101+
### When to Use Computed Properties vs Watchers:
102+
103+
- **Computed Properties**: Use computed properties when you need to derive a new value based on existing data properties or when you want to perform calculations that should be cached and reactive.
104+
105+
- **Watchers**: Use watchers when you need to perform more complex or asynchronous operations in response to changes in data properties. Watchers are also useful for responding to changes that cannot be expressed declaratively with computed properties.
106+
107+
By leveraging computed properties and watchers effectively, you can build more reactive and performant Vue.js applications that handle complex state management and dynamic behavior seamlessly.

docs/Vue.js/DeclarativeRendering.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
id: vue.js-declarative-rendering
3+
title: Declarative Rendering in Vue.Js
4+
sidebar_label: Declarative Rendering in Vue.Js
5+
sidebar_position: 2
6+
tags: [Vue.js,JavaScript,Vue.js Introduction,Declarative Rendering,Framework,Vue.Js Concepts]
7+
description: Vue.Js Declarative Rendering.
8+
---
9+
10+
## Declarative Rendering
11+
Declarative rendering is a core concept in Vue.js that simplifies how you specify what you want to display in your web application's user interface (UI). Instead of manually manipulating the DOM (Document Object Model) to update the UI based on changes in data, Vue.js allows you to declare what should be rendered based on the current state of your data.
12+
13+
### Example 1: Basic Data Binding
14+
15+
Consider a simple Vue.js component that displays a user's name:
16+
17+
```html
18+
<div id="app">
19+
<p>{{ message }}</p>
20+
</div>
21+
22+
<script>
23+
var app = new Vue({
24+
el: '#app',
25+
data: {
26+
message: 'Hello, Vue.js!'
27+
}
28+
});
29+
</script>
30+
```
31+
32+
In this example:
33+
- The `{{ message }}` syntax in the `<p>` tag is a template syntax in Vue.js.
34+
- Vue.js automatically binds the `message` data property to the text content of the `<p>` tag.
35+
- When `message` changes (for example, through user interaction or data updates), Vue.js will automatically update the DOM to reflect the new value.
36+
37+
This approach is declarative because you declare what content should be displayed (`{{ message }}`), and Vue.js handles updating the DOM for you based on changes in the `message` data property.
38+
39+
### Example 2: Conditional Rendering
40+
41+
You can also use declarative rendering to conditionally display elements based on the state of your data:
42+
43+
```html
44+
<div id="app">
45+
<p v-if="seen">Now you see me</p>
46+
</div>
47+
48+
<script>
49+
var app = new Vue({
50+
el: '#app',
51+
data: {
52+
seen: true
53+
}
54+
});
55+
</script>
56+
```
57+
58+
Here:
59+
- The `v-if` directive in Vue.js conditionally renders the `<p>` element based on the `seen` data property.
60+
- When `seen` is `true`, Vue.js will include the `<p>` element in the DOM. If `seen` changes to `false`, Vue.js will remove the element from the DOM.
61+
62+
This declarative approach makes it easier to manage UI state and logic in your Vue.js components. Instead of manually adding or removing elements from the DOM based on conditions, you declare the conditions directly in your template with directives like `v-if`, `v-show`, or `v-for`, and Vue.js takes care of the rest.
63+
64+
In summary, declarative rendering in Vue.js simplifies UI development by allowing you to focus on describing the desired UI state based on your data, rather than writing imperative DOM manipulation code. This approach enhances code readability, maintainability, and reduces the likelihood of bugs related to UI updates.

docs/Vue.js/Directives.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
id: vue.js-Directives
3+
title: Directives in Vue.Js
4+
sidebar_label: Directives in Vue.Js
5+
sidebar_position: 5
6+
tags: [Vue.js,JavaScript,Vue.js Introduction,Directives,Framework,Vue.Js Concepts]
7+
description: Vue.Js Directives.
8+
---
9+
10+
## Directives
11+
12+
Directives in Vue.js are special attributes prefixed with `v-`, which are used to add dynamic behavior to HTML elements or components. They are fundamental building blocks of Vue.js applications and provide declarative syntax to manipulate the DOM, handle events, conditionally render elements, and more.
13+
14+
### Commonly Used Vue.js Directives:
15+
16+
1. **`v-bind` (Shorthand: `:`)**:
17+
- Used to bind one or more attributes or props to an element.
18+
- Example:
19+
20+
```html
21+
<div v-bind:class="{ active: isActive }"></div>
22+
```
23+
24+
This binds the `class` attribute of the `<div>` dynamically based on the `isActive` data property.
25+
26+
2. **`v-if`, `v-else-if`, `v-else`**:
27+
- Conditional rendering directive. The element will only render if the expression evaluates to true.
28+
- Example:
29+
30+
```html
31+
<p v-if="loggedIn">Welcome, {{ username }}</p>
32+
<p v-else>Please log in to continue.</p>
33+
```
34+
35+
3. **`v-for`**:
36+
- Used for rendering a list of items based on an array.
37+
- Example:
38+
39+
```html
40+
<ul>
41+
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
42+
</ul>
43+
```
44+
45+
This renders an `<li>` element for each item in the `items` array.
46+
47+
4. **`v-on` (Shorthand: `@`)**:
48+
- Used to listen to DOM events and execute JavaScript when they occur.
49+
- Example:
50+
51+
```html
52+
<button v-on:click="incrementCounter">Increment</button>
53+
```
54+
55+
This executes the `incrementCounter` method in Vue instance when the button is clicked.
56+
57+
5. **`v-model`**:
58+
- Creates two-way data bindings on form input elements or components.
59+
- Example:
60+
61+
```html
62+
<input v-model="message" type="text">
63+
```
64+
65+
This binds the `message` data property to the value of the `<input>` element, allowing changes in the input field to automatically update `message` and vice versa.
66+
67+
6. **`v-show`**:
68+
- Similar to `v-if`, but toggles the display of the element with CSS `display` property (visibility toggle) instead of removing it from the DOM.
69+
- Example:
70+
71+
```html
72+
<p v-show="isVisible">This paragraph will be shown or hidden based on the isVisible data property.</p>
73+
```
74+
75+
### Custom Directives:
76+
77+
In addition to the built-in directives, Vue.js also allows you to create custom directives when you need to encapsulate some low-level DOM manipulation or behavior. Custom directives are registered using `Vue.directive()` global method or locally in components.
78+
79+
### Directive Modifiers:
80+
81+
Directives can also have modifiers that provide additional functionality or behavior to the directive. For example:
82+
83+
- `.prevent`: Prevents the default behavior of an event.
84+
- `.stop`: Stops event propagation.
85+
- `.capture`: Adds an event listener in capture mode.
86+
- `.once`: Trigger the event listener at most once.
87+
88+
### Summary:
89+
90+
Vue.js directives provide a powerful way to add dynamic behavior to your HTML templates. They simplify the interaction between the DOM and Vue instance data, making it easier to build interactive and reactive applications. Understanding and utilizing directives effectively is key to mastering Vue.js development.

0 commit comments

Comments
 (0)