💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 基础语法 - 简单例子 ``` <pre class="calibre14">``` <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token2">{</span><span class="token2">{</span> message <span class="token2">}</span><span class="token2">}</span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>script<span class="token">></span> var app <span class="token">=</span> <span class="token5">new</span> <span class="token1">Vue</span><span class="token2">(</span><span class="token2">{</span> el<span class="token2">:</span> <span class="token4">'#app'</span><span class="token2">,</span> data<span class="token2">:</span> <span class="token2">{</span> message<span class="token2">:</span> <span class="token4">'测试vue!'</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">)</span> <span class="token"><</span><span class="token">/</span>script<span class="token">></span> ``` ``` ``` <pre class="calibre14">``` <span class="token"><</span>div id<span class="token">=</span><span class="token4">"app"</span><span class="token">></span> <span class="token"><</span>ul<span class="token">></span><span class="token"><</span>li v<span class="token">-</span><span class="token5">for</span><span class="token">=</span><span class="token4">"rs in todos"</span><span class="token">></span> <span class="token2">{</span><span class="token2">{</span>rs<span class="token2">.</span>text<span class="token2">}</span><span class="token2">}</span><span class="token"><</span><span class="token">/</span>li<span class="token">></span><span class="token"><</span><span class="token">/</span>ul<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> var app <span class="token">=</span> <span class="token5">new</span> <span class="token1">Vue</span><span class="token2">(</span><span class="token2">{</span> el<span class="token2">:</span> <span class="token4">'#app'</span><span class="token2">,</span> data<span class="token2">:</span> <span class="token2">{</span> todos<span class="token2">:</span> <span class="token2">[</span> <span class="token2">{</span> text<span class="token2">:</span> <span class="token4">'学习 JavaScript'</span> <span class="token2">}</span><span class="token2">,</span> <span class="token2">{</span> text<span class="token2">:</span> <span class="token4">'学习 Vue'</span> <span class="token2">}</span><span class="token2">,</span> <span class="token2">{</span> text<span class="token2">:</span> <span class="token4">'整个牛项目'</span> <span class="token2">}</span> <span class="token2">]</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">)</span> ``` ``` - 模板语法 ``` <pre class="calibre14">``` 一次插值<span class="token2">,</span>后面不会改变 v<span class="token">-</span>once 输出原始值 <span class="token"><</span>span v<span class="token">-</span>html<span class="token2">:</span>style<span class="token">=</span><span class="token4">"msg"</span><span class="token">></span>你好<span class="token"><</span><span class="token">/</span>span<span class="token">></span> 输出属性 <span class="token"><</span>span v<span class="token">-</span>bind<span class="token2">:</span>style<span class="token">=</span><span class="token4">"msg"</span><span class="token">></span>你好<span class="token"><</span><span class="token">/</span>span<span class="token">></span> <span class="token5">if</span>语句 <span class="token"><</span>span v<span class="token">-</span><span class="token5">if</span><span class="token">=</span><span class="token4">"msg"</span><span class="token">></span><span class="token3">true</span>看见我<span class="token"><</span><span class="token">/</span>span<span class="token">></span> <span class="token"><</span>h1 v<span class="token">-</span><span class="token5">if</span><span class="token">=</span><span class="token4">"msg=='ok'"</span><span class="token">></span>是<span class="token"><</span><span class="token">/</span>h1<span class="token">></span> <span class="token"><</span>h1 v<span class="token">-</span><span class="token5">else</span><span class="token">-</span><span class="token5">if</span><span class="token">=</span><span class="token4">"msg=='ok1'"</span><span class="token">></span>是<span class="token3">1</span><span class="token"><</span><span class="token">/</span>h1<span class="token">></span> <span class="token"><</span>h1 v<span class="token">-</span><span class="token5">else</span><span class="token">></span>否<span class="token"><</span><span class="token">/</span>h1<span class="token">></span> 管理重复数据 key<span class="token">=</span><span class="token4">"username-input"</span> 监听事件 <span class="token"><</span>a v<span class="token">-</span>on<span class="token2">:</span>click<span class="token">=</span><span class="token4">"alert(1)"</span><span class="token">></span><span class="token3">123</span><span class="token"><</span><span class="token">/</span>a<span class="token">></span> v<span class="token">-</span>bind缩写 <span class="token2">:</span> v<span class="token">-</span>on 缩写@ 数组方式调用class <span class="token"><</span>div v<span class="token">-</span>bind<span class="token2">:</span>class<span class="token">=</span><span class="token4">"[activeClass, errorClass]"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>div<span class="token">></span> 绑定内联样式 <span class="token"><</span>div v<span class="token">-</span>bind<span class="token2">:</span>style<span class="token">=</span><span class="token4">"{ color: activeColor, fontSize: fontSize + 'px' }"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>div<span class="token">></span> 显示隐藏 <span class="token"><</span>h1 v<span class="token">-</span>show<span class="token">=</span><span class="token4">"msg=='ok'"</span><span class="token">></span>是<span class="token"><</span><span class="token">/</span>h1<span class="token">></span> 不支持<span class="token"><</span>template<span class="token">></span>和<span class="token5">else</span><span class="token2">,</span>是display显示隐藏 列表渲染 <span class="token"><</span>li v<span class="token">-</span><span class="token5">for</span><span class="token">=</span><span class="token4">"item in items"</span><span class="token">></span> <span class="token2">{</span><span class="token2">{</span> item<span class="token2">.</span>message <span class="token2">}</span><span class="token2">}</span> <span class="token"><</span><span class="token">/</span>li<span class="token">></span> 带索引 <span class="token"><</span>li v<span class="token">-</span><span class="token5">for</span><span class="token">=</span><span class="token4">"(item, index) in items"</span><span class="token">></span> <span class="token2">{</span><span class="token2">{</span> parentMessage <span class="token2">}</span><span class="token2">}</span> <span class="token">-</span> <span class="token2">{</span><span class="token2">{</span> index <span class="token2">}</span><span class="token2">}</span> <span class="token">-</span> <span class="token2">{</span><span class="token2">{</span> item<span class="token2">.</span>message <span class="token2">}</span><span class="token2">}</span> <span class="token"><</span><span class="token">/</span>li<span class="token">></span> 对象也支持三个参数 <span class="token"><</span>div v<span class="token">-</span><span class="token5">for</span><span class="token">=</span><span class="token4">"(value, key, index) in object"</span><span class="token">></span> <span class="token2">{</span><span class="token2">{</span> index <span class="token2">}</span><span class="token2">}</span><span class="token2">.</span> <span class="token2">{</span><span class="token2">{</span> key <span class="token2">}</span><span class="token2">}</span><span class="token2">:</span> <span class="token2">{</span><span class="token2">{</span> value <span class="token2">}</span><span class="token2">}</span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> 唯一性 <span class="token"><</span>div v<span class="token">-</span><span class="token5">for</span><span class="token">=</span><span class="token4">"item in items"</span> <span class="token2">:</span>key<span class="token">=</span><span class="token4">"item.id"</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> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> 方法 <span class="token"><</span>button v<span class="token">-</span>on<span class="token2">:</span>click<span class="token">=</span><span class="token4">"fn"</span><span class="token">></span>创建<span class="token"><</span><span class="token">/</span>button<span class="token">></span> var app <span class="token">=</span> <span class="token5">new</span> <span class="token1">Vue</span><span class="token2">(</span><span class="token2">{</span> el<span class="token2">:</span> <span class="token4">'#app'</span><span class="token2">,</span> data<span class="token2">:</span> <span class="token2">{</span>msg<span class="token2">:</span><span class="token4">'ok1'</span> <span class="token2">}</span> <span class="token2">,</span>methods<span class="token2">:</span><span class="token2">{</span> <span class="token4">'fn'</span><span class="token2">:</span><span class="token5">function</span><span class="token2">(</span>event<span class="token2">)</span><span class="token2">{</span> <span class="token1">alert</span><span class="token2">(</span><span class="token4">"q"</span><span class="token">+</span>this<span class="token2">.</span>msg<span class="token2">)</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">)</span> ``` ``` - 事件修饰符 ``` <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="token2">:</span>click<span class="token2">.</span>stop<span class="token">=</span><span class="token4">"doThis"</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>form v<span class="token">-</span>on<span class="token2">:</span>submit<span class="token2">.</span>prevent<span class="token">=</span><span class="token4">"onSubmit"</span><span class="token">></span><span class="token"><</span><span class="token">/</span>form<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 v<span class="token">-</span>on<span class="token2">:</span>click<span class="token2">.</span>stop<span class="token2">.</span>prevent<span class="token">=</span><span class="token4">"doThat"</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>form v<span class="token">-</span>on<span class="token2">:</span>submit<span class="token2">.</span>prevent<span class="token">></span><span class="token"><</span><span class="token">/</span>form<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><span class="token">!</span><span class="token">--</span> 即元素自身触发的事件先在此处处理,然后才交由内部元素进行处理 <span class="token">--</span><span class="token">></span> <span class="token"><</span>div v<span class="token">-</span>on<span class="token2">:</span>click<span class="token2">.</span>capture<span class="token">=</span><span class="token4">"doThis"</span><span class="token">></span><span class="token2">.</span><span class="token2">.</span><span class="token2">.</span><span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 只当在 event<span class="token2">.</span>target 是当前元素自身时触发处理函数 <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> <span class="token"><</span>div v<span class="token">-</span>on<span class="token2">:</span>click<span class="token2">.</span>self<span class="token">=</span><span class="token4">"doThat"</span><span class="token">></span><span class="token2">.</span><span class="token2">.</span><span class="token2">.</span><span class="token"><</span><span class="token">/</span>div<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 v<span class="token">-</span>on<span class="token2">:</span>click<span class="token2">.</span>once<span class="token">=</span><span class="token4">"doThis"</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="token2">(</span>即滚动行为<span class="token2">)</span> 将会立即触发 <span class="token">--</span><span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 而不会等待 `onScroll` 完成 <span class="token">--</span><span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 这其中包含 `event<span class="token2">.</span><span class="token1">preventDefault</span><span class="token2">(</span><span class="token2">)</span>` 的情况 <span class="token">--</span><span class="token">></span> <span class="token"><</span>div v<span class="token">-</span>on<span class="token2">:</span>scroll<span class="token2">.</span>passive<span class="token">=</span><span class="token4">"onScroll"</span><span class="token">></span><span class="token2">.</span><span class="token2">.</span><span class="token2">.</span><span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 只有在 `keyCode` 是 <span class="token3">13</span> 时调用 `vm<span class="token2">.</span><span class="token1">submit</span><span class="token2">(</span><span class="token2">)</span>` <span class="token">--</span><span class="token">></span> <span class="token"><</span>input v<span class="token">-</span>on<span class="token2">:</span>keyup<span class="token2">.</span><span class="token3">13</span><span class="token">=</span><span class="token4">"submit"</span><span class="token">></span> <span class="token2">.</span>enter <span class="token2">.</span>tab <span class="token2">.</span>delete <span class="token2">(</span>捕获“删除”和“退格”键<span class="token2">)</span> <span class="token2">.</span>esc <span class="token2">.</span>space <span class="token2">.</span>up <span class="token2">.</span>down <span class="token2">.</span>left <span class="token2">.</span>right ``` ``` - 表单 ``` <pre class="calibre16">``` <span class="token"><</span>input v<span class="token">-</span>model<span class="token">=</span><span class="token4">"message"</span> placeholder<span class="token">=</span><span class="token4">"edit me"</span><span class="token">></span> <span class="token"><</span>input type<span class="token">=</span><span class="token4">"checkbox"</span> id<span class="token">=</span><span class="token4">"checkbox"</span> v<span class="token">-</span>model<span class="token">=</span><span class="token4">"checked"</span><span class="token">></span> <span class="token"><</span><span class="token">!</span><span class="token">--</span> 在“change”时而非“input”时更新 <span class="token">--</span><span class="token">></span> <span class="token"><</span>input v<span class="token">-</span>model<span class="token2">.</span>lazy<span class="token">=</span><span class="token4">"msg"</span> <span class="token">></span> <span class="token"><</span>input v<span class="token">-</span>model<span class="token2">.</span>number<span class="token">=</span><span class="token4">"age"</span> type<span class="token">=</span><span class="token4">"number"</span><span class="token">></span> <span class="token"><</span>input v<span class="token">-</span>model<span class="token2">.</span>trim<span class="token">=</span><span class="token4">"msg"</span><span class="token">></span> ``` ```