vue2.5入门 - 基础语法

1.vue基本介绍
三大主流前端框架:Angular、React、Vue

2.基础知识->案例实战->TodoList->Vue-cli->TodoList

3.创建第一个Vue实例

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vue入门</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>

<body>
    <div id="root">{{msg}}</div>

    <script>
        // vue实例与#root进行绑定
        new Vue({
            el:"#root",         
            data:{
                msg : "hello world"
            },                  
        })
    </script>
</body>
</html>

这里写图片描述

4.挂载点、模板、vue实例之间的关系

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vue入门</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>

<body>
    <div id="root"></div>

    <script>
        // vue实例与#root进行绑定
        new Vue({                       //  vue实例
            el:"#root",                 //  挂载点 
            template:'<h1>hello {{msg}}</h1>',              //  模板  
            data:{
                msg : "world"
            },                  
        })
    </script>
</body>
</html>

这里写图片描述

5.Vue实例中的数据,事件与方法

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vue入门</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>

<body>
    <div id="root">
        <h1>插值表达式:{{number}}</h1>
        <h1 v-text="number"></h1>
        <div v-text="content"></div>
        <div v-html="content"></div>
        <div v-on:click="handleClick">事件:{{content1}}</div>
        <div @click="handleClick">事件绑定简写:{{content1}}</div>
    </div>

    <script>
        // vue实例与#root进行绑定
        new Vue({                       //  vue实例
            el:"#root",                 //  挂载点     
            data:{                      //  数据                      
                msg : "world",
                number : 123,
                content : "<h1>hello</h1>",
                content1 : "hello",
            },      
            methods : {                 //  方法                      
                handleClick : function(){
                    this.content1 = "world";
                }
            }           
        })
    </script>
</body>
</html>

这里写图片描述

6.Vue中的属性绑定和双向数据绑定

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>属性绑定和双向数据绑定</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>

<body>

  <div id="root">
      <div :title=`属性绑定演示-${title}`>
          hello world
      </div>
      <input v-model="content" />
      <div>{{content}}</div>
  </div>


</body>
</html>

<script>
    new Vue({
        el:"#root",
        data(){
            return {
                title : 'this is hello world',
                content : "this is a content"
            }
        }
    })
</script>

这里写图片描述

7.Vue中的计算属性和侦听器

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>计算属性和侦听器</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>

<body>

  <div id="root">
      姓:<input v-model="firstName" />
      名:<input v-model="lastName" />
      <div>{{fullName}}</div>
      <div>{{count}}</div>
  </div>


</body>
</html>

<script>
    new Vue({
        el:"#root",
        data(){
            return {
                firstName : '',
                lastName : '',
                count : 0,
            }
        },
        computed: {
            fullName : function(){
                return this.firstName + ' '  +this.lastName
            }
        },
        watch : {
            fullName : function(){
                this.count++;
            },
        }
    })
</script>

这里写图片描述

8.v-if,v-show与v-for指令

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>计算属性和侦听器</title>
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.js"></script>
</head>

<body>

  <div id="root">
      <div v-if="show">hello world</div>
      <button @click="handleClick">toggle</button>
      <ul>
          <li v-for="(item,index) of list" :key="index">{{index}} - {{item}}</li>
      </ul>
  </div>


</body>
</html>

<script>
    new Vue({
        el:"#root",
        data(){
            return {
                show : true,
                list : [1,2,3]
            }
        },
        methods: {
            handleClick(){
                this.show = !this.show;
            }
        }
    })
</script>

这里写图片描述

猜你喜欢

转载自blog.csdn.net/zjsfdx/article/details/80010693