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

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.

主站蜘蛛池模板: 江阴市| 绥化市| 武夷山市| 山阳县| 阜康市| 白银市| 罗江县| 密山市| 哈巴河县| 获嘉县| 海门市| 读书| 成武县| 安岳县| 临城县| 上饶市| 庆城县| 聊城市| 万载县| 盐津县| 崇礼县| 姜堰市| 宝山区| 丰城市| 宜兴市| 大同县| 海城市| 凤山县| 梅河口市| 炉霍县| 西乡县| 白银市| 都江堰市| 洛川县| 平昌县| 普陀区| 丹凤县| 钟祥市| 扎赉特旗| 科技| 巴青县|