nini-chang / Notes

紀錄學習筆記
0 stars 0 forks source link

Vue - The Complete Guide (w/ Router, Vuex, Composition API) #10

Open nini-chang opened 3 years ago

nini-chang commented 3 years ago

Instructor: Maximilian Schwarzmüller course

Section 1: Getting Started

Different ways of using Vue Vue Alternatives
image image
Build An App With Just JavaScript Rebuild the App with Vue
gs-01-starting-project.zip gs-03-rebuild-the-app-with-vue-project.zip
practice-with-just-javascript rebuild-the-app-with-vue

Course Content

nini-chang commented 3 years ago

Section 2: Basics & Core Concepts - DOM interaction with Vue

Module Content


Creating and Connecting Vue App Instances | app.js | | ------ | | Note: If we control a HTML element with Vue, we'll also control all child elements of that element | | ![image](https://user-images.githubusercontent.com/58713955/121824500-33935900-ccdf-11eb-8653-c293d490e423.png) |
Interpolation and Data Binding - `{{ courseGoal }}` interpolation - interpolation: reference properties that are part of that object you return in data, and then the value of the property will be output instead of `{{ courseGoal }}` - text(courseGoal) is stored in JavaScript, yet, it's rendered on the screen | app.js | index.html | output(view) | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121824500-33935900-ccdf-11eb-8653-c293d490e423.png) | ![image](https://user-images.githubusercontent.com/58713955/121824515-56be0880-ccdf-11eb-83a6-958d6e7653a7.png) | ![image](https://user-images.githubusercontent.com/58713955/121824103-2e80da80-ccdc-11eb-9520-bdfa4a2bfb8a.png) |
Binding Attributes with the "v-bind" Directive - `{{ }}` this double curly brace syntax is only available between opening and closing HTML tags - Set the value of some HTML attributes - If you wanna pass in a dynamic value to an attribute, need to use a different syntax, which is Vue binding syntax (also called Vue directive, which is simply an extra instruction we add in HTML code) - example: `v-bind:href` | index.html | app.js | output | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121824407-a0f2ba00-ccde-11eb-80b5-e1f673dfb553.png) |![image](https://user-images.githubusercontent.com/58713955/121824531-69d0d880-ccdf-11eb-940b-bb5f682be14c.png) | ![v-bind_directive](https://user-images.githubusercontent.com/58713955/121824427-b9fb6b00-ccde-11eb-8e5a-799682b50978.gif) |
Understanding "methods" in Vue Apps - Methods allow you to define functions - `data` and `methods` are a reserved name - Different with `data` - data, itself is a function a method - methods takes an object, which will be full of methods, but these functions are bow totally up to you > Note: you can use any JavaScript expression between double curly braces (`{{ }}`) | index.html | app.js | output | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121824694-9f29f600-cce0-11eb-8cb4-13362c876e80.png) | ![image](https://user-images.githubusercontent.com/58713955/121824701-b9fc6a80-cce0-11eb-8c9b-05f5643d983a.png) | ![methods](https://user-images.githubusercontent.com/58713955/121824728-ef08bd00-cce0-11eb-8297-bdf258ef17bb.gif) |
Working with Data inside of a Vue App - You can't use `courseGoalA` directly inside methods, because `courseGoalA` is not a variable or constant, it's a property of data(object). Instead you can use `this.courseGoalA` - Explanation: Vue does some magic. It basically takes all the data you return in data object (which you return in the data method) and **it merge it into a global Vue instance object**. So the data properties are available behind the scenes managed Vue app object. **Your methods are also available there.** And they do have access to anything stored in that global object through **this** keyword. | app.js | | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121825003-99cdab00-cce2-11eb-8f54-fc95faac2733.png) |
Outputting Raw HTML Content with v-html - Be Aware if you have some contents stored in Vue App, which contents HTML code that should be treated as HTML - `v-html` - set the content between opening and closing text of an element - you should definitely not use it as a default, because you can introduce security issue with that, also because basically circumvent(規避) the built in cross site scripting attack protection you have with the double curly braces - use it only if you know what you're doing and you need to output something as HTML | index.html | | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121825425-e2866380-cce4-11eb-9de4-3f3e23d9a9f1.png) |

Vue uses a declarative approach. We just declare our goal. We don't care about the steps of getting there. basic-starting-code (practice data-binding): basics-01-starting-code.zip

Assignment 1: Data Binding basics-assignment-1.zip


Understanding Event Binding - `v-on` directive - takes the event to which you wanna listen to on HTML elements - `v-on:click`, `v-on:mouseenter` ...
Events & Methods > Vue detect both `v-on:click="onAdd"` or `v-on:click="onAdd()"` | index.html | app.js | output | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121827287-0e5a1700-ccee-11eb-9ce1-ac7fd5ca4eec.png) | ![image](https://user-images.githubusercontent.com/58713955/121827334-321d5d00-ccee-11eb-8c37-ed234c518f7f.png) | ![event-binding](https://user-images.githubusercontent.com/58713955/121827422-81fc2400-ccee-11eb-86d5-2c3c9a4d1b18.gif) |
Working with Event Arguments | index.html | app.js | output | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121827563-19fa0d80-ccef-11eb-9747-ac62cb14349d.png) | ![image](https://user-images.githubusercontent.com/58713955/121827584-28482980-ccef-11eb-83e4-83789e81644a.png) | ![event-binding-with-args](https://user-images.githubusercontent.com/58713955/121827675-7ceba480-ccef-11eb-8337-57145221e63c.gif) |
Using the Native Event Object - The best event to listen to on an input HTML element is the `input` event, which is a default DOM event available on input elements, nothing Vue specific. - How do we get the concrete value the user enter ? - While there is a built-in behavior into JavaScript (not into Vue), which we can leverage(利用) - When you add an event listener and point at a function that should be executed. When that event occurs, that function will automatically get one argument. The browser will ensure that this argument is provided so to say. And that will be an OBJECT that describing the event that occurred. > Note: The "order" of methods - just like the order of "data" and "methods" does NOT matter. | index.html | app.js | output | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121828501-5844fc00-ccf2-11eb-9047-dac28f3efb7d.png) | ![image](https://user-images.githubusercontent.com/58713955/121828592-a1954b80-ccf2-11eb-9b5a-40dbd2318eaa.png) | ![using-the-native-event-object](https://user-images.githubusercontent.com/58713955/121828761-1b2d3980-ccf3-11eb-98ab-beb575df4c1c.gif) |
Exploring Event Modifiers - Event Modifiers - `
` as an example: When you wanna prevent this browser default(click on submit, automatically sending a request and the page will reload) and instead you wanna handle this manually in JavaScript with help of Vue in your Vue app, and read the user input there, validate it and then send it manually to a backend server and store it in a database. - Two way to do: listen to the submit event (`v-on:submit=""`) or Vue built-in event modifiers | | index.html | app.js | output | | ------ | ------ | ------ | ------ | | `v-on:submit` | ![image](https://user-images.githubusercontent.com/58713955/121829150-4c5a3980-ccf4-11eb-8398-f0851f11fba0.png) | ![image](https://user-images.githubusercontent.com/58713955/121829183-672cae00-ccf4-11eb-9155-56faf7d7c678.png) | ![v-on_submit](https://user-images.githubusercontent.com/58713955/121829354-d6a29d80-ccf4-11eb-8b45-0f4825e3e366.gif) | | Vue built-in `v-on:submit.prevent` | ![image](https://user-images.githubusercontent.com/58713955/121829438-19647580-ccf5-11eb-8ec7-7b038905dd15.png) | ![image](https://user-images.githubusercontent.com/58713955/121829479-3d27bb80-ccf5-11eb-9cec-f99e1aefd7c9.png) | same as above | - Vue built-in event modifiers - `v-on:submit.prevent`: prevent automatically send request and reload the page - `v-on:click.right`: only execute the function when the user click on the right button of the mouse - `v-on:keyup.enter`: only execute the function when the user press on the enter key
Locking Content with v-once `v-once` - Any dynamic data bindings should only be evaluated once. | index.html | output | | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121830340-634e5b00-ccf7-11eb-985a-842894e369f0.png) | ![v-once](https://user-images.githubusercontent.com/58713955/121830432-a4466f80-ccf7-11eb-8644-41fce7d0a710.gif) |

basic-starting-code (practice event-binding): basics-03-events-starting-code.zip

Assignment 2: Event Binding basics-assignment-2-problem.zip

(assignment practice) event binding > keydown: the key is pressed, but not yet released. The value in the input will only update once the key is released or finished. | index.html | app.js | output | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/121911686-90842300-cd62-11eb-961f-2856dbe233b5.png) | ![image](https://user-images.githubusercontent.com/58713955/121911756-9da11200-cd62-11eb-87f6-0aaf884b64af.png) | ![assignment-2-event-binding](https://user-images.githubusercontent.com/58713955/121912128-e2c54400-cd62-11eb-9641-77eab52a56dc.gif) |

Data Binding + Event Binding = Two-way Binding - `v-model` 同時取代 `v-bind:value` 和 `v-on:input` - 監聽 input 的值,同步輸出 input 的值 | index.html | app.js | view | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/123030151-e1f07a00-d414-11eb-9760-15e5334023d9.png) | ![image](https://user-images.githubusercontent.com/58713955/123030292-2845d900-d415-11eb-95ea-14921e45c674.png) | ![two-way-bimding](https://user-images.githubusercontent.com/58713955/123030446-6d6a0b00-d415-11eb-93a3-17042eccaad3.gif) | > two-way-binding: [basics-05-two-way-binding.zip](https://github.com/nini-chang/Notes/files/6698705/basics-05-two-way-binding.zip)
Methods used for Data Binding: How It Works | 擷取自課程 | | ------ | | Methods are not the best solution for outputting some dynamically calculated value | | ![not-the-best-solution](https://user-images.githubusercontent.com/58713955/123077530-b38e9100-d44c-11eb-992a-6e45837ad8f5.gif) |
Introducing Computed Properties - Computed = the third big configuration option (cannot change the name) - An object: define a bunch of methods - Use it as data property (not like a method), not going to call it - Essentially like methods with one important different view will be aware of their dependencies changed - You only want to recalculate a value, if a dependency changed > If you just want to calculate some output value dynamically, computed properties are your friend > Note: You still bind your events to methods, You don't bind events to computer properties | Computed | | ------ | | ![computed](https://user-images.githubusercontent.com/58713955/123079701-c1ddac80-d44e-11eb-9adc-c408d28b8015.gif) |
Working with Watchers - Watcher - An object: define a bunch of methods - A function that you can tell Vue to execute, when one of its dependencies changed (sounds like computed properties) - Used: watch on one dependency, HTTP requests (send certain data changes), Timer (which you wanna set, if value exceeds 50 or other) > If you wanna do that execute code, because something changed, then watchers can be helpful

Practice Computed and Watcher: basic-practice-compute-and-watcher.zip

Methods vs Computed Properties vs Watchers | Versus | | ------ | | ![Screen Shot 2021-06-23 at 6 39 51 PM](https://user-images.githubusercontent.com/58713955/123083311-847b1e00-d452-11eb-829b-835cd6b61894.png) | > Amongst the free Watches are probably the feature you'll use the least, because you need methods all the time with even binding and computed properties for outputting data that depends on other data
v-bind and v-on shorthands - `v-on:click.right` can be replace to `@click.right` - `v-bind:value` can be replace to `:value` > Note: there is no `v-model` shorthands

Assignment 3: Reactivity basics-assignment-3-problem.zip


Dynamic Styling with Inline Styling | index.html | app.js | view | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/131691146-43c82d1b-2ae0-4caf-ae62-539c77f742f1.png) | ![image](https://user-images.githubusercontent.com/58713955/131691214-72b48ac8-a310-4f5d-a6a8-b406a65d4375.png) | ![Dynamic_styling_with_inline_styling](https://user-images.githubusercontent.com/58713955/131691720-83ab534d-3773-4b55-9d07-15010c2000e9.gif) | > Note that you can also write in this way: `:style="{ 'border-color': boxASelected ? 'red : '#ccc' }"`. Recommended and better use the camel case: `borderColor`
Add CSS Classes Dynamically - Inline styles ( `:style` ) have a couple of disadvantages. They overrule all other styles. Therefore, in modern web development and CSS, we typically don't use inline styles too often. | index.html | styles.css | app.js | view | | ------ | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/131694643-c1087af7-8b0e-4343-9303-a55ff04ac064.png) | ![image](https://user-images.githubusercontent.com/58713955/131694725-5b410446-e1db-4005-9d2a-bdec826c5c82.png) | ![image](https://user-images.githubusercontent.com/58713955/131694815-f8a045a2-cb7a-4e1c-a0e0-b33f9ac63038.png) | ![Add_CSS_Classes_Dynamically](https://user-images.githubusercontent.com/58713955/131694900-2ec2d506-d53a-422d-874c-e9fedb43e5da.gif) |
Classes & Computed Properties - Having Logic in the HTML code is suboptimal. As a class binding, it actually us okay. - But if you want to move this logic to js file > optional, unless you have more complex dynamic class code (not just referring to a true or false Boolean here) | index.html | app.js | view | | ------ | ------ | ------ | | ![image](https://user-images.githubusercontent.com/58713955/131696617-cd37139f-bf43-4b68-bf7e-d07815fd56bc.png) | ![image](https://user-images.githubusercontent.com/58713955/131696656-07542b55-8062-4b95-bd13-c2a7a63bb7ee.png) | ![Classes_and_Computed_Properties](https://user-images.githubusercontent.com/58713955/131696928-ee4c6567-ee91-42e7-bfa0-4614c52d656a.gif) |
Dynamic Classes: Array Syntax - normally ```
basics-10-styling-starting-setup.zip

Assignment 4: Dynamic Styling basics-assignment-4-problem_solution.zip

basics-assignment-4-problem_mySolution.zip


Module Summary

Graph | Module Summary | | ------ | | ![image](https://user-images.githubusercontent.com/58713955/131861799-7a0c5966-471d-4e93-b678-4786f379b30f.png) |
nini-chang commented 3 years ago

Section 3: Rendering Conditional Content & Lists

Module Content

  • Rendering content with conditions
  • Outputting lists of data
  • A first look behind the scenes

codes: lists-cond-01-starting-setup.zip


Understanding the Problem
Rendering Content Conditionally
v-if, v-else and v-else-if
Using v-show instead Of v-if
Rendering Lists of Data
Diving Deeper into v-for
Removing List Items
Lists & Keys

Assignment 5: Time to Practice: Conditional Content & Lists


Module Summary