Vue Notes 1
Vue Notes 1
js
Introduction to Vue.js
● A front-end framework
● Create JavaScript drive web applications
● Runs in the browser
● No need to make multiple server requests for pages
● Very lean production version (16kb)
● Very high run-time performance
Installing Vue.js
How to Install Vue.js the easy way using CDN (Content Delivery Network):
● Conditionals
● Events
● Data
*** Installing Vue.js with the vue-cli and webpack is typically used for larger files.
*** Get Vue on (https://vuejs.org/v2/guide/)
*** In talking about JavaScript, "instance" can be used informally to mean an
object created using a particular constructor function.
*** Repository of the Sample Vue.js Code in the tutorial:
https://github.com/iamshaunjp/vuejs-playlist
https://github.com/iamshaunjp/vuejs-playlist/blob
Figure 1.0 - Installation of Vue.js the Easy Way through CDN
Input:
Figure 1.1 - Sample Vue Instance #1
Output:
Figure 1.3 - Sample Vue Instance #3
Input:
Figure 1.0 - Sample Vue Instance with Method #1
Output:
Figure 1.2 - Sample Vue Instance with Method #3
Data Binding
● Bind dynamic data to HTML elements
● Use of v-bind: directive to bind dynamic data that was stored on view instance
● The @ character can be used as shorthand for the v-on: directive
● V-bind can be used to different HTML attributes
Input:
Figure 1.0 - Use of V-bind Directive on Href and Value #1
Input:
Figure 1.3 - Property containing HTML Tag #1
Output:
Events
● The v-on: directive is used to create HTML events
Input:
Output:
Input:
Figure 1.6 - V-on:mousemove Directive #1
Output:
Event Modifiers
● DOM event handlers can have modifiers that alter their behaviour.
● Types of Event Modifiers: once, prevent, stop, capture, self, passive
● For example, an event with once modifier will execute a function only once
Figure 1.9 - Once Event Modifier
Keyboard Events
● Three types of keyboard events:
○ Keydown - Keydown happens when the key is pressed down, and auto
repeats if the key is pressed down for long.
○ Keypress - This event is fired when an alphabetic, numeric, or
punctuation key is pressed down.
○ Keyup - Keyup happens when the key is released
● Key Modifiers: enter, tab, delete, esc, space, up, down, left, right
Input:
Figure 1.0 - Keyup Event with Alt and Enter Key Modifiers #1
Figure 1.1 - Keyup Event with Alt and Enter Key Modifiers #2
Output:
Figure 1.2 - Keyup Event with Alt and Enter Key Modifiers #3
Input:
Figure 1.0 - V-model Sample #1
Output:
Input:
Output:
Output:
Input:
Figure 1.0 - Dynamic CSS Class #1
Output:
Input:
Figure 1.0 - V-if Sample #1
Output:
Figure 1.2 - V-if Sample #3
Input:
Input:
Figure 1.0 - Loop through Characters List using V-for #1
Output:
Figure 1.2 - Loop through Characters List using V-for #3
Input:
Output:
Figure 1.4 - Loop through Ninjas List using V-for #2
Input:
Output:
Figure 1.6 - Output Index #2
Input:
Figure 1.0 - HTML Punch Bag Game #1
Figure 1.1 - CSS Punch Bag Game #2
Figure 1.2 - JS Punch Bag Game #3
Output:
Figure 1.2 - Punch Bag Game #4
Input:
Figure 1.0 - HTML Vue Instance #1
Figure 1.1 - JS Vue Instance #2
Introduction to Components
● A component is a reusable piece of code or template that can be used in different
vue instances
● Define a new component using Vue.component() method
● When registering a component, it will always be given a name. For example, in
the global registration: Vue.component('my-component-name', { /* ... */ })
● The component’s name is the first argument of Vue.component
● There are two options when defining component names: kebab-case and
PascalCase
● When defining a component with PascalCase, you can use either case when
referencing its custom element. That means both <my-component-name> and
<MyComponentName> are acceptable. Note, however, that only kebab-case
names are valid directly in the DOM (i.e. non-string templates).
● The Vue.component() method takes two parameters. The first parameter is the
name of the component and the second parameter is an object.
● In Vue.component(), the data is not an object, rather, it must be a function.
Input:
Figure 1.0 - HTML Vue Component #1
Refs
● Referencing with $refs
● Use refs to reach into the DOM and access information such as text input values,
inner HTML and more.
● Despite the existence of props and events, sometimes you might still need to
directly access a child component in JavaScript. To achieve this you can assign a
reference ID to the child component or HTML element using the ref attribute.
● Note: $refs are only populated after the component has been rendered. It is only
meant as an escape hatch for direct child manipulation - you should avoid
accessing $refs from within templates or computed properties.
● Props are custom attributes you can register on a component. When a value is
passed to a prop attribute, it becomes a property on that component instance. To
pass a title to our blog post component, we can include it in the list of props this
component accepts, using a prop option:
Vue.component('blog-post', {
props: ['title'],
template: '<h3>{{ title }}</h3>'
})
● A component can have as many props as you’d like and by default, any value
can be passed to any prop. In the template above, you’ll see that we can access
this value on the component instance, just like with data.
● Once a prop is registered, you can pass data to it as a custom attribute, like this:
● Above, you’ll see that we can use v-bind to dynamically pass props. This is
especially useful when you don’t know the exact content you’re going to render
ahead of time, like when fetching posts from an API.
Input:
Output:
Installation:
Figure 1.0 - Install Vue-CLI #1
Slots
● Vue implements a content distribution API that’s modeled after the current Web
Components spec draft, using the <slot> element to serve as distribution outlets
for content.
Figure 1.0 - Slot Sample on App.vue #1
Figure 1.1 - Slot Sample on formHelper.vue #2
Dynamic Components
● Using the 'component' tag Vue JS provides us with. Dynamic components allow
us to dynamically change which component is output to the browser.
● The is attribute to switch between components in a tabbed interface:
.
Retrieving Posts from Firebase (Hooking up to Firebase #1)