12.17 calculate, monitor property

1. The form instructions:

v-model = "variable" variable associated with a value

Normal: variable represents the value value

Single-box: a value for a variable value of a plurality of single box

Single box: variable is a Boolean value, whether the check on behalf of

Multi-box: variable is an array, storing the selected value option

2. conditional instructions:

v-show = '' Boolean variable ": hidden when using {display: none} use when rendering the layout requirements, can make changes.

v-if = '' Boolean variable ": hide, not rendering the page from scratch, there are requirements when using data security

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        [v-cloak] { display: none; }

        .box {
            width: 200px;
            height: 200px;
        }
        .r {background-color: red}
        .b {background-color: blue}
        .g {background-color: green}

        .active {
            background-color: #b1e5ff;
        }
    </style>

</head>
<body>
    <div id="app" v-cloak>
        <!--条件指令:
            v-show="布尔变量"   隐藏时,采用display:none进行渲染
            v-if="布尔变量"     隐藏时,不再页面中渲染(保证不渲染的数据泄露)
            -----------------------------
            v-if | v-else-if | v-else
        -->
        <div class="box r" v-show="is_show"></div>
        <div class="box b" v-if="is_show"></div>
        <hr>

        <div class="wrap">
            <div>
                <button @click="page='r_page'" :class="{active: page === 'r_page'}">红</button>
                <button @click="page='b_page'" :class="{active: page === 'b_page'}">蓝</button>
                <button @click="page='g_page'" :class="{active: page === 'g_page'}">绿</button>
            </div>
            <div class="box r" v-if="page === 'r_page'"></div>
            <div class="box b" v-else-if="page === 'b_page'"></div>
            <div class="box g" v-else></div>
        </div>

    </div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            is_show: false,
            page: 'r_page'
        }
    })
</script>
</html>

3. loop instruction

v-for="(v,i) in str |arr"

v-for="(v,k,i) in dic"

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>todo list 案例</title>
    <style>
        li:hover {
            color: red;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="app">
        <input type="text" v-model="comment">
        <button type="button" @click="send_msg">留言</button>
        <ul>
            <li v-for="(msg, i) in msgs" @click="delete_msg(i)">{{ msg }}</li>
        </ul>
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            comment: '',
            msgs: localStorage.msgs ? JSON.parse(localStorage.msgs) : [],

        },
        methods: {
            send_msg() {
                // 将comment添加到msgs数组中:unshift push 首尾增 | shift pop 首尾删
                // this.msgs.push(this.comment);

                // 数组操作最全方法:splice(begin_index, count, ...args)
                //begin_index:从第几位开始,操作几位,操作为。。。,args不写默认操作为空,
                // 也就是删除,写了就是替换为相应内容。
                // this.msgs.splice(0, 2);

                if (!this.comment) {
                    alert('请输入内容');
                    return false;
                }
                this.msgs.push(this.comment);
                this.comment = '';

                localStorage.msgs = JSON.stringify(this.msgs);
            },
            delete_msg(index) {
                this.msgs.splice(index, 1);
                localStorage.msgs = JSON.stringify(this.msgs);
            }
        }
    })
</script>
<script>
    // 前台数据库
    // localStorage: 永久存储
    // sessionStorage:临时存储(所属页面标签被关闭后,清空)

    // 存
    // localStorage.n1 = 10;
    // sessionStorage.n2 = 20;

    // 取
    // console.log(localStorage.n1);
    // console.log(sessionStorage.n2);

    // 数组等类型需要先序列化成JSON
    // localStorage.arr = JSON.stringify([1, 2, 3]);
    // console.log(JSON.parse(localStorage.arr));

    // 清空数据库
    // localStorage.clear();
</script>
</html>

4. delimiter:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div id="app">
        {{ msg }}
        [{ msg }]
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            msg: 'message'
        },
        delimiters: ['[{', '}]'],  // 修改插值表达式符号,delimiters指定对应的格式才能渲染出来,
        // 用不同格式是防止和django前端的表示变量的{{}}冲突
    })
</script>
</html>

5. Filter:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>过滤器</title>
</head>
<body>
    <div id="app">
        <!--
        总结:
        1、在filters成员中定义过滤器方法
        2、可以对多个值进行过滤,过滤时还可以额外传入辅助参数
        3、过滤的结果可以再进行下一次过滤(过滤的串联)
        -->
        <p>{{ num | f1 }}</p>
        <p>{{ a, b | f2(30, 40) | f3 }}</p>
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            num: 10,
            a: 10,
            b: 20,
        },
        filters: {
            // 传入所有要过滤的条件,返回值就是过滤的结果
            f1 (num) {
                console.log(num);
                return num * 10;
            },
            f2 (a, b, c, d) {
                console.log(a, b, c, d);
                return a + b + c + d;
            },
            f3 (num) {
                return num * num;
            }
        }
    })
</script>
</html>

6. Calculate properties:

  • The method of calculation of computed attribute can be declared properties (Method properties must not be repeated in data declarations)

  • The method attribute must render the page, will enable the binding method, binding method is the return value of the method attribute
  • All variables that appear in the bound method will be monitored, any occurrence of a variable value updates will re-trigger the binding methods to update the value of the property method
  • General problem to be solved: a variable value dependent on multiple variables

<body>
    <div id="app">
        <input type="number" min="0" max="100" v-model="n1">
        +
        <input type="number" min="0" max="100" v-model="n2">
        =
        <button>{{ result }}</button>
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            n1: '',
            n2: '',
            // result: 0,
        },
        
        computed: {
            result () {
                console.log('被调用了');
                n1 = +this.n1;
                n2 = +this.n2;
                return n1 + n2;
            }
        }
    })
</script>

7. Monitoring attributes:

<body>
    <div id="app">
        <p>姓名:<input type="text" v-model="full_name"></p>
        <p>姓:{{ first_name }}</p>
        <p>名:{{ last_name }}</p>
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            full_name: '',
            first_name: '未知',
            last_name: '未知',
        },
        watch: {
            // n是监听的属性当前值,o是其上一次的值,监听的属性值每次更新都会回调监听方法

            /**
             * 总结:
             * 1、监听的属性需要在data中声明,监听方法不需要返回值
             * 2、监听的方法名就是监听的属性名,该属性值发生更新时就会回调监听方法
             * 3、监听方法有两个回调参数:当前值,上一次值
             *
             * 解决的问题:多个变量值依赖于一个变量值
             */
            full_name(n, o) {
                name_arr = n.split('');
                this.first_name = name_arr[0];
                this.last_name = name_arr[1];
            },
        }
    })
</script>

Guess you like

Origin www.cnblogs.com/lidandanaa/p/12056720.html