└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Vue Vixens DC's Hello World Meetup - Intro to Vue 2 | 3 |  4 | 5 | Vue.js is a frontend framework that is optimized for **progressive integration**. That means you can have a large app with only a couple Vue components integrated -- or you could start from scratch and work completely within the Vue ecosystem. 6 | 7 | Another thing that sets Vue apart is the lower learning curve than a lot of frameworks. Instead of having to understand complex topics, if you know HTML, CSS, and JavaScript, you're already pretty close! 8 | 9 | Like any framework, it adds a structure and utilities to your frontend so that your app is easier to extend as it grows, is more organized, and you don't have to "reinvent the wheel" as often. 10 | 11 | Vue is also really cool because it's ecosystem is really well integrated -- a lot of the utilities that would normally be 3rd party libraries are built by the Vue core maintainers, like [Vue Router](https://router.vuejs.org/) and [Vuex](https://vuex.vuejs.org/). 12 | 13 | Throughout this workshop, we'll explore the key features of Vue, and create an app together! 14 | 15 | Here's what we'll be building, though with some more interactive features. The like button will toggle from the heart outline to the red heart based on user clicks. Also, the character number will count down when someone types in the text box. 16 | 17 | finished product - https://codepen.io/aspittel/pen/oVMBmO 18 | 19 | starting app - https://codepen.io/aspittel/pen/oVMYqG 20 | 21 | Go ahead and check out the HTML and CSS code above, we'll be building off of the HTML with our Vue code. 22 | 23 | ## Setting up a Vue App 24 | 25 | For now, we'll use a Vue CDN -- we want a minimalist setup. In the future, you may want a more extensive environment, in which case you can use the [Vue CLI](https://cli.vuejs.org/). 26 | 27 | Go to the `settings` button on Codepen, switch to the JavaScript tab, and search for Vue on CDNjs. This adds the Vue library to our project, so we can use all of the methods and features that Vue gives us. 28 | 29 | Now, we need to create a Vue instance and attach it to our HTML in order to fully integrate Vue! 30 | 31 | Let's create a `const` that stores our `Vue` instance. 32 | 33 | ```js 34 | const app = new Vue() 35 | ``` 36 | 37 | We're going to pass an object when we create this Vue app, it'll have all our configuration and application logic for now. 38 | 39 | The first thing we're going to add to that object is `el` -- which is the element that we want to be the base of our Vue app. In this case the element with the `status` class. 40 | 41 | ```js 42 | const app = new Vue({ 43 | el: ".status" 44 | }) 45 | ``` 46 | 47 | Then, we'll add our `data`. To test this out, let's add the `tweetText` as data -- so where we have `Hello World!` right now will become a variable. Down the road we're going to make more tweets with different text, so it makes sense to make that piece of the tweet dynamic. 48 | 49 | ```js 50 | const app = new Vue({ 51 | el: ".status", 52 | data: { 53 | tweetText: "Hello World!" 54 | } 55 | }) 56 | ``` 57 | 58 | When we want to add more dynamic data (or data that will change within our Vue app) we'll add more attributes to this `data` object. 59 | 60 | Now, we can use our newly created data in our HTML and plug in the variables that way! If you've ever used Handlebars or another templating language, it's kind of like that. 61 | 62 | If you go to the hardcoded "Hello World!" in the HTML, we can now replace it with `{{tweetText}}` which will pull from our Vue data! 63 | 64 | ```html 65 |
66 | {{ tweetText }} 67 |
68 | ``` 69 | 70 | Try to change your `tweetText` in Vue, and it'll change in your output as well! 71 | 72 | Let's brainstorm for a second on what other data we have that will change within the course of our app. 73 | 74 | - The heart will toggle between liked and unliked 75 | - Our characters remaining will decrease when we type in the 76 | 77 | 97 | 98 | ### Your turn: Add attributes for the `charactersRemaining` and `liked`. Add the `charactersRemaining` to the HTML too! 99 | 100 | ## Methods 101 | 102 | Now that we have our data, we need to make it update based on user actions. 103 | 104 | We're going to add another attribute to our Vue object -- this one will store our methods. 105 | 106 | ```js 107 | const app = new Vue({ 108 | el: ".status", 109 | data: { 110 | tweetText: "Hello World!", 111 | charactersRemaining: 280, 112 | liked: false 113 | }, 114 | methods: {} 115 | }) 116 | ``` 117 | 118 | We have two "actions" for our app -- toggling the like and changing the characters remaining number when the user types. Let's work on the character counting first. 119 | 120 | We'll add a method to our methods object first: 121 | 122 | ```js 123 | methods: { 124 | countCharacters: function() { 125 | 126 | } 127 | } 128 | ``` 129 | 130 | Let's think about the logic for this function: we need to count how many characters the user has typed into the `textarea`. Then, we need to subtract that count from 280 (or our character limit). 131 | 132 | Let's create a data attribute for the comment text, and then update that every time the user types in the `textarea`. 133 | 134 | ```diff 135 | data: { 136 | tweetText: 'Hello World!', 137 | charactersRemaining: 280, 138 | + commentText: '', 139 | liked: false 140 | }, 141 | ``` 142 | 143 | ```html 144 | 145 | ``` 146 | 147 | `v-model` is a _directive_ that syncs our data attribute with what the user has typed into the `textarea`. So no matter how much or little they have typed in, `commentText` will match what they've typed. To take one quick step back, _directives_ are HTML attributes that are provided by Vue, they're prefixed by `v-`. 148 | 149 | Okay, now back to our method. We can access our data in our methods with `this.myDataAttribute` ([here's](https://codeburst.io/all-about-this-and-new-keywords-in-javascript-38039f71780c) a great reference on JavaScript's `this`). 150 | 151 | So, we can update the `charactersRemaining` with the following logic: 152 | 153 | ```js 154 | methods: { 155 | countCharacters: function() { 156 | this.charactersRemaining = 280 - this.commentText.length 157 | } 158 | } 159 | ``` 160 | 161 | Now, we need to make sure that `countCharacters` runs every time the user types in the `textarea`. 162 | 163 | Luckily, Vue has the `v-on` directive, and we can add the event after it so that we run the method each time that event takes place. In this case, `v-on:input="countCharacters` will run the `countCharacters` method each time the user types in the `textarea`. 164 | 165 | ```html 166 | 171 | ``` 172 | 173 | Okay, now let's step back and work on our `toggleLike` method. 174 | 175 | ### Your turn: Create a `toggleLike` method! 176 | 177 | > Hint: instead of running this event on input, we want to run it on click! 178 | 179 | Don't worry about changing emojis quite yet, we'll go over conditionals together in a minute! 180 | 181 | 212 | 213 | 214 | ## Conditionals 215 | 216 | Vue allows us to conditionally render data with the `v-if` directive. 217 | 218 | Let's add the following span-wrapped emoji within our `reactions` div: 219 | 220 | ```html 221 | ♥️ 222 | ``` 223 | 224 | Now, our red heart emoji only shows up if `liked` is `true`. Let's also add a `v-else` to our heart outline emoji, so that it only renders if `liked` is `false`. 225 | 226 | ```html 227 | ♥️ ♡ 228 | ``` 229 | 230 | Yay! Now our likes work! 231 | 232 | If you had any issues with the above steps, here's a Codepen with what we have so far. 233 | 234 | https://codepen.io/aspittel/pen/WmKRNbG 235 | 236 | Now that we have our interaction down, how would we create a bunch more tweets with the same functionality but different state and text? Components! 237 | 238 | ## Components 239 | 240 | Similar to other frontend frameworks, Vue apps are broken down into components. We compose components together in order to create full user interfaces. A good rule of thumb is that if a chunk of the user interface is used multiple times, it should be broken into a component. 241 | 242 | In a production application, our tweet would probably be broken into subcomponents -- we may have a component for the comment text area, one for the like functionality, one for the profile picture, etc. But, for tonight, we will just make the full tweet into a component so that we can easily create a bunch more tweets. 243 | 244 | First, let's move the logic from our Vue instance into a component. 245 | 246 | The first argument to `Vue.component` is the name of the component, in this case "tweet". We're also turning data into a function that returns an object. This allows us to have multiple `tweet` component instance, each with separate data. 247 | 248 | ```js 249 | Vue.component("tweet", { 250 | data: function() { 251 | return { 252 | charactersRemaining: 280, 253 | commentText: "", 254 | liked: false 255 | } 256 | }, 257 | methods: { 258 | countCharacters: function() { 259 | this.charactersRemaining = 280 - this.commentText.length 260 | }, 261 | toggleLike: function() { 262 | this.liked = !this.liked 263 | } 264 | } 265 | }) 266 | ``` 267 | 268 | We also need the `template` for the component -- or the HTML that the component will render. We're going to grab all of the existing HTML and paste into a template attribute on our component. 269 | 270 | ```js 271 | template: `278 | {{ tweetText }} 279 |
280 |