vue

<div id="content">
    {{ msg }}
    <div v-text="msg"></div>
    <div v-html="msg"></div>
</div>
<script src="vue.js"></script>
<script>
    new Vue({
        el:"#content",
        data(){
            //data中是一个函数,函数中return一个对象,可以是空对象,但是不能不return
            return {
                msg:"<h2>zhangqing</h2>"
            }
        }
    })
</script>

 v-show:相当于 style.display

    v-if:相当于创建

v-if vs v-show
v-if 是“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建。

v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做——直到条件第一次变为真时,才会开始渲染条件块。

相比之下,v-show 就简单得多——不管初始条件是什么,元素总是会被渲染,并且只是简单地基于 CSS 进行切换。

一般来说,v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用 v-show 较好;如果在运行时条件很少改变,则使用 v-if 较好。

v-bind和v-on

      v-bind可以绑定标签中任何属性  简写 :

      v-on可以监听 js中所有事件    简写 @

 1 <!DOCTYPE html>
 2 <html lang="zh-CN">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <meta name="viewport" content="width=device-width, initial-scale=1">
 7     <style>
 8         .box {
 9              200px;
10             height: 200px;
11             background-color: red;
12         }
13         .active{
14             background-color: green;
15         }
16 
17     </style>
18 </head>
19 <body>
20 
21 <div id="app">
22     <!--<button v-on:click = 'handlerChange'>切换颜色</button>-->
23    <!--v-bind 标签中所有的属性 img标签src alt,a标签的href title id class-->
24     <!--<img v-bind:src="imgSrc" v-bind:alt="msg">-->
25     <!--<div class="box" v-bind:class = '{active:isActive}'></div>-->
26 
27      <button @mouseenter = 'handlerChange' @mouseleave = 'handlerLeave'>切换颜色</button>
28    <!--v-bind 标签中所有的属性 img标签src alt,a标签的href title id class-->
29     <img :src="imgSrc" :alt="msg">
30     <div class="box" :class = '{active:isActive}'></div>
31 </div>
32 <!--1.引包-->
33 <script src='./vue.js'></script>
34 <script>
35     //数据驱动视图  设计模式 MVVM  Model  View  ViewModel
36 
37     //声明式的JavaScript框架
38 
39 //    v-bind和v-on的简便写法 :  @
40     new Vue({
41         el: '#app',
42         data() {
43             //data中是一个函数 函数中return一个对象,可以是空对象 但不能不return
44             return {
45                 imgSrc:'./1.jpg',
46                 msg:'美女',
47                 isActive:true
48             }
49         },
50         methods:{
51             handlerChange(){
52 //                this.isActive = !this.isActive;
53                 this.isActive = false;
54             },
55             handlerLeave(){
56                 this.isActive = true;
57             }
58         }
59        
60     })
61 </script>
62 
63 </body>
64 </html>
on bind

v-for 遍历:可以遍历列表,也可以遍历对象

      在使用vue的v-for指令的时候,一定要绑定key,避免vue计算dom

 1 <div id="app">
 2     <ul v-if='data.status == "ok"'>
 3         <li v-for="(item,index) in data.users" :key="item.id">
 4             <h3>{{ item.id }}---{{ item.name }}</h3>
 5         </li>
 6     </ul>
 7     <div v-for="(value,key) in data.person" :key="index">    #遍历对象时,第一个是value,第二个是key
 8         {{ key }} --- {{ value }}
 9     </div>
10 </div>
11 <script src='./vue.js'></script>
12 <script>
13     new Vue({
14         el:"#app",
15         data(){
16             return{
17                 data:{
18                     status:"ok",
19                     users:[
20                         {id:1,name:"alex",age:18},
21                         {id:2,name:"wusir",age:30},
22                         {id:3,name:"yuan",age:58}
23                     ],
24                     person:{
25                         name:"alex"
26                     }
27                 }
28             }
29         }
30     })
31 </script>
v-for

watch和computed

      watch可以监听单个属性,也可以监听多个,computed动态监听函数返回的数据

 1 <!DOCTYPE html>
 2 <html lang="zh-CN">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <meta name="viewport" content="width=device-width, initial-scale=1">
 7 </head>
 8 <body>
 9     <div id="app">
10         <p>{{ msg }}</p>
11         <button @click = 'clickHandler'>修改</button>
12     </div>
13     <script src="vue.js"></script>
14     <script>
15         new Vue({
16             el:'#app',
17             data(){
18                 return {
19                     msg:"alex",
20                     age:18
21                 }
22 
23             },
24             methods:{
25                 clickHandler(){
26                     this.msg = "wusir"
27                 }
28             },
29             watch:{
30                 //watch单个属性,如果想监听多个属性 声明多个属性的监听
31                 'msg':function (value) {
32 
33                     console.log(value);
34 
35                     if (value === 'wusir'){
36                         alert(1);
37                        this.msg = '大武sir'
38                     }
39                 },
40                 'age' :function (value) {
41                     
42                 }
43             }
44         })
45     </script>
46 </body>
47 </html>
watch ,computed

计算属性 computed

       监听多个属性

 1 <!DOCTYPE html>
 2 <html lang="zh-CN">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <meta name="viewport" content="width=device-width, initial-scale=1">
 7 </head>
 8 <body>
 9 <div id="app">
10     <p>{{ myMsg }}</p>
11     <button @click='clickHandler'>修改</button>
12 </div>
13 <script src="vue.js"></script>
14 <script>
15     let vm = new Vue({
16         el: '#app',
17         data() {
18             return {
19                 msg: "alex",
20                 age: 18
21             }
22 
23         },
24         created() {
25             //定时器  ajax  库 function(){}
26             setInterval(() => {
27 
28             })
29         },
30         methods: {
31             clickHandler() {
32                 //this的指向就是当前对象
33                 this.msg = "wusir";
34                 this.age = 20;
35             },
36             clickHandler: function () {
37                 console.log(this);
38             }
39 
40         },
41         computed: {
42             myMsg: function () {
43                 //业务逻辑
44 
45 //                    计算属性默认只有getter方法
46                 return `我的名字叫${this.msg},年龄是${this.age}`;
47             }
48         }
49     })
50 
51     console.log(vm);
52 </script>
53 </body>
54 </html>
computed
原文地址:https://www.cnblogs.com/zhangqing979797/p/10085315.html