season2-全局API

第1节:Vue.directive 自定义指令

Vue.directive自定义指令

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <div v-changecolor="color" id="demo">
    <div v-changesize="size" id="demosize">
      {{num}}
    </div>
  </div>
  <div>
      <button @click="add">加分</button>
  </div>
</div>

<script>
  Vue.directive('changecolor', function (el, binding, vnode) {
    console.log(el);
    console.log(binding);
    el.style = 'color:' + binding.value;
  });
  Vue.directive('changesize', {
    bind:function(el, binding){//被绑定
     console.log('1 - bind');
    },
    inserted:function(){//绑定到节点
          console.log('2 - inserted');
    },
    update:function(){//组件更新
          console.log('3 - update');
          el.style = 'font-size:' + binding.value;
    },
    componentUpdated:function(){//组件更新完成
          console.log('4 - componentUpdated');
    },
    unbind:function(){//解绑
          console.log('5 - unbind');
    }
  })
  var app = new Vue({
    el: '#app',
    data: {
      num: 10,
      color: 'red',
      size: '200px',
    },
    methods: {
      add: function () {
        this.num++;
      }
    }
  })
</script>
</body>
</html>
  1. 自定义的指令:changecolor不能有大写字母。
  2. Vue.directive 参数说明:
  • el: 指令所绑定的元素,可以用来直接操作DOM。

     
     
    15756588-132b94b7902ad1f5.png
    console.log(el)结果
  • binding: 一个对象,包含指令的很多信息。

     
     
    15756588-8c89cb2b297e79ae.png
    console.log(binding)
  • vnode: Vue编译生成的虚拟节点。

.自定义指令的生命周期

自定义指令有五个生命周期(也叫钩子函数),分别是 bind,inserted,update,componentUpdated,unbind

  1. bind:只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个绑定时执行一次的初始化动作。
  2. inserted:被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于document中)。
  3. update:被绑定于元素所在的模板更新时调用,而无论绑定值是否变化。通过比较更新前后的绑定值,可以忽略不必要的模板更新。
  4. componentUpdated:被绑定元素所在模板完成一次更新周期时调用。
  5. unbind:只调用一次,指令与元素解绑时调用。

2.Vue.extend构造器的延伸

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <title>Extends Optin Demo</title>
</head>
<body>
    <h1>Extends Optin Demo</h1>
    <hr>
    <author id="author"></author>

    <script type="text/javascript">
        var authorURL = Vue.extend({
          template: "<p><a :href='authorURL'>{{ authorName }}</a></p>",
          data: function () {
            return {
              authorName: 'Hahah',
              authorURL: 'http://www.baidu.com',
            }
          }
        });

        new authorURL().$mount("#author")
    </script>
</body>
</html>
  • 通常用于组件中使用,另外,标签中一般绑定id值,若是使用广泛,亦可绑定标签名,同jquery。
  • 注意template中的<a :href>,是v-bind的简写。

3. Vue.set全局操作

Vue.set 的作用就是在构造器外部操作构造器内部的数据、属性或者方法。比如在vue构造器内部定义了一个count为1的数据,我们在构造器外部定义了一个方法,要每次点击按钮给值加1.就需要用到Vue.set。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <title>vue set</title>
</head>
<body>
    <div id="app">
      {{ count }}
      <ul>
        <li v-for="item in arr">{{item}}</li>
      </ul>
    </div>
    <p><button onclick="add()">加分</button></p>

    <script type="text/javascript">
        function add() {
          app.count++;
          outData.count++;
          Vue.set(app.arr, 1, 'adw')
        }

        var outData = {
          count: 1,
          goods: 'YY',
          arr: ['aaa', 'bbb', 'ccc']
        }

        var app = new Vue({
          el: '#app',
          data: outData
        })
    </script>
</body>
</html>

由于Javascript的限制,Vue不能自动检测以下变动的数组。
当你利用索引直接设置一个项时,vue不会为我们自动更新。
当你修改数组的长度时,vue不会为我们自动更新。

4. Vue 生命周期

 
15756588-10c64bab42a4da6e.png
vue生命周期解释
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <title>构造器的生命周期</title>
</head>
<body>
    <h1>构造器的生命周期</h1>
    <hr>
    <div id="app">
        {{message}}
        <p><button @click="jia">加分</button></p>
    </div>
        <button onclick="app.$destroy()">销毁</button>

    <script type="text/javascript">
        var app=new Vue({
            el:'#app',
            data:{
                message: 1
            },
            methods:{
                jia:function(){
                    this.message ++;
                }
            },
            beforeCreate:function(){
                console.log('1-beforeCreate 初始化之后');
            },
            created:function(){
                console.log('2-created 创建完成');
            },
            beforeMount:function(){
                console.log('3-beforeMount 挂载之前');
            },
            mounted:function(){
                console.log('4-mounted 被创建');
            },
            beforeUpdate:function(){
                console.log('5-beforeUpdate 数据更新前');
            },
            updated:function(){
                console.log('6-updated 被更新后');
            },
            activated:function(){
                console.log('7-activated');
            },
            deactivated:function(){
                console.log('8-deactivated');
            },
            beforeDestroy:function(){
                console.log('9-beforeDestroy 销毁之前');
            },
            destroyed:function(){
                console.log('10-destroyed 销毁之后')
            }

        })
    </script>
</body>
</html>
 
15756588-e1af19b4f5717054.png
image.png

5. template 标签模板

组件是什么:其实就是HTML中不存在的标签,分为全局组件和局部组件。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <title>template 三种写法</title>
</head>
<body>
    <h1>template 三种写法</h1>
    <hr>
    <div id="app">
        {{message}}
    </div>

    <template id="dd2">
      <h2>我是template 标签模板</h2>
    </template>

    <script type="x-template" id="demo3">
        <h2 style="color:red">我是script标签模板</h2>
    </script>

    <script type="text/javascript">
        var app=new Vue({
            el:'#app',
            data:{
                message: 'dddd'
            },
             methods1:
            // template:`
            //   <h2 style="color: red;">我是模板</h2>
            // `

          // template: "#dd2"
          template: "#demo3"



        })
    </script>
</body>
</html>

6:Component 初识组件

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  <title>template 三种写法</title>
</head>
<body>
<h1>template 三种写法</h1>
<hr>
<div id="app">
  <test></test>
  <panda></panda>
</div>

<script type="text/javascript">
  <!--定义全局化组件-->
  Vue.component('test', {
    template: `<div style="color:red;">全局化注册的jspang标签</div>`
  })

  var app = new Vue({
    el: '#app',
    data: {
      message: 'dddd'
    },
    components: {
      "panda": {
        template: `<div style="color:red;">局部注册的panda标签</div>`
      }
    }
  })
</script>
</body>
</html>

组件绑定数据的方法:使用props

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  <title>组件2</title>
</head>
<body>
<h1>组件2</h1>
<hr>
<div id="app">
  <test></test>
  <panda here="message"></panda>
  <panda :here="message"></panda>
</div>

<script type="text/javascript">
  var app = new Vue({
    el: '#app',
    data: {
      message: 'dddd'
    },
    components: {
      "panda": {
        template: `<div style="color:red;">Pandas from {{ here }}</div>`,
        props: ['here']
      }
    }
  })
</script>
</body>
</html>

父子组件关系:顺序一定要合理jspang---city

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  <title>组件3</title>
</head>
<body>
<h1>component-3</h1>
<hr>
<div id="app">
  <jspang></jspang>
</div>
<script type="text/javascript">
  var city = {
    template: `<div>Sichuan of China</div>`
  }
  var jspang = {
    template: `
      <div>
        <p> Panda from China!</p>
        <city></city>
      </div>`,
    components: {
      "city": city
    }
  }
  var app = new Vue({
    el: '#app',
    components: {
      "jspang": jspang
    }

  })
</script>
</body>
</html>

组件之间的切换:其实实现设计模式中的——策略模式。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
  <title>组件3</title>
</head>
<body>
<h1>component-3</h1>
<hr>
<div id="app">
  <component v-bind:is="who"></component>
  <button @click="changeComponent">changeComponent</button>
</div>
<script type="text/javascript">
  var componentA = {
    template: `<div>I'm componentA</div>`
  }

  var componentB = {
    template: `<div>I'm componentB</div>`
  }

  var componentC = {
    template: `<div>I'm componentC</div>`
  }

  var app = new Vue({
    el: '#app',
    data: {
      who: 'componentA'
    },
    components: {
      "componentA": componentA,
      "componentB": componentB,
      "componentC": componentC,
    },
    methods: {
      changeComponent: function () {
        if (this.who == 'componentA') {
          this.who = 'componentB';
        } else if (this.who == 'componentB') {
          this.who = 'componentC';
        } else {
          this.who = 'componentA';
        }
      }
    }

  })
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值