zoukankan      html  css  js  c++  java
  • vue的组件

    1、组件

    <!DOCTYPE html>
    <html>
    <head>
    	<title>组件</title>
    
    </head>
    <body>
    
    	<div id="app">
    		<my-component></my-component>
    
    		<my-component1></my-component1>
    	</div>
    
    	<div id="app1">
    		<my-component></my-component>
    		<jubuzujian></jubuzujian>
    
    		<jubuzujian2></jubuzujian2>
    
    	</div>
    
    
    	<!-- 可以把组件中的template的字符串写在这里,叫做模板字符串,如果字符串比较庞大,则可以使用这样的方式定义template -->
    	<script type="text/x-Template" id="tpl1">
    			<tr><td>1</td><td>2</td><td>3</td></tr>
    
    
    	</script>
    	<script type="text/javascript" src="vue.js"></script>
    	<script type="text/javascript">
    
    		// 注册全局组件,任何一个vue实例都可以使用,作用域是全局
    		Vue.component("my-component",{
    			template:'<div>{{mes}}<input type="button" value="tanchu" v-on:click="tanchu"></div>',
    			data:function(){
    				return {
    					mes:"hello component"
    				}
    			},
    			methods:{
    				tanchu:function(){
    					alert(123)
    				}
    			}
    		})
    
    
    		// 初始化根实例
    		var app = new Vue({
    			el:"#app",
    			data:{
    			},
    			// 局部注册组件的第一种方法
    			components:{
    				"my-component1":{
    				 template:'<div>{{mes}}<input type="button" value="jubuzujian" v-on:click="tanchu1"></div>',
    				 data:function(){
    					return {
    						mes:"hello jubuzujian"
    						}
    					},
    			methods:{
    				tanchu1:function(){
    					alert(123)
    						}
    					}
    				}	
    			}
    		})
    
    
    		// 局部注册组件的第二种方法
    		var zujian = {
    				 template:'<div>{{mes}}<input type="button" value="oooo" v-on:click="tanchu1"></div>',
    
    				 // data必须是一个函数
    				 data:function(){
    					return {
    						mes:"hello jubuzujian"
    						}
    					},
    				methods:{
    				tanchu1:function(){
    					alert(123)
    						}
    					}
    		}
    
    
    		var zujiantpl = {
    				// 这里使用dom的选择器选择就可以了
    				 template:'#tpl1',
    				 data:function(){
    					return {
    						mes:"hello jubuzujian"
    						}
    					},
    				methods:{
    				tanchu1:function(){
    					alert(123)
    						}
    					}
    		}
    
    
    		var app1 = new Vue({
    			el:"#app1",
    			data:{
    			},
    			components:{
    				"jubuzujian":zujian,
    				"jubuzujian2":zujiantpl
    			}
    		})
    			
    	</script>
    </body>
    </html>
    	
    

      

    2、组件之间的通信

    <!DOCTYPE html>
    <html>
    <head>
    	<title>组件通信</title>
    
    </head>
    <body>
    
    	<div id="app">
    		<!-- 静态传参 -->
    		<my-new-tpl1 mes="hello vue1"></my-new-tpl1>
    
    		<!-- 动态传参 -->
    		<my-new-tpl1 v-bind:mes="mes"></my-new-tpl1>
    
    		<my-new-tpl1 mes="hello vue3" v-on:jieshou="jieshoufunc"></my-new-tpl1>
    	</div>
    
    	<script type="text/x-Template" id="id1">
    		<input type="button" value="tanchu" v-on:click="tanchu1">
    
    
    	</script>
    	<script type="text/javascript" src="vue.js"></script>
    	<script type="text/javascript">
    
    
    		var mytpl = {
    				 template:'#id1',
    				 // 接受一个静态属性
    				 props:["mes"],
    				 // data必须是一个函数
    				 data:function(){
    					return {
    						mes:"hello jubuzujian"
    						}
    					},
    				methods:{
    				tanchu1:function(){
    					alert(this.mes);
    					this.$emit("jieshou")
    						}
    					}
    				}
    
    		var app = new Vue({
    			el:"#app",
    			data:{
    				mes:"zhen TM nan"
    			},
    			components:{
    				"my-new-tpl1":mytpl
    			},
    			methods:{
    				jieshoufunc:function(){
    					alert("123.....")
    				}
    			}
    		})
    			
    	</script>
    </body>
    </html>
    

      

  • 相关阅读:
    搭建自己的博客(四):优化首页和详情页
    搭建自己的博客(三):简单搭建首页和详情页
    搭建自己的博客(二):创建表,创建超级用户
    搭建自己的博客(一):前期准备
    linux系列(五):rm命令
    linux系列(四):mkdir命令
    linux系列(三):pwd命令
    Statically Linking freeglut
    ffmpeg 版本升级到 4.0 增加 libaom 库 [AOMedia 的 AV1 视频编码格式]
    FFmpeg configure: rename cuda to ffnvcodec 2018-03-06
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/10604848.html
Copyright © 2011-2022 走看看