<template> <div :class="{ 'news-active': isActive }"> <ul> <li v-for="item in news">{{ item.title }}</li> </ul> </div> </template> <script> export default { data() { return { isActive: true, news: [ { title: 'Vue.js 3.0 发布了' }, { title: 'Vue 2.x 开发指南' }, { title: '使用 Vuex 管理应用状态' } ] } } } </script>
<div :class="{ 'news-active': isActive }">
Use the v-bind
directive to bind a dynamic class style. A change in isActive status will update class="news-active"
or remove the class.
isEnabled
:
<template> <div> <p v-if="isEnabled">这段文本会在isEnabled为真时渲染</p> <p v-else>这段文本会在isEnabled为假时渲染</p> </div> </template> <script> export default { data() { return { isEnabled: true } } } </script>
isEnabled
is true, the One <p>
element will be displayed; however, when isEnabled
is false, a second <p>
element will be displayed. This forms a very powerful conditional statement.
display:none
.
<p>For example, the following code shows an example of using the v-show directive:
<template> <div> <p v-show="isVisible">这段文本会根据isVisible的值显示或者隐藏</p> </div> </template> <script> export default { data() { return { isVisible: true } } } </script>
isVisible
is true, <p> ;
elements will be displayed. When isVisible
is false, the <p>
element still exists in the DOM, but is invisible.
news
array to a DOM element:
<template> <ul> <li v-for="item in news">{{ item.title }}</li> </ul> </template> <script> export default { data() { return { news: [ { title: 'Vue.js 3.0 发布了' }, { title: 'Vue 2.x 开发指南' }, { title: '使用 Vuex 管理应用状态' } ] } } } </script>
<li>
element obtains a news title through the v-for instruction.
message
property of the Vue instance:
<template> <div> <input v-model="message" /> <p>{{ message }}</p> </div> </template> <script> export default { data() { return { message: 'Hello Vue!' } } } </script>
message
attribute is rendered into a <p>
element. However, when anything is entered in the input box, the message
property is also updated.
click
event to a button:
<template> <div> <button v-on:click="onClick">点击我</button> </div> </template> <script> export default { methods: { onClick() { console.log('Button clicked!') } } } </script>
onClick
The method will be executed when the button is clicked.
<p>In addition to the click
event, other common DOM events such as keydown
, submit
, mousemove
, etc. can be used v -on binding.
<template> <ul> <li v-for="item in news" :key="item.id">{{ item.title }}</li> </ul> </template> <script> export default { data() { return { news: [ { id: 1, title: 'Vue.js 3.0 发布了' }, { id: 2, title: 'Vue 2.x 开发指南' }, { id: 3, title: '使用 Vuex 管理应用状态' } ] } } } </script>
id
attribute of the list item is bound to the v-bind:key directive to ensure that each list item has a unique identifier.
<p>Summary:
<p>Vue’s built-in instructions provide developers with a series of convenient DOM operations and data rendering operations. Proficiency in these instructions will make it easier for developers to develop high-quality Vue applications. The above is the detailed content of Let's talk about some commonly used built-in instructions in vue. For more information, please follow other related articles on the PHP Chinese website!