vue04-components

components 组件

//组件
Vue.component('friend-component', {
	props : ['friend'],
	filters:{
		ageInOneYear(age) {
	      return age + 1;
	    },
	    fullName(value) {
	      return `${value.last}, ${value.first}`;
	    }
	},
	methods: {
		incrementAge(friend){
			friend.age =  friend.age + 1;
		},
		decrementAge(friend) {
	      friend.age = friend.age - 1;
	    }
	},
	template : `<div>
		<h2>age: {{friend.age}}</h2>
		<h2>Name: {{friend | fullName}}</h2>
		<button v-on:click="incrementAge(friend)">+</button>
		<input v-model="friend.first"/>
		<input v-model="friend.last"/>
		<button v-on:click="decrementAge(friend)">-</button>
	</div>`
})

const app = new Vue({
	el : '#app',
	data : {
		friends: [
			{
				first : 'bobby',
				last : 'banne',
				age : 25
			},
			{
				first : 'john',
				last : 'Baby',
				age : 25
			}
		]
		
	},
	template : `<div>
		 <friend-component v-for="item in friends" v-bind:friend="item" />
	</div>`
})

posted @ 2018-02-22 00:49  IT-caijw  阅读(121)  评论(0编辑  收藏  举报