亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue全局組件和局部組件的寫法介紹

 更新時(shí)間:2022年03月29日 09:08:38   作者:ywltoread  
這篇文章主要介紹了vue全局組件和局部組件的寫法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

全局組件和局部組件寫法

vue組件有兩種,一種是全局組件,一種是局部組件。整個(gè)項(xiàng)目經(jīng)常用到的用全局寫法,用到比較少的專供特定頁面用的使用局部組件。

全局組件引入寫法

在項(xiàng)目的main.js中:

import Vue from 'vue';
import MyComponent from '@/components/MyComponent.vue'; // 導(dǎo)入自己寫的組件文件
?
Vue.use(MyComponent); // 自定義全局組件的時(shí)候需要Vue.use();
Vue.component('my-component', MyComponent); //初始化組件
?
new Vue({
? el: '#app',
? router,
? components: {
? ? App,
? ? MyComponent
? },
? template: '<App/>',
});?

局部組件引入寫法

在需要使用組件的a.vue文件中:

<template>
</template>
?
<script>
import MyComponent from '@/components/MyComponent.vue';
export default {
? components: {MyComponent}, // 直接初始化就可以啦,不需要Vue.use();
? data() {},
? methods: {}
};
</script>
?
<style lang="scss" scoped>
</style>?

下面附上自定義組件的MyComponent.vue文件代碼:

<template>
? <div>
? ? <a @click="methods1()"></a>
? </div>
</template>
<script>
import { MessageBox } from 'mint-ui';
export default {
? data () { // 組件內(nèi)參數(shù)定義在data中
? ? return {
? ? ? data1: {}
? ? };
? },
? props: { // 組件傳參使用props
? ? value1: String,
? ? value2: Number
? },
? methods: { // 組件的計(jì)算方法
? ? methods1 () {
? ? }
? }
};
</script>
<style lang="scss" scoped>
</style>

vue全局/局部組件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/vue.js"></script>
	</head>
	<body>
		
		<!-- 全局組件簡寫-->
	    <div id="example1">
			<my-compoent></my-compoent>
		</div>
		<script>
			Vue.component('my-compoent',{
				template:'<div>測試1</div>'
			})
			
			new Vue({
				el:'#example1'
			})
		</script>
		
		
		<!-- 注冊全局組件-->
		<div id="example2">
			<my-compoent1></my-compoent1>
		</div>
		<script>
			//創(chuàng)建一個(gè)組件構(gòu)造器
			var myComponent = Vue.extend({
				template:'<div> 測試2</div>'
			})
			//注冊全局組件: (z組件名稱 組件構(gòu)造器)
			//Vue.component(tagName,options)
			Vue.component('my-compoent1',myComponent)
			
			new Vue({
				el:'#example2'
			})
		</script>
		
		
		<!-- 注冊局部組件-->
		<div id="example3">
			<my-compoent></my-compoent>
		</div>
		<div id="example4">
			<my-compoent></my-compoent>
		</div>
		<script>
			//創(chuàng)建一個(gè)組件構(gòu)造器
			var myComponent = Vue.extend({
				template:'<div> 局部組件4</div>'
			})
			//注冊組件 并指定組件的標(biāo)簽 逐漸的html標(biāo)簽為 my-compoent
			Vue.component('my-compoent',myComponent)
			
			new Vue({
				el:'#example4',
				components:{
					'my-component':myComponent
				}
			})
		</script>
		
		<!-- 父子組件 數(shù)據(jù)傳遞
			
			父子組件的關(guān)系:通常組件A在它的模板中使用組件B,此時(shí)組件A為父組件,組件B為子組件。父子組件應(yīng)該解耦,
			組件實(shí)例的作用域是孤立的,子組件中不能直接使用父組件的數(shù)據(jù)。應(yīng)該使用props傳遞父組件到子組件的數(shù)據(jù),
			子組件通過events給父組件發(fā)消息,以此實(shí)現(xiàn)父子組件間的通信。 
			如上,在其他組件內(nèi)部用components聲明組件,即為局部注冊。在Vue實(shí)例中用components注冊組件時(shí),
			可以理解為Vue實(shí)例為一個(gè)大的父組件,其他任何注冊的組件都是子組件。所以在注冊組件時(shí),
			如果要使用Vue實(shí)例data中的數(shù)據(jù),都要用props傳遞Vue實(shí)例中的數(shù)據(jù),讓Vue實(shí)例的數(shù)據(jù)在組件中可用。 
			還可以用v-bind動(dòng)態(tài)綁定props的值到父組件的數(shù)據(jù),父組件數(shù)據(jù)發(fā)生變化時(shí),子組件的數(shù)據(jù)也相應(yīng)的變化。
			
			父--》子:父組件通過props傳遞父組件到子組件
			子--》父:子組件通過events給父組件發(fā)消息
		-->
		<div id="test">
		    <template id="testProp">
		        <ul>
		            <li v-for="book in books">
		                <p>{{book.title}}</p>
		                <p>{{book.desc}}</p>
		                <p>{{book.author}}</p>
		            </li>
		        </ul>
		    <template>
		    <test-prop :book-list = "books"></test-prop>
		</div>
		<script>
			var TestProp = Vue.extend({
			    template:'#testProp',
			    props: ['book-list']
			});
			var test = new Vue({
			    el: '#test',
			    data: function(){
			        return {
			            books: [
			                {
			                    title: 'title1',
			                    desc: 'desc1',
			                    author: 'author1'
			                },
			                {
			                    title: 'title2',
			                    desc: 'desc2',
			                    author: 'author2'
			                },
			                {
			                    title: 'title3',
			                    desc: 'desc3',
			                    author: 'author3'
			                },
			            ],
			        }
			    },
			    components:{
			        'test-prop': TestProp,
			    },
			});
		</script>
	</body>
</html>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • vue使用axios?post發(fā)送json數(shù)據(jù)跨域請求403的解決方案

    vue使用axios?post發(fā)送json數(shù)據(jù)跨域請求403的解決方案

    這篇文章主要介紹了vue使用axios?post發(fā)送json數(shù)據(jù)跨域請求403的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能

    vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能

    這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目中canvas實(shí)現(xiàn)截圖功能,截取圖片的一部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法

    vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法

    defineExpose是Vue3中新增的選項(xiàng),用于向父組件暴露子組件內(nèi)部的屬性和方法,通過defineExpose,子組件可以主動(dòng)控制哪些屬性和方法可以被父組件訪問,本文主要介紹了vue3通過ref獲取子組件defineExpose的數(shù)據(jù)和方法,需要的朋友可以參考下
    2023-10-10
  • vue如何使用rem適配

    vue如何使用rem適配

    這篇文章主要介紹了vue如何使用rem適配,幫助大家處理vue開發(fā)移動(dòng)應(yīng)用時(shí)的兼容性問題,感興趣的朋友可以了解下
    2021-02-02
  • vue3實(shí)現(xiàn)無縫滾動(dòng)組件的示例代碼

    vue3實(shí)現(xiàn)無縫滾動(dòng)組件的示例代碼

    在日常開發(fā)中,經(jīng)常遇到需要支持列表循環(huán)滾動(dòng)展示,特別是在數(shù)據(jù)化大屏開發(fā)中,所以小編今天為大家介紹一下如何利用vue3實(shí)現(xiàn)一個(gè)無縫滾動(dòng)組件吧
    2023-09-09
  • 解讀vant的Uploader上傳問題

    解讀vant的Uploader上傳問題

    這篇文章主要介紹了解讀vant的Uploader上傳問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 解決運(yùn)行vue項(xiàng)目內(nèi)存溢出問題

    解決運(yùn)行vue項(xiàng)目內(nèi)存溢出問題

    這篇文章主要介紹了解決運(yùn)行vue項(xiàng)目內(nèi)存溢出問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue.js高德地圖實(shí)現(xiàn)熱點(diǎn)圖代碼實(shí)例

    vue.js高德地圖實(shí)現(xiàn)熱點(diǎn)圖代碼實(shí)例

    這篇文章主要介紹了vue.js高德地圖實(shí)現(xiàn)熱點(diǎn)圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue3使用echarts并封裝echarts組件方式

    vue3使用echarts并封裝echarts組件方式

    這篇文章主要介紹了vue3使用echarts并封裝echarts組件方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue實(shí)現(xiàn)移動(dòng)端返回頂部

    vue實(shí)現(xiàn)移動(dòng)端返回頂部

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)移動(dòng)端返回頂部,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10

最新評(píng)論