官术网_书友最值得收藏!

Adding basic interactivity with directives

Let's add some interactivity to our otherwise quite static app, for example, a text input that will allow the user to change the message displayed. We can do that in templates with special HTML attributes called directives.

All the directives in Vue start with v- and follow the kebab-case syntax. That means you should separate the words with a dash. Remember that HTML attributes are case insensitive (whether they are uppercase or lowercase doesn't matter).

The directive we need here is v-model, which will bind the value of our <input> element with our message data property. Add a new <input> element with the v-model="message" attribute inside the template:

<p id="root">
<p>{{ message }}</p>
<!-- New text input -->
<input v-model="message" />
</p>

Vue will now update the message property automatically when the input value changes. You can play with the content of the input to verify that the text updates as you type and the value in the devtools changes:

There are many more directives available in Vue, and you can even create your own. Don't worry, we will cover that in later chapters.

主站蜘蛛池模板: 永平县| 曲麻莱县| 成都市| 洮南市| 德清县| 富裕县| 扬中市| 西藏| 蓬安县| 石嘴山市| 寻甸| 宝兴县| 郸城县| 政和县| 馆陶县| 伊吾县| 华安县| 昂仁县| 凤凰县| 乳山市| 东丽区| 潜江市| 江源县| 房山区| 门头沟区| 富民县| 同江市| 濮阳县| 玛多县| 通辽市| 仁化县| 太白县| 新源县| 武城县| 盐亭县| 商南县| 文水县| 长兴县| 盘锦市| 和平区| 红桥区|