寒假学习day1(Vue)
一,声明式渲染
1. 关于组合式api,通常会和script setup 搭配使用
2.在script中,使用ref或者reactive来声明响应式状态
3.在script中,将组件声明,并且有组件逻辑和其他一些响应式状态。
4.然后在template中对调取对应的响应式状态的值来进行文本渲染
举例
1 |
|
二,Attribute绑定
1.在template中为了给attribute绑定一个动态值,一般都是需要v-bind指令,而且可以进行缩写
1 |
|
2.还有bool型,当 isButtonDisabled
为真值或一个空字符串 (即 <button disabled="">
) 时,元素会包含这个 disabled
attribute。而当其为其他假值时 attribute 将被忽略。
1 |
|
3.动态绑定多个值
1 |
|
通过不带参数的 v-bind
,你可以将它们绑定到单个元素上:
1 |
|
4.v-if
指令会基于表达式 seen
的值的真假来移除/插入该 <p>
元素。
1 |
|
5.href
就是一个参数,它告诉 v-bind
指令将表达式 url
的值绑定到元素的 href
attribute 上。在简写中,参数前的一切 (例如 v-bind:
) 都会被缩略为一个 :
字符
1 |
|
6.监听DOM时间 v-on
指令
1 |
|
举例
1 |
|
三,事件监听
1.用v-on
进行监听,简写语法和用法上面提到过。
1 |
|
实现 increment
函数并通过使用 v-on
将其绑定到按钮上。这种就是内联事件处理器,一般用于简单场景。
2.方法事件处理器
1 |
|
方法事件处理器会自动接收原生 DOM 事件并触发执行。在上面的例子中,我们能够通过被触发事件的 event.target.tagName
访问到该 DOM 元素。
3.在内联事件处理器中访问事件参数
有时我们需要在内联事件处理器中访问原生 DOM 事件。你可以向该处理器方法传入一个特殊的 $event
变量,或者使用内联箭头函数:
1 |
|
1 |
|
所以看出想要进行事件访问,funtion中有一个参数是event
还有一系列按键操作,就不赘述。
四,表单绑定
就是v-model操作
v-mode操作可以支持文本输入框或者多选框,下拉框等等。
1 |
|
还有更多例子在官网上看即可
条件渲染
v-if
v-if就是当指令的表达式为真的时候才会返回里面的值
v-else就是当指令为假返回值
1 |
|
这可以实现点击按钮进行切换
v-show简单,如果需要频繁切换那么v-show好一点
如果运行时绑定条件很少改变v-if好
寒假学习day1(Vue)
https://ljw030710.github.io/2024/01/28/寒假学习day1-Vue/