💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# vue绑定元素 HTML 属性中的值应使用 v-bind 指令。 以下实例判断 class1 的值,如果为 true 使用 class1 类的样式,否则不使用该类: ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html<span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"utf-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Vue 测试实例 <span class="token">-</span> <span class="token3">菜鸟教程</span><span class="token1">(</span>runoob<span class="token1">.</span>com<span class="token1">)</span><span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>style<span class="token">></span> <span class="token1">.</span>class1<span class="token1">{</span> background<span class="token1">:</span> #<span class="token5">444</span><span class="token1">;</span> color<span class="token1">:</span> #eee<span class="token1">;</span> <span class="token1">}</span> <span class="token"><</span><span class="token">/</span>style<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.staticfile.org/vue/2.2.2/vue.min.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span>label <span class="token2">for</span><span class="token">=</span><span class="token4">"r1"</span><span class="token">></span>修改颜色<span class="token"><</span><span class="token">/</span>label<span class="token">></span><span class="token"><</span>input type<span class="token">=</span><span class="token4">"checkbox"</span> v<span class="token">-</span>model<span class="token">=</span><span class="token4">"use"</span> id<span class="token">=</span><span class="token4">"r1"</span><span class="token">></span> <span class="token"><</span>br<span class="token">></span><span class="token"><</span>br<span class="token">></span> <span class="token"><</span>div v<span class="token">-</span>bind<span class="token1">:</span><span class="token2">class</span><span class="token">=</span><span class="token4">"{'class1': use}"</span><span class="token">></span> v<span class="token">-</span>bind<span class="token1">:</span><span class="token2">class</span> 指令 <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span><span class="token1">{</span> use<span class="token1">:</span> <span class="token5">false</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> ``` ``` v-if 指令将根据表达式 seen 的值(true 或 false )来决定是否插入 p 元素。 ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html<span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"utf-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Vue 测试实例 <span class="token">-</span> <span class="token3">菜鸟教程</span><span class="token1">(</span>runoob<span class="token1">.</span>com<span class="token1">)</span><span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.staticfile.org/vue/2.2.2/vue.min.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span>p v<span class="token">-</span><span class="token2">if</span><span class="token">=</span><span class="token4">"seen"</span><span class="token">></span>现在你看到我了<span class="token"><</span><span class="token">/</span>p<span class="token">></span> <span class="token"><</span>template v<span class="token">-</span><span class="token2">if</span><span class="token">=</span><span class="token4">"ok"</span><span class="token">></span> <span class="token"><</span>h1<span class="token">></span>菜鸟教程<span class="token"><</span><span class="token">/</span>h1<span class="token">></span> <span class="token"><</span>p<span class="token">></span>学的不仅是技术,更是梦想!<span class="token"><</span><span class="token">/</span>p<span class="token">></span> <span class="token"><</span>p<span class="token">></span>哈哈哈,打字辛苦啊!!!<span class="token"><</span><span class="token">/</span>p<span class="token">></span> <span class="token"><</span><span class="token">/</span>template<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> seen<span class="token1">:</span> <span class="token5">true</span><span class="token1">,</span> ok<span class="token1">:</span> <span class="token5">true</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ``` # 参数在指令后以冒号指明。例如, v-bind 指令被用来响应地更新 HTML 属性: ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html<span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"utf-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Vue 测试实例 <span class="token">-</span> <span class="token3">菜鸟教程</span><span class="token1">(</span>runoob<span class="token1">.</span>com<span class="token1">)</span><span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.staticfile.org/vue/2.2.2/vue.min.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span>pre<span class="token">></span><span class="token"><</span>a v<span class="token">-</span>bind<span class="token1">:</span>href<span class="token">=</span><span class="token4">"url"</span><span class="token">></span>菜鸟教程<span class="token"><</span><span class="token">/</span>a<span class="token">></span><span class="token"><</span><span class="token">/</span>pre<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> url<span class="token1">:</span> <span class="token4">'http://www.runoob.com'</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ``` # **在 input 输入框中我们可以使用 v-model 指令来实现双向数据绑定:** ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html<span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"utf-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Vue 测试实例 <span class="token">-</span> <span class="token3">菜鸟教程</span><span class="token1">(</span>runoob<span class="token1">.</span>com<span class="token1">)</span><span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.staticfile.org/vue/2.2.2/vue.min.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span>p<span class="token">></span><span class="token1">{</span><span class="token1">{</span> message <span class="token1">}</span><span class="token1">}</span><span class="token"><</span><span class="token">/</span>p<span class="token">></span> <span class="token"><</span>input v<span class="token">-</span>model<span class="token">=</span><span class="token4">"message"</span><span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> message<span class="token1">:</span> <span class="token4">'Runoob!'</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ``` 按钮的事件我们可以使用 v-on 监听事件,并对用户的输入进行响应。 以下实例在用户点击按钮后对字符串进行反转操作: ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token5">DOCTYPE</span> html<span class="token">></span> <span class="token"><</span>html<span class="token">></span> <span class="token"><</span>head<span class="token">></span> <span class="token"><</span>meta charset<span class="token">=</span><span class="token4">"utf-8"</span><span class="token">></span> <span class="token"><</span>title<span class="token">></span>Vue 测试实例 <span class="token">-</span> <span class="token3">菜鸟教程</span><span class="token1">(</span>runoob<span class="token1">.</span>com<span class="token1">)</span><span class="token"><</span><span class="token">/</span>title<span class="token">></span> <span class="token"><</span>script src<span class="token">=</span><span class="token4">"https://cdn.staticfile.org/vue/2.2.2/vue.min.js"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>head<span class="token">></span> <span class="token"><</span>body<span class="token">></span> <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span>p<span class="token">></span><span class="token1">{</span><span class="token1">{</span> message <span class="token1">}</span><span class="token1">}</span><span class="token"><</span><span class="token">/</span>p<span class="token">></span> <span class="token"><</span>button v<span class="token">-</span>on<span class="token1">:</span>click<span class="token">=</span><span class="token4">"reverseMessage"</span><span class="token">></span>反转字符串<span class="token"><</span><span class="token">/</span>button<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>script<span class="token">></span> <span class="token2">new</span> <span class="token3">Vue</span><span class="token1">(</span><span class="token1">{</span> el<span class="token1">:</span> <span class="token4">'#app'</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> message<span class="token1">:</span> <span class="token4">'Runoob!'</span> <span class="token1">}</span><span class="token1">,</span> methods<span class="token1">:</span> <span class="token1">{</span> reverseMessage<span class="token1">:</span> <span class="token2">function</span> <span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">this</span><span class="token1">.</span>message <span class="token">=</span> <span class="token2">this</span><span class="token1">.</span>message<span class="token1">.</span><span class="token3">split</span><span class="token1">(</span><span class="token4">''</span><span class="token1">)</span><span class="token1">.</span><span class="token3">reverse</span><span class="token1">(</span><span class="token1">)</span><span class="token1">.</span><span class="token3">join</span><span class="token1">(</span><span class="token4">''</span><span class="token1">)</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> <span class="token"><</span><span class="token">/</span>body<span class="token">></span> <span class="token"><</span><span class="token">/</span>html<span class="token">></span> ``` ``` 常用的缩写: ### v-bind 缩写 Vue.js 为两个最为常用的指令提供了特别的缩写: ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">!</span><span class="token">--</span> 完整语法 <span class="token">--</span><span class="token">></span> <span class="token"><</span>a v<span class="token">-</span>bind<span class="token1">:</span>href<span class="token">=</span><span class="token4">"url"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>a<span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 缩写 <span class="token">--</span><span class="token">></span> <span class="token"><</span>a <span class="token1">:</span>href<span class="token">=</span><span class="token4">"url"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>a<span class="token">></span> ``` ``` ### v-on 缩写 ``` <pre class="calibre14">``` <span class="token"><</span><span class="token">!</span><span class="token">--</span> 完整语法 <span class="token">--</span><span class="token">></span> <span class="token"><</span>a v<span class="token">-</span>on<span class="token1">:</span>click<span class="token">=</span><span class="token4">"doSomething"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>a<span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 缩写 <span class="token">--</span><span class="token">></span> <span class="token"><</span>a @click<span class="token">=</span><span class="token4">"doSomething"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>a<span class="token">></span> ``` ```