u1jodi1q 发表于 2024-10-10 04:43:49

迈入前端框架 vue 的第1脚,你必须晓得的模板语法


    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">迈入前端框架 vue 的<span style="color: black;">第1</span>脚,你必须<span style="color: black;">晓得</span>的模板语法</span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">目录</p>文本插值属性绑定列表渲染<span style="color: black;">要求</span>渲染事件处理<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在理解模板语法之前,<span style="color: black;">咱们</span>先来<span style="color: black;">瞧瞧</span>浏览器<span style="color: black;">能够</span>识别的 html 代码是什么样子的</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">你好啊</div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">但<span style="color: black;">倘若</span><span style="color: black;">咱们</span><span style="color: black;">期盼</span>变量<span style="color: black;">能够</span>展示出来,那该怎么做?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//&nbsp;js</p>var&nbsp;data&nbsp;=&nbsp;{name:<span style="color: black;">zhy</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//&nbsp;html</p>&lt;div&gt;&nbsp;你好啊&nbsp;data.name&nbsp;&lt;/div&gt;&nbsp;&nbsp;//&nbsp;<span style="color: black;">咱们</span>想把数据和html字符串<span style="color: black;">相关</span>起来,除了先<span style="color: black;">经过</span>document.querySelector(<span style="color: black;">"div"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)获取这个div<span style="color: black;">而后</span>再更改其中内容,还有其他办法吗</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">很<span style="color: black;">显著</span>,<span style="color: black;">咱们</span><span style="color: black;">这般</span>做的目的是为了<span style="color: black;">能够</span>直接在标签里面<span style="color: black;">相关</span><span style="color: black;">咱们</span>的数据,<span style="color: black;">那样</span>原生 html <span style="color: black;">供给</span>给<span style="color: black;">咱们</span>这样的能力了吗?到<span style="color: black;">日前</span>为止,显然还<span style="color: black;">无</span>。那<span style="color: black;">咱们</span>该怎么做呢?</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">vue 给<span style="color: black;">咱们</span><span style="color: black;">供给</span>了一种<span style="color: black;">办法</span>,那<span style="color: black;">便是</span>模板语法,让<span style="color: black;">咱们</span><span style="color: black;">能够</span>很<span style="color: black;">容易</span>在标签里面<span style="color: black;">相关</span><span style="color: black;">咱们</span>的响应式的数据。模板语法是 vue 响应式中重要的一环,利用ref等响应式函数(<span style="color: black;">咱们</span><span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">叫作</span>它为 hook)创建的变量,利用插值语法{{}}<span style="color: black;">相关</span>到模板中,<span style="color: black;">而后</span>响应式数据<span style="color: black;">出现</span>变化,<span style="color: black;">咱们</span>的模板中相应的数据<span style="color: black;">亦</span>会变化,这<span style="color: black;">便是</span>模板语法所起到的响应式方面的<span style="color: black;">功效</span>。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">文本插值</span></h3>import&nbsp;{&nbsp;ref&nbsp;}&nbsp;from&nbsp;<span style="color: black;">vue</span>const&nbsp;name&nbsp;=&nbsp;ref(<span style="color: black;">"zhy"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;//&nbsp;定义<span style="color: black;">咱们</span>的响应式数据</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">&nbsp;你好啊&nbsp;{{&nbsp;name&nbsp;}}&nbsp;</div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">实质</span>上,文本插值之中的内容是一个响应式的变量,是一个变量,既然是变量就属于表达式,<span style="color: black;">因此</span>文本插值之中<span style="color: black;">能够</span>编写JavaScript的表达式。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">&nbsp;你好啊&nbsp;{{&nbsp;name&nbsp;+&nbsp;"!!"&nbsp;}}&nbsp;</div>
    <div style="color: black; text-align: left; margin-bottom: 10px;">&nbsp;{{&nbsp;2.99999.toFixed(2)&nbsp;}}&nbsp;</div>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">属性绑定</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一般</span>在标签内部<span style="color: black;">咱们</span>会去经常书写属性,<span style="color: black;">那样</span><span style="color: black;">怎样</span>动态的绑定属性呢?<span style="color: black;">运用</span> v-bind 指令(或其简写:)来动态绑定属性或对象的值到元素上。</p>import&nbsp;{&nbsp;ref&nbsp;}&nbsp;from&nbsp;<span style="color: black;">vue</span>const&nbsp;id&nbsp;=&nbsp;ref(<span style="color: black;">"name"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;//&nbsp;定义<span style="color: black;">咱们</span>的响应式数据</p>&nbsp;&lt;div&nbsp;v-bind:id=<span style="color: black;">"id"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&gt;&nbsp;‌属性绑定&nbsp;</p>&nbsp;&lt;div&nbsp;:id=<span style="color: black;">"id"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&gt;&nbsp;‌属性绑定&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>当<span style="color: black;">咱们</span>不确定需要绑定的属性到底是 id 还是 class <span style="color: black;">或</span>是其他该怎么做?这个时候就要用到动态属性绑定了</p>const&nbsp;attr&nbsp;=&nbsp;ref(<span style="color: black;">id</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>const&nbsp;value&nbsp;=&nbsp;ref(<span style="color: black;">"value"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>const&nbsp;changeAttr&nbsp;=&nbsp;<span style="color: black;"><span style="color: black;">function</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(){</p>&nbsp;&nbsp;&nbsp;&nbsp;attr.value&nbsp;=&nbsp;<span style="color: black;">class</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;利用[]将变量属性包裹起来就<span style="color: black;">能够</span>做到响应式</p>&nbsp;&lt;div&nbsp;v-bind:=<span style="color: black;">"value"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&gt;&nbsp;‌动态属性绑定&nbsp;{{attr}}&nbsp;</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">列表渲染</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假如说有一组数据,需要重复的渲染呢?例如书籍数据。<span style="color: black;">运用</span> v-for 指令来循环渲染数组或对象,生成一系列元素。</p>const&nbsp;booksRef&nbsp;=&nbsp;ref([<span style="color: black;">"book1"</span>,&nbsp;<span style="color: black;">"book2"</span>,&nbsp;<span style="color: black;">"book3"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">]);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&nbsp;v-for=<span style="color: black;">"item&nbsp;in&nbsp;booksRef"</span>&nbsp;:key=<span style="color: black;">"item"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&gt;{{&nbsp;item&nbsp;}}</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">‌ <span style="color: black;">要求</span>渲染</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span> v-if、v-else-if 和 v-else 来<span style="color: black;">掌控</span>元素的<span style="color: black;">表示</span>或<span style="color: black;">隐匿</span>,<span style="color: black;">按照</span><span style="color: black;">要求</span>渲染<span style="color: black;">区别</span>的内容。‌</p>const&nbsp;isShow&nbsp;=&nbsp;ref(<span style="color: black;">true</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)&nbsp;//&nbsp;定义<span style="color: black;">咱们</span>的响应式数据</p>&lt;p&nbsp;v-if=<span style="color: black;">"isShow"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&gt;<span style="color: black;">要求</span>为真时这个p元素会渲染到页面中。。。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要求</span>为假时这个p元素会渲染到页面中。。。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">事件处理</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在原生<span style="color: black;">其中</span>经常会有点击某个原生进行事件处理。<span style="color: black;">运用</span> v-on 指令(或简写@)来监听 DOM 事件并执行<span style="color: black;">有些</span> JavaScript 代码。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">const&nbsp;btnClick&nbsp;=&nbsp;funciton(e){</p>&nbsp;&nbsp;&nbsp;&nbsp;console.log(<span style="color: black;">事件处理程序</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,event)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>&nbsp;&nbsp;&nbsp;&lt;button&nbsp;@click=<span style="color: black;">"btnClick"</span>&gt;点我进行事件处理&lt;/button&gt;&nbsp;&nbsp;//&nbsp;@事件类型=<span style="color: black;">"事件处理<span style="color: black;">办法</span>"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;类型为点击事件click,表单提交事件submit等</p>




qzmjef 发表于 2024-10-13 12:18:31

我赞同你的看法,你的智慧让人佩服,谢谢分享。
页: [1]
查看完整版本: 迈入前端框架 vue 的第1脚,你必须晓得的模板语法