[1118 | Day61] Vue use

First, the mount point (el)

<!--导入vue-->
<script src="js/vue.js"></script>

<script>
    new Vue({
        el:'ccs选择器'
    })
</script>

Vue this will be associated with the corresponding selector This setting css good

important point:

  • Mount point only through the results of the first match
  • html tag body and can not be used as the mount point
  • A mount point on the use of only generally id selector (Uniqueness)

Second, the interpolation expression

html差值部分页面
<h1>{{msg}}</h1>
Vue部分代码
<script>
    new Vue({
        el:'h1'
        data:{
        msg:'插入的信息'
    }
    })
</script>

注意点: Under no greater number of other conditions about the Vue msg variable transmission changes will change msg

Inside filter function value

<h2>{{msg|函数名}}</h2> h2标签内值为msg进过函数处理后的返回值

Third, once bound variable rendering rendering

还是基于上述的html页面

<!--原html页面-->
<h1>{{msg}}</h1>

<!--进行约束设置,设置后一次性渲染,后面msg发送变化后里面内容不会发送改变-->
<h1 v-once="msg">{{msg}}</h1>
<h1 v-once="msg" v-text='msg'></h1>
<h1 v-once="msg" v-html='msg'></h1>

<h1 v-once="msg">{{msg+msg2}}</h1>
<!--一次性渲染,插值表达式中的任何一个变量被限制,整个结果就不可变-->

改变值我们可以通过input框,输入值改变msg变量的值
<input type="text"  v-model="msg">
注意我们不能使用
<input type="text"  :value="msg">
这个只能显示msg,input输入的值不会使得msg的值发送变化

Fourth, the text instructions

1.v-text

<h2 v-text="msg"></h2>

Tags within the test h2 is a value in the pass vue msg variable, but where the label is not parsed html

Value which can pass the string four operations related operations like

2.v-html

<h2 v-html="html"></h2>
<--假设html变量为<a>ss<a>-->

h2 tags will be displayed ss

html tags may be parsed

Fifth, the property commands

Emphasis

属性指令:v-bind:属性名="属性值" => v-bind: 可以简写为 :

Different style of writing about

A wording:

<!--1)变量:变量的值为字典-->
<div :style="my_style" class='test'></div>
<script>
    new Vue({
        el:'.test'
        data:{
        my_style: {
                width: '100px',
                height: '100px',
                'background-color': 'cyan',
                borderRadius: '50%'
            },
    }
    })
</script>

Written two:

<!--2)字典中的多个变量-->
<div :style="{width: w, height: h, background: b}"></div>
<script>
    new Vue({
        el:'.test'
        data:{
            w: '50px',
            h: '50px',
            b: 'red',
    }
    })
</script>

Writing about the class

<!--class属性-->
<!--<div class="box blue"></div>-->
<div :class="c"></div>
<div :class="[c1, c2]"></div>
<div :class="[c1, 'blue']"></div>
<!--x为类名,是否生效有变量y(true|false)值决定-->
<div :class="{x: y}"></div>
<div :class="[{'box': true}, c2]"></div>


<script>
    new Vue({
        el: '#app',
        data: {
            c: 'box blue',
            c1: 'box',
            c2: 'green',
            y: true,
        }
    })
</script>

Sixth, the event command

事件指令 v-on:事件名="函数" => v-on: 可以简写为 @

Seven, to create a function

method one

<script>
    new Vue({
        el:'h1'
        data:{
        msg:'插入的信息'
    }
       methods: {
            函数名 () {
                函数体
            },
    })
</script>

Method Two

<script>
    new Vue({
        el:'h1'
        data:{
        msg:'插入的信息'
    }
       methods: {
            函数名:function () {
                函数体
            },
    })
</script>

Method Three

<script>
    new Vue({
        el:'h1'
        data:{
        msg:'插入的信息'
    }
       methods: {
            函数名:() => {
                函数体
            },
    })
</script>

注意点: Such an approach, the interior get this (this here refers window)

Eight, event participation and do not command mass difference parameter passing

<!--没有传值默认传 函数会接收到事件对象 -->
<div @click="btnClick1">{{ msg }}</div>

<!--方法()不会直接调用方法,而是在点击触发后进行传参,接收到的参数就是传入的参数-->
<div @click="btnClick2(1, msg)">{{ msg }}</div>

<!--一旦书写 方法() 就不再传入事件对象,通过 $event 手动传入事件对象-->
<div @click="btnClick3(msg, $event, $event)">{{ msg }}</div>

IX Form command

<form action="">
    <!--表单指令:v-model="变量"-->

    <!--双向绑定:一个地方修改值,所有地方的数据都会被更新-->
    <div>
        <input type="text" v-model="info" name="usr">
        <input type="password" v-model="info" name="pwd">
        <p>{{ info | infoFilter }}</p>
    </div>

    <div>
        <!--单选框:v-model="变量存放的是某个单选框的value值,代表该选框选中"-->
        男<input type="radio" name="sex" value="male" v-model="sex_val">
        女<input type="radio" name="sex" value="female" v-model="sex_val">
    </div>


    <div>
        <!--单独的复选框:v-model="true|false代表该选框是否选中"-->
        是否同意<input v-model="cb_val" value="yes" type="checkbox" name="agree">
    </div>

    <div>
        <!--群复选框:v-model="存放选中选框value的数组"-->
        <!--cbs_valrug如果传空数组会一个都不选,如果数组对应里面的value会勾选-->
        <!--cbs_valrug如果传true|false,里面的能容就表示要么全选要么都不选->
        男<input v-model="cbs_val"  value="male" type="checkbox" name="hobby">
        女<input v-model="cbs_val"  value="female" type="checkbox" name="hobby">
        不能说<input v-model="cbs_val"  value="others" type="checkbox" name="hobby">
        <p>{{ cbs_val }}</p>
    </div>

    <div>
        <input type="submit">
    </div>
</form>


<script>
    new Vue({
        el: '#app',
        data: {
            info: '',
            sex_val: 'female',
            cb_val: 0,
            cbs_val: ["others"]  
        }
    })
</script>

Ten, conditional instructions

  • v-if: not render hide, set up a cache memory when hidden, you can set the cache key by key attributes
  • v-show: hide with display: none render

use:

<div class="box red" v-if="ture|flase" key="box_red"></div>  
<div class="box blue" v-show="ture|flase"></div>

接收的值只能是ture|flase,如果是0则是flase,如果是1则是ture
key属性可以对于在内存中名字进行设置,且f12你看不到key这个属性
关于内存的存储
  • localStorage.key name: key values: permanent but can manually delete the cache
  • sessionStorage.key name: key values: temporary buffer off the page or close the browser will disappear reopen

Calls on memory

  • localStorage.key name
  • sessionStorage.key名称
  • v-if|v-elif|v-else:
    • important point
      • When in effect if not go down the same way, too elif
      • front else does not take effect even if he has set flasehe will still take effect
      • All else generally do not do any value to him are true

Guess you like

Origin www.cnblogs.com/fxyadela/p/11885250.html