Vue基础之实验篇——学习周记14

实验1——“平平无奇”?

<!DOCTYPE html>
<html>
	<head lang="en">
		<script src=" https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
		<meta charset="utf-8">
		<title>实验1</title>
	</head>
	<body>
		<div id="container">
			<h2>{{msg}}</h2>
			<button @click="add" style="font-size: large;">click</button>
			   <p v-change="handleClick">文字</p >
			  </div>
			
		<script>
			//directive
			new Vue({
				el:'#container',
				data:{
					msg:'Hello Vue',
					colors:['orange','yellow','red','pink'],
					order:0
				},
				methods:{
					add:function(){
					this.order++;	
					},
					handleClick:function(){
						if(this.order==this.colors.length){
							this.order=0;
						}
						return this.colors[this.order];
					}
				},
				directives:{
					changes:{
						bind:function(el,bindings){
							console.log('指令已经绑定到元素了');
							console.log(el);
							console.log(bindings);
							el.style.backgroundColor=bindings.value();
						},
						update:functioon(el,bindings){
							console.log('指令的数据有所变化');
							console.log(el);
							console.log(bindings);
							el.style.bckgroundColor=bindings.value();
						},
						unbind:function(){
							console.log('已经解绑了')
						}
					}
				}
			})
		</script>
	</body>
</html>

【运行效果如图】
在这里插入图片描述

实验2——四种颜色任你选择

<body>
  <div id="container">
   <h2>{{msg}}</h2>
   <button @click="add" style="font-size: large;">click</button>
   <p v-change="handleClick">文字</p >
  </div>
   <script>
    new Vue({
     el:'#container',
     data:{
      msg:'hello',
      color:['orange','yellow','red','pink'],
      order:0
     },
     methods:{
      add:function(){
       this.order++;
      },
      handleClick:function(){
       if(this.order==this.color.length){
        this.order=0;
       }
       return this.color[this.order];
      }
     },
     directives:{
      change:{
       bind:function(el,bindings){
        el.style.backgroundColor=bindings.value();
       },
       update:function(el,bindings){
        el.style.backgroundColor=bindings.value();
       },
       unbind:function(){
        console.log('已经解绑了')
       }
      }
     }
    })
    
   </script>
 </body>
</html>

【运行效果如图】
在这里插入图片描述
点击按钮“click”,后:
在这里插入图片描述
再一次点击按钮“click”,后:
在这里插入图片描述
又点击了一次按钮“click”,后:
在这里插入图片描述
再再再点击按钮“click”,后,“文字”的背景颜色变回最开始的橙色

实验3——指令

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <p>页面载入时,input 元素自动获取焦点:</p>
  <input v-focus>
</div>

<script>
// 创建根实例
new Vue({
  el: '#app',
  directives: {
    // 注册一个局部的自定义指令 v-focus
    focus: {
      // 指令的定义
      inserted: function (el) {   //生命周期函数
        // 聚焦元素
        el.focus()
      }
    }
  }
})
</script>
</body>
</html>

【运行效果如图】
在这里插入图片描述

实验4——计数器

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 </title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
	<div id="counter-event-example">
	  <p>{{ total }}</p>
	  <button-counter v-on:increment="incrementTotal"></button-counter>
	  <button-counter v-on:increment="incrementTotal"></button-counter>
	</div>
</div>

<script>
Vue.component('button-counter', {
  template: '<button v-on:click="incrementHandler">{{ counter }}</button>',
  data: function () {
    return {
      counter: 0
    }
  },
  methods: {
    incrementHandler: function () {
      this.counter += 1
      this.$emit('increment')
    }
  },
})
new Vue({
  el: '#counter-event-example',
  data: {
    total: 0
  },
  methods: {
    incrementTotal: function () {
      this.total += 1
    }
  }
})
</script>
</body>
</html>

【运行效果如图】
在这里插入图片描述
分别点击下面的两个“0”,可观察到:上面的数字“0”随之变动且为下面两个数字之和:
在这里插入图片描述
★VUE的学习到这里就差不多啦,敬请期待下一篇更精彩有趣的内容吧★

!喜欢的话不要忘记【一键三连】哦!撒花花啦~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值