概述
一,看代码
<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>
最后
以上就是饱满睫毛膏为你收集整理的【vue笔记系列第四篇】vue.component的全部内容,希望文章能够帮你解决【vue笔记系列第四篇】vue.component所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复