当前位置: 代码迷 >> 综合 >> vue2.0 全局组件和局部组件
  详细解决方案

vue2.0 全局组件和局部组件

热度:74   发布时间:2024-01-10 19:30:16.0

全局组件主要是component放外面,全局使用。局部组件主要是放Vue实例对象里,只能这个Vue元素内使用。

组件通过extend创建。

注意:exntend组件内的data是方法、方法、方法

 

全局创建组件全局调用:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title>
</head>
<body><div id="demo"><aaa></aaa></div><script src="//cdn.bootcss.com/vue/2.1.10/vue.js"></script><script>// 创建组件var A=Vue.extend({template:"<h3 @click='change'>{
   {msg}}</h3>",data:function(){return {msg:"i am btn"}},methods:{change:function(){this.msg="i am not a btn"}}})// 全局调用组件Vue.component('aaa',A);new Vue({el:"#demo"})</script>
</body>
</html>

或省略extend:

 

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style></style>
</head>
<body><div id="demo"><aaa></aaa></div><script src="//cdn.bootcss.com/vue/2.1.10/vue.js"></script><script>// 全局组件Vue.component('aaa',{template:"<h3 @click='change'>{
   {msg}}</h3>",data:function(){return {msg:"i am btn"}},methods:{change:function(){this.msg="i am not a btn"}}});new Vue({el:"#demo"})</script>
</body>
</html>

 

全局创建组件局部调用:

 

 

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title>
</head>
<body><div id="demo"><aaa></aaa></div>
<script src="//cdn.bootcss.com/vue/2.1.10/vue.js"></script><script>// 创建组件var A=Vue.extend({template:"<h3 @click='change'>{
   {msg}}</h3>",data:function(){return {msg:"i am btn"}},methods:{change:function(){this.msg="i am not a btn"}}})new Vue({el:"#demo",components:{// 局部调用组件aaa:A}})</script>
</body>
</html>

或省略extend:

 

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style></style>
</head>
<body><div id="demo"><aaa></aaa></div><script src="//cdn.bootcss.com/vue/2.1.10/vue.js"></script><script>new Vue({el:"#demo",components:{// 局部组件aaa:{template:"<h3 @click='change'>{
   {msg}}</h3>",data:function(){return {msg:"i am btn"}},methods:{change:function(){this.msg="i am not a btn"}}}}})</script>
</body>
</html>

 

 

最后再来个动态组件。。。:

 

 

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Document</title><style></style>
</head>
<body><div id="demo"><button @click="a='aaa'">aaa</button><button @click="a='bbb'">bbb</button><component :is="a"></component></div><script src="//cdn.bootcss.com/vue/2.1.10/vue.js"></script><script>new Vue({el:"#demo",data:{a:'aaa'},components:{'aaa':{template:"<h3>我是aaa</h3>"},'bbb':{template:"<h3>我是bbb</h3>"}}})</script>
</body>
</html>

 

2.0以后其实使用extend很少了,基本不用!

 

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <template id="aaa"> <div> <h3>abc</h3> <h4>def</h4> <h5>xyz</h5> </div> </template> <div id="box"> <aaa></aaa> {
   {msg}} </div> <script src="//cdn.bootcss.com/vue/2.2.1/vue.common.js"></script> <script> var Home={ template:'#aaa' } Vue.component('my-aaa',Home); window.onload=function(){ new Vue({ el:"#box", data:{ msg:"welcome vue2.0" }, components:{ 'aaa':Home } }) } </script> </body> </html>