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

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.

主站蜘蛛池模板: 仁怀市| 轮台县| 青田县| 定西市| 两当县| 诸城市| 凉城县| 天水市| 十堰市| 横峰县| 汾阳市| 宝山区| 买车| 长白| 怀远县| 岢岚县| 广安市| 五台县| 玉山县| 屏东县| 南丹县| 泽州县| 阳泉市| 额济纳旗| 岳阳县| 佛教| 福鼎市| 奈曼旗| 阜新市| 南宁市| 西宁市| 方山县| 阳东县| 南乐县| 阳谷县| 南平市| 思南县| 贺州市| 沾益县| 基隆市| 东安县|