• 尚硅谷前端框架vue语法(二)


    一.class绑定(会动态改变的class需要用到class绑定)

     写法:class="xxx" xxx可以是字符串、对象、数组。
     字符串写法适用于:类名不确定,要动态获取。
     对象写法适用于:要绑定多个样式,个数不确定,名字也不确定。
     数组写法适用于:要绑定多个样式,个数确定,名字也确定,但不确定用不用。

     例子:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>绑定样式title>
    6. <style>
    7. .basic{
    8. width: 400px;
    9. height: 100px;
    10. border: 1px solid black;
    11. }
    12. .happy{
    13. border: 4px solid red;;
    14. background-color: rgba(255, 255, 0, 0.644);
    15. background: linear-gradient(30deg,yellow,pink,orange,yellow);
    16. }
    17. .sad{
    18. border: 4px dashed rgb(2, 197, 2);
    19. background-color: gray;
    20. }
    21. .normal{
    22. background-color: skyblue;
    23. }
    24. .atguigu1{
    25. background-color: yellowgreen;
    26. }
    27. .atguigu2{
    28. font-size: 30px;
    29. text-shadow:2px 2px 10px red;
    30. }
    31. .atguigu3{
    32. border-radius: 20px;
    33. }
    34. style>
    35. <script type="text/javascript" src="../js/vue.js">script>
    36. head>
    37. <body>
    38. <div id="root">
    39. <div class="basic" :class="mood" @click="changeMood">{{name}}div> <br/><br/>
    40. <div class="basic" :class="classArr">{{name}}div> <br/><br/>
    41. <div class="basic" :class="classObj">{{name}}div> <br/><br/>
    42. div>
    43. body>
    44. <script type="text/javascript">
    45. Vue.config.productionTip = false
    46. const vm = new Vue({
    47. el:'#root',
    48. data:{
    49. name:'尚硅谷',
    50. mood:'normal',
    51. classArr:['atguigu1','atguigu2','atguigu3'],
    52. classObj:{
    53. atguigu1:false,
    54. atguigu2:false,
    55. },
    56. },
    57. methods: {
    58. changeMood(){
    59. const arr = ['happy','sad','normal']
    60. const index = Math.floor(Math.random()*3)
    61. this.mood = arr[index]
    62. }
    63. },
    64. })
    65. script>
    66. html>

    二.列表渲染

     v-for指令:  简单来说,就是用v-for遍历时,需要加不同的key值
                     1.用于展示列表数据
                      2.语法:v-for="(item, index) in xxx" :key="yyy"    v-for="temi xxx" :key="yyy"    
                       3.可遍历:数组、对象、字符串(用的很少)、指定次数(用的很少)

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>基本列表title>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. head>
    8. <body>
    9. <div id="root">
    10. <h2>人员列表(遍历数组)h2>
    11. <ul>
    12. <li v-for="(p,index) of persons" :key="index">
    13. {{p.name}}-{{p.age}}
    14. li>
    15. ul>
    16. <h2>汽车信息(遍历对象)h2>
    17. <ul>
    18. <li v-for="(value,k) of car" :key="k">
    19. {{k}}-{{value}}
    20. li>
    21. ul>
    22. div>
    23. <script type="text/javascript">
    24. Vue.config.productionTip = false
    25. new Vue({
    26. el:'#root',
    27. data:{
    28. persons:[
    29. {id:'001',name:'张三',age:18},
    30. {id:'002',name:'李四',age:19},
    31. {id:'003',name:'王五',age:20}
    32. ],
    33. car:{
    34. name:'奥迪A8',
    35. price:'70万',
    36. color:'黑色'
    37. },
    38. }
    39. })
    40. script>
    41. html>

     

     三、列表过滤

    利用filter函数:

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>列表过滤title>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. head>
    8. <body>
    9. <div id="root">
    10. <h2>人员列表h2>
    11. <input type="text" placeholder="请输入名字" v-model="keyWord">
    12. <ul>
    13. <li v-for="(p,index) of filPerons" :key="index">
    14. {{p.name}}-{{p.age}}-{{p.sex}}
    15. li>
    16. ul>
    17. div>
    18. <script type="text/javascript">
    19. Vue.config.productionTip = false
    20. //用watch实现
    21. //#region
    22. /* new Vue({
    23. el:'#root',
    24. data:{
    25. keyWord:'',
    26. persons:[
    27. {id:'001',name:'马冬梅',age:19,sex:'女'},
    28. {id:'002',name:'周冬雨',age:20,sex:'女'},
    29. {id:'003',name:'周杰伦',age:21,sex:'男'},
    30. {id:'004',name:'温兆伦',age:22,sex:'男'}
    31. ],
    32. filPerons:[]
    33. },
    34. watch:{
    35. keyWord:{
    36. immediate:true,
    37. handler(val){
    38. this.filPerons = this.persons.filter((p)=>{
    39. return p.name.indexOf(val) !== -1
    40. })
    41. }
    42. }
    43. }
    44. }) */
    45. //#endregion
    46. //用computed实现
    47. new Vue({
    48. el:'#root',
    49. data:{
    50. keyWord:'',
    51. persons:[
    52. {id:'001',name:'马冬梅',age:19,sex:'女'},
    53. {id:'002',name:'周冬雨',age:20,sex:'女'},
    54. {id:'003',name:'周杰伦',age:21,sex:'男'},
    55. {id:'004',name:'温兆伦',age:22,sex:'男'}
    56. ]
    57. },
    58. computed:{
    59. filPerons(){
    60. return this.persons.filter((p)=>{
    61. return p.name.indexOf(this.keyWord) !== -1
    62. })
    63. }
    64. }
    65. })
    66. script>
    67. html>

     四.列表排序

    这里涉及一个sort排序的问题

    当我们想要对arr数组正序或逆序排序时,可以用如下操作:

    1. arr.sort((p1,p2)=>{    //进行升序或降序排序
    2.   return this.sortType === 1 ? p2.age-p1.age : p1.age-p2.age
    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>列表排序title>
    6. <script type="text/javascript" src="../js/vue.js">script>
    7. head>
    8. <body>
    9. <div id="root">
    10. <h2>人员列表h2>
    11. <input type="text" placeholder="请输入名字" v-model="keyWord">
    12. <button @click="sortType = 2">年龄升序button>
    13. <button @click="sortType = 1">年龄降序button>
    14. <button @click="sortType = 0">原顺序button>
    15. <ul>
    16. <li v-for="(p,index) of filPerons" :key="p.id">
    17. {{p.name}}-{{p.age}}-{{p.sex}}
    18. <input type="text">
    19. li>
    20. ul>
    21. div>
    22. <script type="text/javascript">
    23. Vue.config.productionTip = false
    24. new Vue({
    25. el:'#root',
    26. data:{
    27. keyWord:'',
    28. sortType:0, //0原顺序 1降序 2升序
    29. persons:[
    30. {id:'001',name:'马冬梅',age:30,sex:'女'},
    31. {id:'002',name:'周冬雨',age:31,sex:'女'},
    32. {id:'003',name:'周杰伦',age:18,sex:'男'},
    33. {id:'004',name:'温兆伦',age:19,sex:'男'}
    34. ]
    35. },
    36. computed:{
    37. filPerons(){
    38. const arr = this.persons.filter((p)=>{
    39. return p.name.indexOf(this.keyWord) !== -1
    40. })
    41. //判断一下是否需要排序
    42. if(this.sortType){
    43. arr.sort((p1,p2)=>{ //进行升序或降序排序
    44. return this.sortType === 1 ? p2.age-p1.age : p1.age-p2.age
    45. })
    46. }
    47. return arr
    48. }
    49. }
    50. })
    51. script>
    52. html>

    js中的两个api:

    Object.keys           //将对象转化为数组 

    Object.defineproperty      :

      ①创建属性。如果对象中不存在指定的属性

      ②修改属性。如果属性已经存在,

      ③添加多个属性和默认值

      ④自定义 Setters 和 Getters。

  • 相关阅读:
    强化学习入门
    《基于 Vue 组件库 的 Webpack5 配置》8.在生成打包文件之前清空 output(dist) 目录(两种方式)
    深入浅出-多进程编程
    Kernel Memory 入门系列: RAG 简介
    商业银行云模式下的技术变革
    DOS 命令集合
    04 【Sass语法介绍-运算】
    mybatis实战:四、insert 用法(普通插入、返回主键自增的值)
    TeeChart NET for Blazor v2022
    split和join方法用于字符串和数组的转换
  • 原文地址:https://blog.csdn.net/m0_64226820/article/details/128018868