|
34 | 34 |
|
35 | 35 | The [Installation](installation.md) page provides more options of installing Vue. Note: We **do not** recommend that beginners start with `vue-cli`, especially if you are not yet familiar with Node.js-based build tools.
|
36 | 36 |
|
37 |
| -If you prefer something more interactive, you can also check out [this tutorial series on Scrimba](https://scrimba.com/g/gvuedocs), which gives you a mix of screencast and code playground that you can pause and play around with anytime. |
| 37 | +If you prefer something more interactive, you can also check out [this tutorial series on Scrimba](https://scrimba.com/g/gvuedocs), which gives you a mix of screencast and code playground that you can pause and play around with anytime. |
| 38 | + |
| 39 | +## Declarative Rendering |
| 40 | + |
| 41 | +At the core of Vue.js is a system that enables us to declaratively render data to the DOM using straightforward template syntax: |
| 42 | + |
| 43 | +``` html |
| 44 | +<div id="app"> |
| 45 | + {{ message }} |
| 46 | +</div> |
| 47 | +``` |
| 48 | +``` js |
| 49 | +const app = new Vue({ |
| 50 | + el: '#app', |
| 51 | + data() { |
| 52 | + return { |
| 53 | + message: 'Hello Vue!' |
| 54 | + } |
| 55 | + } |
| 56 | +}) |
| 57 | +``` |
| 58 | +<intro-1 /> |
| 59 | + |
| 60 | +We have already created our very first Vue app! This looks pretty similar to rendering a string template, but Vue has done a lot of work under the hood. The data and the DOM are now linked, and everything is now **reactive**. How do we know? Change the `message` property in the code snippet below to a different value. You the rendered example update accordingly: |
| 61 | + |
| 62 | +<iframe |
| 63 | + src="https://codesandbox.io/embed/vue-basic-example-buthm?autoresize=1&fontsize=14&hidenavigation=1&moduleview=1&theme=dark" |
| 64 | + style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" |
| 65 | + title="Vue basic example" |
| 66 | + allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" |
| 67 | + sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin" |
| 68 | + ></iframe> |
| 69 | +
|
| 70 | +In addition to text interpolation, we can also bind element attributes like this: |
| 71 | + |
| 72 | +``` html |
| 73 | +<div id="app-2"> |
| 74 | + <span v-bind:title="message"> |
| 75 | + Hover your mouse over me for a few seconds |
| 76 | + to see my dynamically bound title! |
| 77 | + </span> |
| 78 | +</div> |
| 79 | +``` |
| 80 | +``` js |
| 81 | +const app2 = new Vue({ |
| 82 | + el: '#app-2', |
| 83 | + data() { |
| 84 | + return { |
| 85 | + message: 'You loaded this page on ' + new Date().toLocaleString() |
| 86 | + } |
| 87 | + } |
| 88 | +}) |
| 89 | +``` |
| 90 | +<intro-2 /> |
| 91 | +Here we are encountering something new. The `v-bind` attribute you are seeing is called a **directive**. Directives are prefixed with `v-` to indicate that they are special attributes provided by Vue, and as you may have guessed, they apply special reactive behavior to the rendered DOM. Here, it is basically saying "keep this element's `title` attribute up-to-date with the `message` property on the Vue instance." |
| 92 | + |
| 93 | +If you open up your JavaScript console again and enter `app2.message = 'some new message'`, you'll once again see that the bound HTML - in this case the `title` attribute - has been updated. |
| 94 | + |
0 commit comments