【vue笔记系列第四篇】vue.component

一,看代码

<html>
<head>
    <title>Vue.component</title>
</head>
<body>
    <div id="app">
		<app></app>
	</div>
    <!-- <span id='baidu'>跳转</span> -->
    <script type="text/javascript" src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    <script type="text/javascript">

        Vue.component("button-counter", {
		  data: function() {
			return {
			  count: 0
			};
		  },
		  template:
			'<button v-on:click="count++">You clicked me {{ count }} times.</button>'
		});

		Vue.component("app", {
		  data: function() {
			return {
			  count: 0
			};
		  },
		  template:
			'<div> <h1>App Component</h1><button @click="insert">click to insert new Component</button> <div id="appId"> </div></div>',
		  methods: {
			insert() {
			  const component = Vue.component("button-counter");
			  const instance = new component();
			  instance.$mount("#appId");
			}
		  }
		});

		new Vue({
		  el: "#app"
		});
    </script>
</body>
</html>

二,分析

1,栗子

<html>
<head>
    <title>Vue.component</title>
</head>
<body>
    <div id="app">
        <button-counter></button-counter>
    </div>
    <!-- <span id='baidu'>跳转</span> -->
    <script type="text/javascript" src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    <script type="text/javascript">

        Vue.component("button-counter", {
		  data: function() {
			return {
			  count: 0
			};
		  },
		  template:
			'<button v-on:click="count++">You clicked me {{ count }} times.</button>'
		});

		new Vue({
		  el: "#app"
		});
    </script>
</body>
</html>

2,目标控件要放app里面

<div id="app">
    <button-counter></button-counter>
</div>

3,需要new一个vue对象

new Vue({
    el: "#app"
});

4,创建vue.component主体

Vue.component("button-counter", {
	data: function() {
		return {
			count: 0
		};
	},
	template:
		'<button v-on:click="count++">You clicked me {{ count }} times.</button>'
});

 5,效果图

 

三,使用extend

<html>
<head>
    <title>Vue.component</title>
</head>
<body>
    <div id="app">
		<button-counter></button-counter>
	</div>
	
    <!-- <span id='baidu'>跳转</span> -->
    <script type="text/javascript" src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
    <script type="text/javascript">

		var extend = Vue.extend({
		  data: function() {
			return {
			  count: 0
			};
		  },
		  template:
			'<button v-on:click="count++">You clicked me {{ count }} times.</button>'
		});
        Vue.component("button-counter", extend);

		new Vue({
		  el: "#app"
		});
    </script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值