• vue2(2)


    目录

    天气案例

    监视属性watch

    深度监视

    监视简写属性

    watch对比computed

    绑定class样式

    条件渲染

    列表渲染


    天气案例

    绑定事件的时候,@xxx="yyy"   yyy可以写一些简单的语句

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    9. head>
    10. <body>
    11. <div id="root">
    12. <h2>今天天气很{{info}}h2>
    13. <button @click="changeWeather">切换天气button>
    14. div>
    15. body>
    16. <script>
    17. Vue.config.productionTip=false//阻止vue在启动时生成生产提示
    18. const vm=new Vue({
    19. el:'#root',
    20. data:{
    21. isHot:true
    22. },
    23. computed:{
    24. info(){
    25. return this.isHot?'炎热':'凉爽'
    26. }
    27. },
    28. methods:{
    29. changeWeather(){
    30. this.isHot=!this.isHot
    31. }
    32. }
    33. })
    34. script>
    35. html>

    监视属性watch

    1. 当被监视的属性变化时,回调函数自动调用,进行相关操作
    2. 监视的属性必须存在,才能进行监视

    监视的两种写法:

    1)new Vue时传入watch配置

    2)通过vm.$watch监视

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    9. head>
    10. <body>
    11. <div id="root">
    12. <h2>今天天气很{{info}}h2>
    13. <button @click="changeWeather">切换天气button>
    14. div>
    15. body>
    16. <script>
    17. Vue.config.productionTip=false//阻止vue在启动时生成生产提示
    18. const vm=new Vue({
    19. el:'#root',
    20. data:{
    21. isHot:true
    22. },
    23. computed:{
    24. info(){
    25. return this.isHot?'炎热':'凉爽'
    26. }
    27. },
    28. methods:{
    29. changeWeather(){
    30. this.isHot=!this.isHot
    31. }
    32. },
    33. // watch:{
    34. // isHot:{
    35. // immediate:true,
    36. // handler(newValue,oldValue){
    37. // console.log('isHot被修改了',newValue,oldValue);
    38. // }
    39. // }
    40. // }
    41. })
    42. vm.$watch('info',{
    43. immediate:true,
    44. handler(newValue,oldValue){
    45. console.log('isHot被修改了',newValue,oldValue);
    46. }
    47. })
    48. script>
    49. html>

    深度监视

    1. Vue中的watch默认不监测对象内部值的改变(一层)
    2. 配置deep:true可以监测对象内部值改变(多层)

    备注:

    1. Vue自身可以监测对象内部值的改变,但Vue提供的watch默认不可以
    2. 使用watch时根据数据结构,决定是否采用深度监视
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    9. head>
    10. <body>
    11. <div id="root">
    12. <h2>今天天气很{{info}}h2>
    13. <button @click="changeWeather">切换天气button>
    14. <hr>
    15. <h3>a的值是:{{numbers.a}}h3>
    16. <button @click="numbers.a++">点我让a+1button>
    17. <h3>b的值是:{{numbers.b}}h3>
    18. <button @click="numbers.b++">点我让b+1button>
    19. <button @click="numbers={a:666,b:888}">彻底换掉numbersbutton>
    20. div>
    21. body>
    22. <script>
    23. Vue.config.productionTip=false//阻止vue在启动时生成生产提示
    24. const vm=new Vue({
    25. el:'#root',
    26. data:{
    27. isHot:true,
    28. numbers:{
    29. a:1,
    30. b:1
    31. }
    32. },
    33. computed:{
    34. info(){
    35. return this.isHot?'炎热':'凉爽'
    36. }
    37. },
    38. methods:{
    39. changeWeather(){
    40. this.isHot=!this.isHot
    41. }
    42. },
    43. watch:{
    44. isHot:{
    45. immediate:true,
    46. handler(newValue,oldValue){
    47. console.log('isHot被修改了',newValue,oldValue);
    48. }
    49. },
    50. // 监视多级结构中某个属性的变化
    51. // 'numbers.a':{
    52. // handler(){
    53. // console.log('a被改变了');
    54. // }
    55. // },
    56. // 监视多级结构中所有属性的变化
    57. numbers:{
    58. deep:true,
    59. handler(){
    60. console.log('number改变了');
    61. }
    62. }
    63. }
    64. })
    65. script>
    66. html>

    监视简写属性

    需要没有immediate和deep属性

    简写有两种方式,一在watch,二在全局中

    1. isHot(newValue,oldValue){
    2. console.log('isHot被修改了',newValue,oldValue);
    3. }
    1. vm.$watch('isHot',function(newValue,oldValue)){
    2. console.log('isHot被修改了',newValue,oldValue);
    3. }

    watch对比computed

    1. computed能完成的功能,watch都可以完成
    2. watch能完成的功能,computed不一定能完成,例如:watch可以进行异步操作,如setTimeout

    两个重要的小原则:

    1. 所有的Vue管理的函数,最好写成普通函数,这样this的指向才是vm或组件实例对象
    2. 所有不被Vue所管理的函数(定时器的回调函数、ajax的回调函数、promise的回调函数),最好写成箭头函数,这样this的指向才是vm或组件实例对象
    1. watch:{
    2. firstname(val){
    3. setTimeout(()=>{
    4. this.fullName=val+'-'+this.lastname
    5. })
    6. this.fullName=newValue+'-'+this.lastname
    7. },
    8. lastname(newValue){
    9. this.fullName=this.firstname+'-'+newValue
    10. }
    11. }

    绑定class样式

    1. 字符串写法,适用于:样式的类名不确定,需要动态指定
    2. 数组写法,适用于要绑定的的样式个数不确定,名字也不确定
    3. 对象写法,适用于要绑定的个数确定,名字也确定,但要动态决定用不用

    条件渲染

    1.v-if

    写法:

    1. v-if="表达式"
    2. v-else-if="表达式"
    3. v-else="表达式”

    适用于:切换频率较高的场景

    特点:不展示DOM的元素直接被移除

    注意:v-if可以和v-else-if、v-else一起使用,但要求结构不能被”打断“

    2.v-show

    写法:v-show="表达式"

    适用于:切换频率较高的场景

    特点:不展示的DOM元素未被移除,仅仅是使用样式隐藏掉

    3.备注:使用v-if的时候,元素可能无法获取到,而使用v-show一定可以获取到

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    9. head>
    10. <body>
    11. <div id="root">
    12. <h2>当前的n值为:{{n}}h2>
    13. <button @click="n++">点我n+1button>
    14. <h2 v-show="false">欢迎来到{{name}}h2>
    15. <h2 v-show="1===1">欢迎来到{{name}}h2>
    16. <h2 v-if="false">欢迎来到{{name}}h2>
    17. <h2 v-if="1===1">欢迎来到{{name}}h2>
    18. <div v-if="n===1">Angulardiv>
    19. <div v-else-if="n===2">Reactdiv>
    20. <div v-else-if="n===3">Vuediv>
    21. <div v-else> 哈哈div>
    22. <div>
    23. <h2 v-show="n===1">你好h2>
    24. <h2 v-show="n===1">尚硅谷h2>
    25. <h2 v-show="n===1">北京h2>
    26. div>
    27. <template>
    28. <h2>你好h2>
    29. <h2>guiguh2>
    30. <h2>北京h2>
    31. template>
    32. div>
    33. body>
    34. <script>
    35. Vue.config.productionTip=false//阻止vue在启动时生成生产提示
    36. const vm=new Vue({
    37. el:'#root',
    38. data:{
    39. name:'花京院',
    40. n:0
    41. }
    42. })
    43. script>
    44. html>

    列表渲染

    v-for指令

    • 用于展示列表数据
    • 语法:v-for="(item,index) in xxx"  :key="yyy"
    • 可遍历:数组、对象、字符串(用的很少)、指定次数(用的很少)
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>Documenttitle>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    9. head>
    10. <body>
    11. <div id="root">
    12. <h2>人员列表h2>
    13. <ul>
    14. <li v-for="(p,index) of persons" :key="index">
    15. {{p.name}}---{{p.age}}---{{index}}--{{p.id}}
    16. li>
    17. ul>
    18. <h2>汽车信息(遍历对象)h2>
    19. <ul>
    20. <li v-for="(value,k) of car" :key="k">
    21. {{k}}---{{value}}
    22. li>
    23. ul>
    24. <h2>测试遍历字符串(用得少)h2>
    25. <ul>
    26. <li v-for="(a,b) of str">
    27. {{a}}---{{b}}
    28. li>
    29. ul>
    30. <h2>测试遍历指定次数(用得少)h2>
    31. <ul>
    32. <li v-for="(number,index) of 5" :key="index">
    33. {{index}}----{{number}}
    34. li>
    35. ul>
    36. div>
    37. body>
    38. <script>
    39. Vue.config.productionTip = false; //阻止vue在启动时生成生产提示
    40. new Vue({
    41. el: "#root",
    42. data: {
    43. // 遍历数组
    44. persons: [
    45. { id: "001", name: "张三", age: 18 },
    46. { id: "002", name: "李四", age: 19 },
    47. { id: "003", name: "王五", age: 20 },
    48. ],
    49. car: {
    50. name: "奥迪A8",
    51. price: "70万",
    52. color: "黑色",
    53. },
    54. str:'hello'
    55. },
    56. });
    57. script>
    58. html>
  • 相关阅读:
    Elasticsearch索引操作(七)
    基因对疾病的影响规律--读论文
    Selenium UI 自动化
    计算机毕业设计Java高校教师个人信息管理系统(源码+系统+mysql数据库+lw文档)
    vue Router从入门到精通
    软件测试面试题:Web服务器指标指标?
    SpringBoot 如何集成 MyBatisPlus - SpringBoot 2.7.2实战基础
    图解 ARP协议工作流程
    卷积神经网络实践-猫狗分类
    [OC学习笔记]属性关键字
  • 原文地址:https://blog.csdn.net/m0_62520946/article/details/126309769