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

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.

主站蜘蛛池模板: 东城区| 西乌珠穆沁旗| 中阳县| 久治县| 新乐市| 上饶县| 浠水县| 安丘市| 邳州市| 沅江市| 内黄县| 綦江县| 屯昌县| 青神县| 苗栗市| 碌曲县| 贞丰县| 汉川市| 安义县| 宁津县| 邢台县| 修文县| 定远县| 葵青区| 烟台市| 邻水| 林西县| 武陟县| 玉田县| 梅州市| 阜康市| 松滋市| 和静县| 固镇县| 江川县| 洛浦县| 镶黄旗| 龙海市| 罗定市| 历史| 界首市|