vue

vue笔记

(一)基础知识

第一个vue入门实例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app">
    {{msg}}
    <h3>用户名:{{username}}</h3>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    var app = new Vue({
        el:"#app",
        data:{
            msg:"第一个vue程序",
            username:"杜浩宇"
        }
    })
</script>
vue实例中定义对象 数组等数据.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app">
    <h2>{{msg}}</h2>
    <h2>姓名:{{ user.name }}</h2>
    <h2>年龄:{{ age }}</h2>
    <h2>信息:{{ user.des }}</h2>
    <h2>学校:{{schools}}</h2>
    <h2>users:{{users[0].name}}</h2>
</div>
</body>
</html>

<script src="js/vue2.5.js"></script>
<script>
    var app = new Vue({
        el: "#app", // 指定vue的作用范围
        data: {
            msg: "vue 学习的第一天",
            age: 23,
            user: {name: "duhaoyu", des: "河北科技师范学院"},
            schools: ["北京大学", "天津大学", "清华大学", "河北大学"],
            users: [
                {name: "lxx", age: 23, school: "河北科技师范学院"},
                {name: "lww", age: 23, school: "燕山大学"}
            ]
        }
    })
</script>
使用{{属性名}}获取数据时使用表达式运算符相关的操作
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app">
<!--    使用{{属性}}的方式,可以进行相关的逻辑运算,算术运算等-->
    <h2>{{age + 1 == 24}}</h2>
</div>
</body>
</html>

<script src="js/vue2.5.js"></script>
<script>
    var app = new Vue({
        el: "#app", // 指定vue的作用范围
        data: {
            msg: "vue 学习的第一天",
            age: 23
        }
    })
</script>
使用vue时el的指定
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app" class="aa">
    {{msg}}
</div>
</body>
</html>

<script src="js/vue2.5.js"></script>
<script>
    var app = new Vue({
        /*
         * 指定vue的作用范围,可以是id选择器,可以是类选择器,所有的css选择器都可以
         *  推荐使用id选择器,因为id选择器具有唯一性
         * 注意:不要将el指向body 或者 html
         */
        el: ".aa",
        data: {
            msg: "vue 学习的第一天",
            age: 23
        }
    })
</script>
使用v-text和v-html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="js/vue2.5.js"></script>
</head>
<body>
<div id="app">
<!--
v-text 和 {{}}的区别:
    v-text : 会覆盖原来标签当中的数据,{{}}不会
    {{}}会产生过插值闪烁,v-text不会
-->

<!--
   v-html:也可以获取数据,还可以解析数据中的html标签
   v-text: 只能取值,不会解析html标签
-->
    <span v-text="msg"></span> <br>
    <span v-html="msg"></span>
</div>
</body>
</html>
<script>
    new Vue({
        el: "#app",
        data: {
            msg: "<a href=''>hello vue</a>"
        }
    })
</script>
vue事件绑定(一)
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
    <div id="app">
        <input type="button" value="点我" v-on:click="aaa" v-on:mouseover="bbb" v-on:mouseout="ccc"/>
    </div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    /*
     * js事件的三要素:
     *   1.事件源:发生事件的源头称为事件源,一般是指html标签
     *   2.事件:发生特定动作,onclick单击。。。
     *   3.事件处理函数或事件处理程序
     *
     * vue事件:
     *   在vue中,可以通过v-on进行事件的绑定
     *   vue中,事件处理函数统一声明在vue对象中methods属性中
     */
    var app = new Vue({
        el: "#app",
        data: {},
        methods:{
            aaa:function () {
                alert('aaa');
            },
            bbb:function () {
                console.log("mouser over");
            },
            ccc:function () {
                console.log("mouse out");
            }
        }
    })
</script>
vue事件绑定(二)
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
    <div id="app">
        <h1>{{msg}}</h1>
        <h1>{{age}}</h1>
        <input type="button" value="给年龄加一" v-on:click="changeAge"/>
<!--        简化写法-->
        <input type="button" value="给年龄加一(简化)" @click="changeAge"/>
    </div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            msg:"hello vue",
            age:23
        },
        methods:{
            changeAge:function () {
                console.log(this.age);
                if(this.age >= 120) return;
                this.age++;
            },
            decrmentAge:function () {
                if(this.age < 2) return;
                this.age--;
            },
            // 函数的简化写法
            change(){
                this.age++;
            }
        }
    })
</script>
vue事件参数传递
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app">
    {{age}}
    <input type="button" value="改变年龄" @click="changeAge(10,'xiaohei')"/>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
            age:23
        },
        methods:{
            changeAge(number,name){
                this.age += number;
                console.log(name);
            }
        }
    })
</script>
v-show,v-if使用
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app">
    <!--
        v-if,v-show都是用来控制页面中的标签是否展示
        v-show : true 展示,false 不展示,可以写boolean值得表达式
        v-if: true 展示,false 不展示,

     区别:
        v-show:底层在控制页面标签是否展示时,使用的是css中display属性
        v-if:底层使用直接操作dom元素,通过对dom元素的删除和增加来控制标签是否展示
    -->
    <h1 v-show="isShow">{{msg}}</h1>
    <h1 v-if="isShow">{{content}}</h1>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            msg: "hello vue",
            content:"duhaoyu",
            isShow:true
        },
        methods: {

        }
    })
</script>
v-show,v-if显示隐藏案例(一)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app">
    <h1 v-show="isShow">{{msg}}</h1>
    <input type="button" value="展示" @click="show"/>
    <input type="button" value="隐藏" @click="hide"/>
    <input type="button" value="切换显示状态" @click="transform"/>
    <!--        另一种写法-->
    <input type="button" value="切换显示状态2" @click="isShow = !isShow"/>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            msg: "hello vue",
            isShow: true
        },
        methods: {
            hide() {
                this.isShow = false;
            },
            show() {
                this.isShow = true;
            },
            transform() {
                this.isShow = !this.isShow;
            }
        }
    })
</script>

v-show,v-if显示隐藏案例(二)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
    <div id="app">
        <h1>{{msg}}</h1>
        <img width="200px" v-show="isShow" @mouseover="hide" src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=141156173,2221205300&fm=26&gp=0.jpg" alt="这是图片">
    </div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
            msg:"hello vue",
            isShow:true
        },
        methods:{
            hide(){
                this.isShow = false;
            }
        }
    })
</script>

v-bind指令学习

<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <style>
        .aa {
            border: 1px red solid
        }

        .bb {
            border: 1px yellow solid
        }
    </style>
</head>

<body>
    <div id="app">
        <h1>{{msg}}</h1>
        <!--
        v-bind:
            用来将html中的标签的属性绑定到vue的实例中,通过vue中数据的改变,来影响html标签当属性值得改变
    -->
        <img :width="width" v-bind:src="src" alt="" :class="isClass?'aa':'bb'">
    </div>
</body>

</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            msg: "hello vue",
            width: 200,
            src: "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=141156173,2221205300&fm=26&gp=0.jpg",
            // src: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201604%2F14%2F20160414123551_2tANx.png&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618376497&t=12d1769aeb2c43776376be096105d283"
            isClass: false
        },
        methods: {

        }
    })
</script>

v-bind案例(一)

<!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>Vue</title>
    <style>
        .aa{
            border: 1px red solid;
        }
        .bb{
            border: 1px green solid;
        }
    </style>
</head>

<body>
    <div id="app">
        <img width="200" :src="src" @mouseover="change" alt="" @mouseout="recover" :class="cls">
    </div>
</body>

</html>

<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            src:"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1694681277,1453280371&fm=26&gp=0.jpg",
            cls:"aa"
        },
        methods: {
            change(){
                this.src = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1963304009,2816364381&fm=26&gp=0.jpg";
                this.cls = "aa";
            },
            recover(){
                this.src = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1694681277,1453280371&fm=26&gp=0.jpg";
                this.cls = "bb";
            }
        }
    })
</script>

v-for指令使用

<!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>Vue</title>
</head>

<body>
<div id="app">
    <!--
        遍历对象:
        v-for使用: <h2 v-for="(value,key,index) in user"></h2>
    -->
    <h1>遍历对象</h1>
    <h2 v-for="(value,key,index) in user">
        index: {{index}}
        key: {{key}}
        value:{{value}}
    </h2>
    <h1>遍历数组</h1>
    <h2 v-for="(value,index) in areas">
        {{value}} ---- {{index}}
    </h2>
    <h1>遍历对象数组</h1>
<!--
    为了给每一个遍历出来的对象都有一个唯一标识,让vue内部更好的识别,在使用v-for的时候,一般要加一个:key属性
-->
    <h2 v-for="(user,index) in users" :key="user.id">
        {{index}} ----- {{user.name}} ----- {{user.age}}
    </h2>
</div>
</body>

</html>

<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            msg: "hello vue",
            user: {name: "duhaoyu", age: 23},
            areas: ["北京", "天津", "上海", "廊坊"],
            users: [
                {name: "张三", age: 24},
                {name: "李四", age: 25},
                {name: "王五", age: 26}
            ]
        }
    })
</script>

v-model指令学习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
    <div id="app">
<!--
    v-model:
        用来绑定form表单标签当中的value属性交给vue管理
-->
        <input type="text" v-model="msg">
        {{msg}}
        <input type="button" @click="change" value="改变数据">
    </div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
            msg:"hello vue"
        },
        methods:{
            change(){
                this.msg = "杜浩宇学vue"
            }
        }
    })
</script>

(二)小案例

vue实现备忘录案例(一)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>备忘录</title>
</head>
<body>
<div id="app">
    <h2>备忘录功能实现</h2>
    输入备忘录内容:<input type="text" @keyup.enter="add" v-model="content"/> <input type="button" value="添加到备忘录" @click="add"/>
    <ul v-show="items.length > 0">
        <li v-for="item,index in items">{{index+1}}.{{item}} <a href="javascript:" @click="deleteItem(index)">删除</a>
        </li>
    </ul>
    <h5 v-show="items.length == 0"> 备忘录暂时没有内容</h5>
    <h2>当前备忘录总数:{{items.length}}条</h2>
    <input type="button" @click="clearItems" value="清空所有"/>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            items: ["今天要好好学习", "今天要去市场买菜", "今天学习了vue"],
            content: ""
        },
        methods: {
            add() {
                if (this.content) {
                    this.items.push(this.content);
                    this.content = "";
                } else {
                    alert('请输入备忘内容');
                }
            },
            deleteItem(index) {
                this.items.splice(index, 1);
            },
            clearItems() {
                this.items = [];
            }
        }
    })
</script>
vue实现购物车案例(一)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
</head>
<body>
<div id="app">
    <h2>购物车功能实现之methods实现</h2>
    <table border="1px">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>count</th>
            <th>price</th>
        </tr>
        <tr v-for="item,index in items" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td><input type="button" @click="descCount(index)" value="-"/>{{item.count}}<input type="button" value="+"
                                                                                        @click="incrCount(index)"></td>
            <td>{{(item.price * item.count).toFixed(2)}}</td>
        </tr>
    </table>
    <h3>总价格:{{getTotal()}}</h3>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            items: [
                {id: "1", name: "苹果手机", count: 1, price: 20.26},
                {id: "2", name: "华为手机", count: 1, price: 30.16}
            ]
        },
        methods: {
            descCount(index) {
                if (this.items[index].count <= 1) {
                    return;
                }
                this.items[index].count--;
            },
            incrCount(index){
                this.items[index].count ++;
            },
            getTotal(){
                var total = 0;
                for (let i = 0; i < this.items.length; i++) {
                    total += this.items[i].count * this.items[i].price;
                }
                return total.toFixed(2);
            }
        }
    })
</script>

vue实现购物车案例(二)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
</head>
<body>
<div id="app">
    <h2>购物车功能实现之computed实现</h2>
    <table border="1px">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>count</th>
            <th>price</th>
        </tr>
        <tr v-for="item,index in items" :key="item.id">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td><input type="button" @click="descCount(index)" value="-"/>{{item.count}}<input type="button" value="+"
                                                                                               @click="incrCount(index)">
            </td>
            <td>{{(item.price * item.count).toFixed(2)}}</td>
        </tr>
    </table>
    <h3>总价格:{{totalPrice}}</h3>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el: "#app",
        data: {
            items: [
                {id: "1", name: "苹果手机", count: 1, price: 20.26},
                {id: "2", name: "华为手机", count: 1, price: 30.16}
            ]
        },
        methods: {
            descCount(index) {
                if (this.items[index].count <= 1) {
                    return;
                }
                this.items[index].count--;
            },
            incrCount(index) {
                this.items[index].count++;
            }
        },
        /*
         *用来书写计算的相关方法,计算属性。
         * 相对于methods来说计算不会重复计算,有缓存机制,更好提高效率
         */
        computed: {
            totalPrice() {
                var total = 0;
                for (let i = 0; i < this.items.length; i++) {
                    total += this.items[i].count * this.items[i].price;
                }
                return total.toFixed(2);
            }
        }
    })
</script>

vue事件修饰符

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
    <div id="app">
        <!--
            事件修饰符:用来和事件共同使用,决定给事件触发条件和触发机制
            .stop  停止事件冒泡
            .prevent 阻止标签的默认行为
            .self 只触发自身行为
            .once 只能让标签上的事件执行一次
         注意:可以多个事件修饰符连续使用
            example: @click.stop.once
        -->

<!--        .stop 用来阻止事件的冒泡-->
        <h2>stop事件修饰符</h2>
        <div style="width: 200px;height: 200px;background: red"  @click="parent">
            <div style="width: 100px;height: 100px;background: green" @click.stop="child">

            </div>
        </div>
        <h2>prevent事件修饰符</h2>
        <a href="http://www.baidu.com" @click.prevent="prevent">百度一下</a>
        <h3>self事件修饰符</h3>
        <div style="width: 200px;height: 200px;background: red"  @click.self="parent">
            <div style="width: 100px;height: 100px;background: green" @click="child">
            </div>
            <div style="width: 100px;height: 100px;background: aqua" @click="child">
            </div>
        </div>
        <h2>once事件修饰符</h2>
        <input type="button" value="点我" @click.once="clickMe"/>
    </div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{},
        methods:{
            parent(){
                alert('parent click')
            },
            child(){
                alert('child click')
            },
            prevent(){
                alert('prevent')
            },
            clickMe(){
                alert('click me')
            }
        }
    })
</script>
按键修饰符
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<!--
    按键修饰符:用来键盘上个事件(keyup,keydown....)进行连用,用来修饰键盘上特定的按键触发对应的事件
     .enter
     .tab
     .delete(退格键 和 删除键)
     .esc
     .space
     .up
     .down
     .left
     .right
-->
<div id="app">
    <h1>{{msg}}</h1>
<!--    keyup:鼠标抬起事件-->
    <input type="text" v-model="msg" @keyup.enter="test">
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{
            msg:"hello vue"
        },
        methods:{
            test(){
                console.log('test');
            }
        }
    })
</script>

(三) axios

3.1axios发送GET方式请求
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios发送GET方式请求</title>
</head>
<body>

</body>
</html>
<script src="js/axios.min.js"></script>
<script>
    // 发送axios的get请求
    // axios.get("http://localhost:8080/demo?id=1")
    //     .then(function (response) {
    //     console.log(response.data);
    // }).catch(function (error) { //请求地址出错
    //     console.log(error);
    // });

    axios.get("http://localhost:8080/user?id=11").then(function (response) {
        console.log(response.data);
    })
    //es6简化写法 (类似于java的lambda表达式)
    axios.get("http://localhost:8080/user?id=12").then(response => {
        console.log(response.data.bir);
    })

    axios.get("http://localhost:8080/user2/50").then(response => {
        console.log(response.data);
    })

</script>
3.2axios发送POST方式请求
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios发送POST方式请求</title>
</head>
<body>

</body>
</html>
<script src="js/axios.min.js"></script>
<script>
    // axios.post("http://localhost:8080/user3", {
    //     name: "xiaosi",
    //     age: 24,
    //     bir: 2012 - 12 - 15
    // }).then(res =>
    //     console.log(res)
    // )
    /*
     * axios的并发请求,axios。all()
     * 两个请求同时执行
     */
    function demo() {
        return axios.get("http://localhost:8080/demo")
    }
    function user() {
        return axios.get("http://localhost:8080/user?id=11")
    }
    axios.all([demo(),user()]).then(axios.spread((demoRes,userRes) => {
        console.log(demoRes);
        console.log(userRes)
    }));
</script>
3.3后端请求代码
package com.duhaoyu.controller;

import com.duhaoyu.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin //允许跨域访问
@RestController
public class AdminController {
    @GetMapping("demo")
    public String demo() {
        System.out.println("Demo");
        return "Demo OK";
    }

    @GetMapping("user")
    public User getUser(@RequestParam("id") Integer id) {
        System.out.println("id" + id);
        System.out.println("user ....");
        return new User(id, "duhaoyu", 23, new Date());
    }

    @GetMapping("user2/{id}")
    public User user2(@PathVariable("id") Integer id) {
        return new User(id, "lxx", 23, new Date());
    }

    @PostMapping("user3")
    public Map<String, Object> save(@RequestBody User user) {
        System.out.println(user);
        HashMap<String, Object> map = new HashMap<>();
        map.put("success","添加成功");
        return map;
    }
}

3.4实体类
package com.duhaoyu.entity;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

public class User {
    private Integer id;
    private String name;
    private Integer age;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date bir;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", bir=" + bir +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBir() {
        return bir;
    }

    public void setBir(Date bir) {
        this.bir = bir;
    }

    public User() {
    }

    public User(Integer id, String name, Integer age, Date bir) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.bir = bir;
    }
}

(四)重要部分

vue生命周期
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
</head>
<body>
<div id="app">
    <h1 id="sp">{{msg}}</h1>
<!--
    vue生命周期:
        1.初始化阶段
            beforeCreate(),created(),beforeMount(), mounted()
        2.运行阶段
            beforeUpdate,updated
        3.销毁阶段
            beforeDestroy,destroy
-->
    <input type="button" @click="changeData" value="改变数据" />
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script>
    var app = new Vue({
        el:"#app",
        data:{
            msg:"vue生命周期"
        },
        methods:{
            changeData(){
                this.msg = "vue 生命周期2";
            }
        },
        // 第一个执行生命周期函数,在这个函数执行的时候vue实例仅仅完成内部事件的初始化,以及生命周期方法的初始化
        beforeCreate(){
            console.log(this.msg);
        },
        // 第二个生命周期执行的函数,在这个函数执行的时候已经完成自定义的data,methods等的初始化以及语法校验
        created(){
            console.log(this.msg);
        },
        // 第三个生命周期的执行函数,在这个函数执行的时候仅仅是将el属性指向的html编译成模板,并没有完成页面的渲染
        beforeMount(){
            console.log(document.getElementById("sp").innerText);
        },
        // 第四个生命周期执行的函数,在这个函数执行的时候会将data的数据渲染到模板上并形成虚拟dom
        mounted(){
            console.log(document.getElementById("sp").innerText);
        },
        // 第五个运行阶段执行的函数,如果data中的数据发生改变会触发该函数,只是改变了data中的数据,但是页面中的数据还没有改变
        beforeUpdate(){
            console.log("beforeUpdate");
            console.log(document.getElementById("sp").innerText);
        },
        // 第六个运行阶段执行到该函数时,vue实例中的data数据和页面已经保存一致了
        updated(){
            console.log("updated");
            console.log(document.getElementById("sp").innerText);
        },
        // 第七个执行的函数,vue实例仅仅是刚刚开始销毁data methods ...数据还可以拿到
        beforeDestroy(){
            console.log("beforeDestroy" + this.msg);
        },
        // 第八个执行的函数,vue实例的所有events,child component(子组件),事件监听都被销毁了...
        destroy(){

        }
    })
</script>
用户列表案例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link href="css/bootstrap.css"  rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="app">
    <div class="container-fluid">
<!--        标题行-->
        <div class="row">
            <div class="col-sm-6 col-sm-offset-3"><h1 class="text-center">用户列表</h1></div>
        </div>
<!--        数据行-->
        <div class="row">
            <div class="col-sm-10 col-sm-offset-1">
<!--                添加按钮-->
                <a href="" class="btn btn-success btn-sm">添加</a>
                <table style="margin-top: 10px" class="table table-striped table-bordered">
                    <tr>
                        <th>编号</th>
                        <th>姓名</th>
                        <th>工资</th>
                        <th>年龄</th>
                        <th>简介</th>
                        <th>操作</th>
                    </tr>
                    <tr>
                        <td>1</td>
                        <td>张三</td>
                        <td>2455</td>
                        <td>23</td>
                        <td>出生于95年,八月26日,获得过三好学生</td>
                        <td>
                            <a href="" class="btn btn-info btn-sm">修改</a>
                            <a href="" class="btn btn-danger btn-sm">删除</a>
                        </td>
                    </tr>
                </table>
                <form>
                    <div class="form-group">
                        <label class="control-label">编号</label>
                        <div>
                            <p class="form-control-static">1</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="email" class="form-control" id="name" placeholder="Email">
                    </div>
                    <div class="form-group">
                        <label for="salary">工资</label>
                        <input type="password" class="form-control" id="salary" placeholder="Password">
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="email" class="form-control" id="age" placeholder="Email">
                    </div>
                    <div class="form-group">
                        <label for="description">简介</label>
                        <input type="password" class="form-control" id="description" placeholder="Password">
                    </div>
                    <button type="submit" class="btn btn-info">提交</button>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script src="js/vue2.5.js"></script>
<script src="js/axios.min.js"></script>
<script>
    new Vue({
        el:"#app",
        data:{

        },
        methods:{

        }
    })
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值