VUE-DAY01

04、简单Vue创建实例

总结:

(1)定义变量:let 定义变量 ; const 定义常量;
(2)const app = new Vue ({
el:加上绑定的元素;
data:{ }
})
其中创建实例关键字为new,Vue指调用vue.js中的Vue函数直接调用函数进行创建实例,类似于var
app= function Vue();

    <div id="app">
        <p>{{message}}</p>
        <p>{{name}}</p>
    </div>

//适合数据,代码分离
    <script src="../js/vue.js"></script> <!-- 引入vue.js -->
    <script>
        // let 变量  const 常量
        //对象类型
        const app = new Vue ({ 
            el:'#app',  //用于挂载管理的元素           
            data: { 
             	           
                message: 'hello world',//定义数据  
                name:'anna'
 
            }
        }) 
    </script>

在这里插入图片描述

05、(掌握)Vue列表的展示

指令:v-for 可循环遍历数组
语法 v-for=“item in array”

<body>
    <div id="app">
    //vue实例管理者app的数据,通过v-for="item in array"来遍历数组进行展示
        <ul>
            <li v-for="item in movies">{{item}}</li>
        </ul>
    </div>
//引入vue
<script src="../js/vue.js"></script>
<script>
    const app = new Vue({
        el: '#app',
        data: {
            message: '你好啊',
            movies: ['海贼王','奶爸','超越','大话西游'],
        }
    })
</script>
    
</body>

在这里插入图片描述

06、(掌握)计数器实现

目的:编写一个计数器,实现加减并输出操作
先编写大概的结构

<div id="app">
    <h2>当前计数:{{counter}}</h2>
    <button>+</button>
    <button>-</button>
</div>

在这里插入图片描述
为了监听按钮,绑定事件,使用指令v-on,因为是click事件配置counter的加减,所以为v-on:click=counter++

    <div id="app">
    <h2>当前计数:{{counter}}</h2>
    <button v-on:click="counter++">+</button>
    <button v-on:click="counter--">-</button>
    </div>


    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                counter:0
            }
        })

    </script>

另外,使用语法糖可简写v-on:click=“couter++” 为@click=“counter++”,效果一样,把v_on 变成了**@** 表示

<button @click="counter--">-</button>

此时已经能够实习加减操作了,但是不能满足复杂的操作,需要运用函数才能处理,这里用函数加上了输出的功能
使用methods(方法)去定义,这里的this,因为在同一对象app里,所以this.counter就是app.counter,如果直接使用counter++是会影响全局的counter

<body>
    <div id="app">
    <h2>当前计数:{{counter}}</h2>
    <button v-on:click="add">+</button>
    <button @click="sub">-</button>
    </div>


    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                counter:0
            },
            methods:{
                add: function(){
                    console.log('add'),
                    this.counter++
                },
                sub: function(){
                    console.log('sub'),
                    this.counter--

                }
            }
        })

    </script>
</body>

在这里插入图片描述

07(理解)vue的MVVM

维基百科MVVM
当有数据绑定时,Viewmodel会自动把数据绑定到view上
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
所以也可以这样单独把model提取出来,由于有内部代理,所以obj的值都赋予到了实例里的data了
在这里插入图片描述

在这里插入图片描述

08、(理解)vue的options对象

vue 对象
在这里插入图片描述

如何区别函数和对象
函数:function
方法:method
在类里面的叫方法,与实例相挂钩(面向对象)

09、(理解)vue的生命周期

vue生命函数详解
生命周期:事物从开始到消亡的过程
created 钩子可以用来在一个实例被创建之后执行代码:

        const app = new Vue({
            el: '#app',
            data:obj,
            methods:{
                add: function(){
                    console.log('add'),
                    this.counter++
                },
                sub: function(){
                    console.log('sub'),
                    this.counter--

                }
            },
            created: function(){
                    console.log ('created');                     
            
            }
        })

在这里插入图片描述
vue源码下载
查看new Vue的源码,先进到这个index.js
在这里插入图片描述
在这里插入图片描述
再进到.\instance\index.js
在这里插入图片描述
在这里回调了created函数,代表生命周期已经到了这里
在这里插入图片描述

10、(理解)生命周期函数有哪些

mounted、updated 和 destroyed等
在这里插入图片描述

12、插值操作-mustache语法

<body>
    <div id="app">
        <h2>{{message}}</h2>
        <h2>{{message}},xxx</h2>
        <!-- mustache不仅可以直接写变量,也可以写见简单阿达表达式-->
        <h2>{{firstname + lastaname}}</h2>
        <h2>{{firstname + '  ' + lastname}}</h2>
        <h2>{{firstname}}  {{lastname}}</h2>
        <h2>{{counter * 2}}</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                firstname: 'kobe',
                lastname: 'bryant',
                counter: 100
            }
        })
    </script>
</body>

效果:
在这里插入图片描述

13、插值操作-其他指令使用

(1)v-once

在这里插入图片描述

<body>
    <div id="app">
        <h2>{{message}}</h2>
        <h2 v-once>{{message}}</h2>
        
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好'
            }
        })

        
    </script>
</body>

效果:当改变message的值时,加了v-once指令的不会被修改
在这里插入图片描述

(2)v-html

希望请求服务器的链接,如果服务器返回的是带a标签的链接百度一下 '
想要把链接直接展示到页面上

<body>
    <div id="app">
        <h2>{{url}}</h2>
        <h2 v-html="url"></h2>      
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                url:'<a href="http://www.baidu.com">百度一下 </a>'
            }
        })

        
    </script>
</body>

将链接格式化显示
在这里插入图片描述

(3)v-text

功能类似与mustache语法一样,用于显示;
这里注意,v-text会覆盖掉这之后的东西,所以一般很少使用

<body>
    <div id="app">
        <h2>{{message}},xxx</h2>
        <h2 v-text="message">,xxx</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
            }
        })       
    </script>
</body>

在这里插入图片描述

(4)v-pre

v-pre会不解析,直接显示内容

<body>
    <div id="app">
        <h2>{{message}}</h2>
        <h2 v-pre>{{message}}</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
            }
        })       
    </script>
</body>

(5)v-cloak,可用于解决插值闪烁问题

先手动模拟延迟,因为代码是由上到下解析的

<body>
    <div id="app">
        <h2>{{message}}</h2>

    </div>
    <script src="../JS/vue.js"></script>
    <script>
        setTimeout(function(){
            const app = new Vue({
                el: '#app',
                data:{
                    message:'你好',
                }
            })  
        },2000)
    </script>
</body>

先出现如图,先渲染{{message}},两秒后解析的js代码执行,渲染完成才出来内容,导致加载会出现闪烁
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v-cloak</title>
    <style>
        /* 根据有没有属性进行样式的显示,
        当有这个属性时不显示,则不显示,
        没有这个属性时就会直接显示内容,
        就不会出现插值闪烁的问题*/
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body>
    <div id="app">
        <h2 v-cloak>{{message}}</h2>

    </div>
    <script src="../JS/vue.js"></script>
    <script>
        // vue解析前有一个属性 v-cloak
        // vue解析后就没有了属性 v-cloak
        setTimeout(function(){
            const app = new Vue({
                el: '#app',
                data:{
                    message:'你好',
                }
            })  
        },2000)
    </script>
</body>
</html>

在这里插入图片描述

14、掌握 绑定属性

(1)v-bind的基本使用

在这里插入图片描述
网站上的图片一般都是从服务器请求,不会写死

<body>
    <div id="app">
        <img src="{{imgurl}}">
        <img v-bind:src="imgurl" >
        <a v-bind:href="hrefurl">百度一下</a>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                imgurl:'https://i0.hdslb.com/bfs/sycp/creative_img/202102/846d523bc004837160b1bd06c64dc8f4.jpg@880w_388h_1c_95q',
                hrefurl:'http://www.baidu.com'
            }
        })
    </script>
</body>

效果:
在这里插入图片描述
v-bind可以使用语法糖写法,如下:

    <div id="app">
        <img :src="imgurl" >
        <a :href="hrefurl">百度一下</a>
    </div>

(2)v-bind动态绑定class(1)

<body>
    <div id="app">
        <h2 :class='active'>{{message}}</h2>
        <h2 v-bind:class='active'>{{message}}</h2>

    </div>
    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                active:'active'
            },
        })
    </script>
</body>

效果:
在这里插入图片描述

15、掌握 v-bind动态绑定class(对象语法)

通过按钮来控制字体颜色变化

 <!-- 大括号跟上对象,可写键值对 -->
 <h2 v-bind:class={key1:value1,key2:value2}>{{message}}</h2> 

key一般为类名,值一般为布尔值

<!-- 当布尔值为true时,添加class为类名1.类名2同理,后续通过控制布尔值来控制类名-->
 <h2 v-bind:class={类名1:boolean,类名2:boolean}>{{message}}</h2>
<h2 v-bind:class="{active:true,line:true}">{{message}}</h2>

可以看到为true就可以添加进class
在这里插入图片描述
将布尔值放到data里进行控制

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v-bind(3)</title>
    <style>
        .active {
            color: red;
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- <h2 :class='active'>{{message}}</h2>
        <h2 v-bind:class='active'>{{message}}</h2> -->

        <!-- 大括号跟上对象,可写键值对 -->
        <!-- <h2 v-bind:class={key1:value1,key2:value2}>{{message}}</h2> -->


        <!-- 当布尔值为true时,添加class为类名1.类名2同理,后续通过控制布尔值来控制类名-->
        <!-- <h2 v-bind:class={类名1:boolean,类名2:boolean}>{{message}}</h2> -->
        <h2 v-bind:class="{active:isactive,line:isline}">{{message}}</h2>

    </div>
    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                isactive:'true',
                isline:'false'
            },
        })
    </script>
</body>
</html>

在这里插入图片描述
增加按钮控制布尔值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v-bind(3)</title>
    <style>
        .active {
            color: red;
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- <h2 :class='active'>{{message}}</h2>
        <h2 v-bind:class='active'>{{message}}</h2> -->

        <!-- 大括号跟上对象,可写键值对 -->
        <!-- <h2 v-bind:class={key1:value1,key2:value2}>{{message}}</h2> -->


        <!-- 当布尔值为true时,添加class为类名1.类名2同理,后续通过控制布尔值来控制类名-->
        <!-- <h2 v-bind:class={类名1:boolean,类名2:boolean}>{{message}}</h2> -->
        <h2 v-bind:class="{active:isactive,line:isline}">{{message}}</h2>
        <button v-on:click='btnclick'>点击按钮控制布尔值更换颜色</button>

    </div>
    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                isactive:'true',
                isline:'false'
            },
            methods:{
                btnclick:function(){
                    this.isactive = !this.isactive
                }
            }
        })
    </script>
</body>
</html>

效果
在这里插入图片描述

 <h2 class='title',v-bind:class="{active:isactive,line:isline}">{{message}}</h2>

并且这里的class可以合并,所以当有固定的样式时可以直接写,如果是会改变的class则用data进行控制

扩展:

 <h2 class='title',v-bind:class="{active:isactive,line:isline}">{{message}}</h2>

可缩写为如下,此时getclass()是一个计算属性

 <h2 class='title' v-bind:class="{active:isactive,line:isline}">{{message}}</h2>
 <!-- 缩写 -->
<h2 class='title' v-bind:class="getclass()">{{message}}</h2>
<button v-on:click='btnclick'>点击按钮控制布尔值更换颜色</button>

并且在方法中添加对应的函数,这里的this是因为使用了app中data的数据

methods:{
   btnclick:function(){
   	    this.isactive = !this.isactive
   },
   getclass:function(){
        return {active:this.isactive,line:this.isline}
	}
}

16、掌握 v-bind动态绑定class(数组语法)

待补充

17、总结+作业

在这里插入图片描述

18、 掌握 v-bind动态绑定style(对象语法)

可利用v-bind:style来绑定一些内联样式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <!-- <h2 :style="{key:vlaue}">{{message}}</h2> -->
        <!-- <h2 :style="{属性名:属性值}">{{message}}</h2> -->
        <!-- 50px必须加上单引号,否则会当作变量解析 -->
        <!-- <h2 :style="{fonSize: '50px'}">{{message}}</h2> -->
        <h2 :style="{fontSize:finalsize + 'px',color:finalcolor}">{{message}}</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                finalsize: 100,
                finalcolor: 'red',
            }
        })       
    </script>  
</body>
</html>

效果:
在这里插入图片描述
也可以单独把对象提取到函数中:

<body>
    <div id="app">
        <!-- <h2 :style="{key:vlaue}">{{message}}</h2> -->
        <!-- <h2 :style="{属性名:属性值}">{{message}}</h2> -->
        <!-- 50px必须加上单引号,否则会当作变量解析 -->
        <!-- <h2 :style="{fonSize: '50px'}">{{message}}</h2> -->
        <h2 :style="{fontSize:finalsize + 'px',color:finalcolor}">{{message}}</h2>
        <h2 :style=getstyle()>{{message}}</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                finalsize: 100,
                finalcolor: 'red',
            },
            methods:{
                getstyle:function(){
                    return {
                        fontSize:this.finalsize + 'px',color:this.finalcolor
                    }
                }
            }
        })       
    </script>  
</body>

19 掌握 v-bind动态绑定style(数组语法)

将对象提取出来作为数组中的一个

<body>
    <div id="app">
        <h2 :style="[baseStyle,baseStyle1]">{{message}}</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                message:'你好',
                baseStyle:{color:'red'},
                baseStyle1:{fontSize:'100px'},
            }
        })       
    </script>
</body>

20、掌握-计算属性的基本使用

<body>
    <div id="app">
        要显示lv ruohan 的效果
        <!-- 第一种 -->
        <h2>{{firstname+' '+lastname}}</h2>
        <!-- 第二种-->
        <h2>{{firstname}} {{lastname}}</h2>
        <!-- 第3种 方法的形式-->
        <h2>{{getfullname()}}</h2>
        <!-- 第4种 计算属性,实际是个属性值-->
        <h2>{{fullname}}</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data:{
                firstname:'lv',
                lastname:'ruohan'
            },
            computed:{
                fullname:function(){
                    return this.firstname + ' ' + this.lastname
                }
            },
            methods:{
                getfullname:function(){
                    return this.firstname + ' ' + this.lastname
                }
            }
        })       
    </script>
</body>

在这里插入图片描述

21、掌握-计算属性的复杂操作

自动计算书本总和
// methods比较适合循环输出,调几次就几次,没有缓存
// 计算属性在多次调用时只调用一次 ,有缓存,节省性能

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>v-text</title>
  </head>
  <body>
    <div id="app">
      <h2>总价格:{{totalPrices}}</h2>
    </div>
    <script src="../JS/vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          books: [
            { id: 110, name: "unix编程", price: 119 },
            { id: 111, name: "代码大全", price: 105 },
            { id: 112, name: "js教程", price: 98 },
            { id: 113, name: "vue教程", price: 87 },
          ],
        },
        computed: {
          totalPrices: function () {
            let result = 0;
            for (let i = 0; i < this.books.length; i++) {
              result += this.books[i].price;
            }
            return result;
          },
        },
        // methods比较适合循环输出,调几次就几次,没有缓存
        // 计算属性在多次调用时只调用一次 ,有缓存,节省性能
      });
    </script>
  </body>
</html>

在这里插入图片描述

语法糖

v-bind -> :
v-on-> @

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值