Vue.js 组件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

全局组件实例
注册一个简单的全局组件 baidu,并使用它:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以下实例中使用 v-bind 指令将 todo 传到每一个重复的组件中:

<!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">
	<ol>
    <todo-item v-for="item in sites" v-bind:todo="item"></todo-item>
  	</ol>
</div>

<script>
Vue.component('todo-item', {
  props: ['todo'],
  template: '<li>{{ todo.text }}</li>'
})
new Vue({
  el: '#app',
  data: {
    sites: [
      { text: 'baidu' },
      { text: 'Google' },
      { text: 'Taobao' }
    ]
  }
})
</script>
</body>
</html>

在这里插入图片描述

在这里插入图片描述

Vue.component('my-component', {
  props: {
    // 基础的类型检查 (`null` 和 `undefined` 会通过任何类型验证)
    propA: Number,
    // 多个可能的类型
    propB: [String, Number],
    // 必填的字符串
    propC: {
      type: String,
      required: true
    },
    // 带有默认值的数字
    propD: {
      type: Number,
      default: 100
    },
    // 带有默认值的对象
    propE: {
      type: Object,
      // 对象或数组默认值必须从一个工厂函数获取
      default: function () {
        return { message: 'hello' }
      }
    },
    // 自定义验证函数
    propF: {
      validator: function (value) {
        // 这个值必须匹配下列字符串中的一个
        return ['success', 'warning', 'danger'].indexOf(value) !== -1
      }
    }
  }
})

在这里插入图片描述

在这里插入图片描述

<!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>

在这里插入图片描述
在这里插入图片描述

<!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="components-demo3" class="demo">
    <button-counter2></button-counter2>
    <button-counter2></button-counter2>
	<button-counter2></button-counter2>
</div>

<script>
var buttonCounter2Data = {
  count: 0
}
Vue.component('button-counter2', {
    /*
    data: function () {
	    // data 选项是一个函数,组件不相互影响
        return {
            count: 0
        }
    },
    */
    data: function () {
        // data 选项是一个对象,会影响到其他实例
        return buttonCounter2Data
    },
    template: '<button v-on:click="count++">点击了 {{ count }} 次。</button>'
})
new Vue({ el: '#components-demo3' })
</script>
</body>
</html>

在这里插入图片描述

其他:
1、父组件给子组件传值的时候,如果想传入一个变量,写法如下:
在这里插入图片描述
在这里插入图片描述
2、
在这里插入图片描述

<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

<div id="app">
    <div id="counter-event-example">
      <p>{{ total }}</p>
      <button-counter v-on:increment="incrementTotal"></button-counter><br/>
      <button-counter v-on:increment="incrementTotal"></button-counter>
    </div>
</div>
Vue.component('button-counter', {
  template: '<div><button v-on:click="incrementHandler(1)">-</button>{{ counter }}<button v-on:click="incrementHandler(2)">+</button></div>',
  data: function () {
    return {
      counter: 0
    }
  },
  methods: {
    incrementHandler: function (v) {
        if(v==1){
            this.counter -= 1
              this.$emit('increment',1)
        }else{ 
            this.counter += 1
              this.$emit('increment',2)
        }
      
    }
  },
})
new Vue({
  el: '#counter-event-example',
  data: {
    total: 0
  },
  methods: {
    incrementTotal: function (d) {
        if(d==1){
            this.total -= 1
        }else{
            this.total += 1
        }
      
    }
  }
})

在这里插入图片描述
3、
在这里插入图片描述

<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<div id="div_col">
  <p>自定义事件</p>
  <p>{{total}}</p>
  <example1 v-on:incretment="incretmentTotal" value="点击"></example1>
</div>
Vue.component('example1', {
  props: ['value'],
  template: '<button v-on:click="incrementHanlder">{{value}}</button>',
  // data: function () {
  //   return {
  //     count: 0
  //   }
  // },
  methods: {
    incrementHanlder: function () {
      // this.count += 1
      this.$emit('incretment')
    }
  }
})
new Vue({
  el: '#div_col',
  data: {
    total: 0
  },
  methods: {
    incretmentTotal: function () {
      this.total += 1
    }
  }
})

在这里插入图片描述

4、你可能在找的系统点击事件的例子。

<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<div id="app">
    <p>点击元素输出元素内容:</p> 
    <ol>
    <todo-item v-for="item in sites" v-bind:todo="item" @click.native="alert(item.text)"></todo-item>
    </ol>
</div>
Vue.component('todo-item', {
  props: ['todo'],
  template: '<li>{{ todo.text }}</li>'
})
new Vue({
  el: '#app',
  data: {
    sites: [
      { text: 'baidu' },
      { text: 'Google' },
      { text: 'Taobao' }
    ]
  }
})

在这里插入图片描述
5、
在这里插入图片描述

<script src="https://cdn.staticfile.org/vue/2.2.2/vue.js"></script>

<div id="app">
  <example
   :propa="'asda'"
   :propb = "'aasasa'"
   :propc="'sdf'"
   :prope="{a:'a'}"
   :propf="100"
  ></example>
 </div>
Vue.component('example', {
   props: {
    // 基础类型检测 (`null` 意思是任何类型都可以)
    propa: Number,
    // 多种类型
    propb: [String, Number],
    // 必传且是字符串
    propc: {
     type: String,
     required: true
    },
    // 数字,有默认值
    propd: {
     type: Number,
     default: 1000
    },
    // 数组/对象的默认值应当由一个工厂函数返回
    prope: {
     type: Object,
     default: function () {
     return { message: 'hello' }
     }
    },
    // 自定义验证函数
    propf: {
     type: Number,
      validator: function (value) {
        // 这个值必须匹配下列字符串中的一个
        return value>0? -1:1
      },
      defalut:12
    }
   },
   template: `
    <table border="1px">
		<tr>
       		<th>propA</th>
       		<th>propB</th>
       		<th>propC</th>
       		<th>propD</th>
			<th>propE</th>
       		<th>propF</th>
		</tr>
		<tr>
       		<td>{{ propa }}</td>
       		<td>{{ propb }}</td>
       		<td>{{ propc }}</td>
       		<td>{{ propd }}</td>
			<td>{{ prope }}</td>
       		<td>{{ propf }}</td>
		</tr>
    </table>`
  })
  new Vue({
   el: "#app"
  });

在这里插入图片描述

6、这个例子的执行过程注解:

<!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 id="counter-event-example">
    // 6. 页面上更新total的值
       <p>{{ total }}</p> 
          // 4. 这里的自定义事件再次触发局域方法incrementTotal
      <button-counter v-on:increment="incrementTotal"></button-counter>
      <button-counter v-on:increment="incrementTotal"></button-counter>
    </div>
</div>

<script>
// 1. 注册全局组件
Vue.component('button-counter', {
   //  2. button绑定点击事件incrementHandler  template: '<button v-on:click="incrementHandler">{{ counter }}</button>',
  data: function () {
    return {
      counter: 0
    }
  },
  methods: {
    // 3. 点击事件触发后,再次触发自定义事件increment
    incrementHandler: function () {
      this.counter += 1
      this.$emit('increment')
    }
  },
})
new Vue({
  el: '#counter-event-example',
  data: {
    total: 0
  },
  methods: {
    // 5. 局域方法执行了total+1
    incrementTotal: function () {
      this.total += 1
    }
  }
})
</script>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值