vue組件及路由練習整理

1、定義組件的兩種方式vue

<div id="itany">
		<hello></hello>
		<my-world></my-world>
</div>
<script src="js/vue.js"></script>
<script>
		/**
		 * 方式1:先建立組件構造器,而後由組件構造器建立組件
		 */
		//1.使用Vue.extend()建立一個組件構造器
		var MyComponent=Vue.extend({
			template:'<h3>Hello World</h3>'
		});
		//2.使用Vue.component(標籤名,組件構造器),根據組件構造器來建立組件
		Vue.component('hello',MyComponent);
		
		/**
		 * 方式2:直接建立組件(推薦)
		 */
		// Vue.component('world',{
		Vue.component('my-world',{
			template:'<h1>你好,世界</h1>'
		});

		var vm=new Vue({ //這裏的vm也是一個組件,稱爲根組件Root
			el:'#itany',
			data:{
				msg:'網博'
			}
		});	
	</script>

2、組件的分類vue-router

<div id="itany">
		<my-hello></my-hello>
		<my-world></my-world>
	</div>
<script src="js/vue.js"></script>
<script>
		/**
		 * 全局組件,能夠在全部vue實例中使用
		 */
		Vue.component('my-hello',{
			template:'<h3>{{name}}</h3>',
			data:function(){ //在組件中存儲數據時,必須以函數形式,函數返回一個對象
				return {
					name:'alice'
				}
			}
		});

		/**
		 * 局部組件,只能在當前vue實例中使用
		 */
		var vm=new Vue({
			el:'#itany',
			data:{
				name:'tom'
			},
			components:{ //局部組件
				'my-world':{
					template:'<h3>{{age}}</h3>',
					data(){
						return {
							age:25
						}
					}
				}
			}
		});	
	</script>

3、引用模板數組

<div id="itany">
		<my-hello></my-hello>
		<my-hello></my-hello>
</div>
<template id="wbs">
		<!-- <template>必須有且只有一個根元素 -->
		<div>
			<h3>{{msg}}</h3>
			<ul>
				<li v-for="value in arr">{{value}}</li>
			</ul>
		</div>
</template>
<script src="js/vue.js"></script>
	<script>
		var vm=new Vue({
			el:'#itany',
			components:{
				'my-hello':{
					name:'wbs17022',  //指定組件的名稱,默認爲標籤名,能夠不設置
					template:'#wbs',
					data(){
						return {
							msg:'歡迎來到南京網博',
							arr:['tom','jack','mike']
						}
					}
				}
				
			}
		});	
	</script>

4、動態組件緩存

<div id="itany">
		<button @click="flag='my-hello'">顯示hello組件</button>
		<button @click="flag='my-world'">顯示world組件</button>


		<div>
			<!-- 使用keep-alive組件緩存非活動組件,能夠保留狀態,避免從新渲染,默認每次都會銷燬非活動組件並從新建立 -->
			<keep-alive>
				<component :is="flag"></component>	
			</keep-alive>
		</div>
	</div>
<script src="js/vue.js"></script>
		<script>
		var vm=new Vue({
			el:'#itany',
			data:{
				flag:'my-hello'
			},
			components:{
				'my-hello':{
					template:'<h3>我是hello組件:{{x}}</h3>',
					data(){
						return {
							x:Math.random()
						}
					}
				},
				'my-world':{
					template:'<h3>我是world組件:{{y}}</h3>',
					data(){
						return {
							y:Math.random()
						}
					}
				}
			}
		});	
	</script>

5、父子組件及組件間數據傳遞dom

<div id="itany">
		<my-hello></my-hello>
	</div>
	
	<template id="hello">
		<div>
			<h3>我是hello父組件</h3>
			<h3>訪問本身的數據:{{msg}},{{name}},{{age}},{{user.username}}</h3>
			<h3>訪問子組件的數據:{{sex}},{{height}}</h3>
			<hr>

			<my-world :message="msg" :name="name" :age="age" @e-world="getData"></my-world>
		</div>
	</template>

	<template id="world">
		<div>
			<h4>我是world子組件</h4>
			<h4>訪問父組件中的數據:{{message}},{{name}},{{age}},{{user.username}}</h4>
			<h4>訪問本身的數據:{{sex}},{{height}}</h4>
			<button @click="send">將子組件的數據向上傳遞給父組件</button>
		</div>
	</template>
<script>
		var vm=new Vue({ //根組件
			el:'#itany',
			components:{
				'my-hello':{  //父組件
					methods:{
						getData(sex,height){
							this.sex=sex;
							this.height=height;
						}
					},
					data(){
						return {
							msg:'網博',
							name:'tom',
							age:23,
							user:{id:9527,username:'唐伯虎'},
							sex:'',
							height:''
						}
					},
					template:'#hello',
					components:{
						'my-world':{ //子組件
							data(){
								return {
									sex:'male',
									height:180.5
								}
							},
							template:'#world',
							// props:['message','name','age','user'] //簡單的字符串數組
							props:{ //也能夠是對象,容許配置高級設置,如類型判斷、數據校驗、設置默認值
								message:String,
								name:{
									type:String,
									required:true
								},
								age:{
									type:Number,
									default:18,
									validator:function(value){
										return value>=0;
									}
								},
								user:{
									type:Object,
									default:function(){ //對象或數組的默認值必須使用函數的形式來返回
										return {id:3306,username:'秋香'};
									}
								}
							},
							methods:{
								send(){
									// console.log(this);  //此處的this表示當前子組件實例
									this.$emit('e-world',this.sex,this.height); //使用$emit()觸發一個事件,發送數據
								}
							}
						}
					}
				}
			}
		});	
	</script>

6、單向數據流函數

<div id="itany">
		<h2>父組件:{{name}}</h2>
		<input type="text" v-model="name">
		<h2>父組件:{{user.age}}</h2>

		<hr>

		<my-hello :name.sync="name" :user="user"></my-hello>
	</div>
	
	<template id="hello">
		<div>
			<h3>子組件:{{name}}</h3>
			<h3>子組件:{{user.age}}</h3>
			<button @click="change">修改數據</button>
		</div>
	</template>
<script src="js/vue.js"></script>
	<script>
		var vm=new Vue({ //父組件
			el:'#itany',
			data:{
				name:'tom',
				user:{
					name:'zhangsan',
					age:24
				}
			},
			components:{
				'my-hello':{ //子組件
					template:'#hello',
					props:['name','user'],
					data(){
						return {
							username:this.name //方式1:將數據存入另外一個變量中再操做
						}
					},
					methods:{
						change(){
							// this.username='alice';
							// this.name='alice';
							// this.$emit('update:name','alice'); //方式2:a.使用.sync,須要顯式地觸發一個更新事件
							this.user.age=18;
						}
					}
				}
			}
		});	
	</script>

7、非父子組件間的通訊ui

<div id="itany">
		<my-a></my-a>
		<my-b></my-b>
		<my-c></my-c>
	</div>

	<template id="a">
		<div>
			<h3>A組件:{{name}}</h3>
			<button @click="send">將數據發送給C組件</button>
		</div>
	</template>

	<template id="b">
		<div>
			<h3>B組件:{{age}}</h3>
			<button @click="send">將數組發送給C組件</button>
		</div>
	</template>
	
	<template id="c">
		<div>
			<h3>C組件:{{name}},{{age}}</h3>
		</div>
	</template>
<script src="js/vue.js"></script>
	<script>
		//定義一個空的Vue實例
		var Event=new Vue();

		var A={
			template:'#a',
			data(){
				return {
					name:'tom'
				}
			},
			methods:{
				send(){
					Event.$emit('data-a',this.name);
				}
			}
		}
		var B={
			template:'#b',
			data(){
				return {
					age:20
				}
			},
			methods:{
				send(){
					Event.$emit('data-b',this.age);
				}
			}
		}
		var C={
			template:'#c',
			data(){
				return {
					name:'',
					age:''
				}
			},
			mounted(){ //在模板編譯完成後執行
				Event.$on('data-a',name => {
					this.name=name;
					// console.log(this);
				});

				Event.$on('data-b',age => {
					this.age=age;
				});
			}
		}

		var vm=new Vue({
			el:'#itany',
			components:{
				'my-a':A,
				'my-b':B,
				'my-c':C
			}
		});	
	</script>

8、slot內容分發this

<div id="itany">
		<!-- <my-hello>wbs17022</my-hello> -->
		<my-hello>
			<ul slot="s1">
				<li>aaa</li>
				<li>bbb</li>
				<li>ccc</li>
			</ul>
			<ol slot="s2">
				<li>111</li>
				<li>222</li>
				<li>333</li>
			</ol>
		</my-hello>
	</div>

	<template id="hello">
		<div>
			<slot name="s2"></slot>
			<h3>welcome to itany</h3>
			<!-- <slot>若是沒有原內容,則顯示該內容</slot> -->
			<slot name="s1"></slot>
		</div>
	</template>
<script src="js/vue.js"></script>
	<script>

		var vm=new Vue({
			el:'#itany',
			components:{
				'my-hello':{
					template:'#hello'
				}
			}
		});	
	</script>

9、路由基本用法url

<style>
		/* .router-link-active{
			font-size:20px;
			color:#ff7300;
			text-decoration:none;
		} */
		.active{
			font-size:20px;
			color:#ff7300;
			text-decoration:none;
		}
</style>
<div id="itany">
		<div>
			<!-- 使用router-link組件來定義導航,to屬性指定連接url -->
			<router-link to="/home">主頁</router-link>
			<router-link to="/news">新聞</router-link>
		</div>
		<div>
			<!-- router-view用來顯示路由內容 -->
			<router-view></router-view>
		</div>
	</div>
<script src="js/vue.js"></script>
	<script src="js/vue-router.js"></script>
	<script>
		//1.定義組件
		var Home={
			template:'<h3>我是主頁</h3>'
		}
		var News={
			template:'<h3>我是新聞</h3>'
		}

		//2.配置路由
		const routes=[
			{path:'/home',component:Home},
			{path:'/news',component:News},
			{path:'*',redirect:'/home'} //重定向
		]

		//3.建立路由實例
		const router=new VueRouter({
			routes, //簡寫,至關於routes:routes
			// mode:'history', //更改模式
			linkActiveClass:'active' //更新活動連接的class類名
		});

		//4.建立根實例並將路由掛載到Vue實例上
		new Vue({
			el:'#itany',
			router //注入路由
		});
	</script>

10、路由嵌套和參數傳遞code

<div id="itany">
		<div>
			<router-link to="/home">主頁</router-link>
			<router-link to="/user">用戶</router-link>
		</div>
		<div>
			<transition enter-active-class="animated bounceInLeft" leave-active-class="animated bounceOutRight">
				<router-view></router-view>
			</transition>
		</div>

		<hr>
		<button @click="push">添加路由</button>
		<button @click="replace">替換路由</button>
	</div>

	<template id="user">
		<div>
			<h3>用戶信息</h3>
			<ul>
				<router-link to="/user/login?name=tom&pwd=123" tag="li">用戶登錄</router-link>
				<router-link to="/user/regist/alice/456" tag="li">用戶註冊</router-link>
			</ul>
			<router-view></router-view>
		</div>
	</template>
<script src="js/vue.js"></script>
	<script src="js/vue-router.js"></script>
	<script>
		var Home={
			template:'<h3>我是主頁</h3>'
		}
		var User={
			template:'#user'
		}
		var Login={
			template:'<h4>用戶登錄。。。獲取參數:{{$route.query}},{{$route.path}}</h4>'
		}
		var Regist={
			template:'<h4>用戶註冊。。。獲取參數:{{$route.params}},{{$route.path}}</h4>'
		}

		const routes=[
			{
				path:'/home',
				component:Home
			},
			{
				path:'/user',
				component:User,
				children:[
					{
						path:'login',
						component:Login
					},
					{
						path:'regist/:username/:password',
						component:Regist
					}
				]
			},
			{
				path:'*',
				redirect:'/home'
			}
		]

		const router=new VueRouter({
			routes, //簡寫,至關於routes:routes
			linkActiveClass:'active' //更新活動連接的class類名
		});

		new Vue({
			el:'#itany',
			router, //注入路由
			methods:{
				push(){
					router.push({path:'home'}); //添加路由,切換路由
				},
				replace(){
					router.replace({path:'user'}); //替換路由,沒有歷史記錄
				}
			}
		});
	</script>
相關文章
相關標籤/搜索