最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

全局組件和局部組件寫法

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

全局組件引入寫法

在項目的main.js中:

import Vue from 'vue';
import MyComponent from '@/components/MyComponent.vue'; // 導(dǎo)入自己寫的組件文件
?
Vue.use(MyComponent); // 自定義全局組件的時候需要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: { // 組件的計算方法
? ? 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ò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òu)造器
			var myComponent = Vue.extend({
				template:'<div> 局部組件4</div>'
			})
			//注冊組件 并指定組件的標簽 逐漸的html標簽為 my-compoent
			Vue.component('my-compoent',myComponent)
			
			new Vue({
				el:'#example4',
				components:{
					'my-component':myComponent
				}
			})
		</script>
		
		<!-- 父子組件 數(shù)據(jù)傳遞
			
			父子組件的關(guān)系:通常組件A在它的模板中使用組件B,此時組件A為父組件,組件B為子組件。父子組件應(yīng)該解耦,
			組件實例的作用域是孤立的,子組件中不能直接使用父組件的數(shù)據(jù)。應(yīng)該使用props傳遞父組件到子組件的數(shù)據(jù),
			子組件通過events給父組件發(fā)消息,以此實現(xiàn)父子組件間的通信。 
			如上,在其他組件內(nèi)部用components聲明組件,即為局部注冊。在Vue實例中用components注冊組件時,
			可以理解為Vue實例為一個大的父組件,其他任何注冊的組件都是子組件。所以在注冊組件時,
			如果要使用Vue實例data中的數(shù)據(jù),都要用props傳遞Vue實例中的數(shù)據(jù),讓Vue實例的數(shù)據(jù)在組件中可用。 
			還可以用v-bind動態(tài)綁定props的值到父組件的數(shù)據(jù),父組件數(shù)據(jù)發(fā)生變化時,子組件的數(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>

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

相關(guān)文章

  • Vue項目中缺少明顯入口文件的原因及解決策略

    Vue項目中缺少明顯入口文件的原因及解決策略

    本文探討了Vue項目中缺少明顯入口文件的原因,并提供了解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-11-11
  • Vue Steps步驟條渲染異常問題

    Vue Steps步驟條渲染異常問題

    這篇文章主要介紹了Vue Steps步驟條渲染異常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 詳解vue-router傳參的兩種方式

    詳解vue-router傳參的兩種方式

    Vue Router 是 Vue.js 官方的路由管理器。這篇文章主要介紹了詳解vue-router傳參的兩種方式,需要的朋友可以參考下
    2018-09-09
  • Vue2.0 $set()的正確使用詳解

    Vue2.0 $set()的正確使用詳解

    這篇文章主要介紹了Vue2.0 $set()的正確使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue 表單控件綁定的實現(xiàn)示例

    Vue 表單控件綁定的實現(xiàn)示例

    本篇文章主要介紹了Vue 表單控件綁定的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • vue使用element-ui的el-date-picker設(shè)置樣式無效的解決

    vue使用element-ui的el-date-picker設(shè)置樣式無效的解決

    本文主要介紹了vue使用element-ui的el-date-picker設(shè)置樣式無效的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • 詳解Vue-cli 創(chuàng)建的項目如何跨域請求

    詳解Vue-cli 創(chuàng)建的項目如何跨域請求

    本篇文章主要介紹了詳解Vue-cli 創(chuàng)建的項目如何跨域請求 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • vue使用canvas的教程詳解

    vue使用canvas的教程詳解

    Vue.js?是一個流行的?JavaScript?框架,用于構(gòu)建用戶界面,它提供了一種簡潔的方式來管理和渲染數(shù)據(jù),同時也支持與其他庫和工具的集成,本文主要來和大家聊聊如何在vue中使用canvas,有需要的可以參考下
    2023-09-09
  • vue中路由router配置步驟詳解

    vue中路由router配置步驟詳解

    vue的主要思想是組件化開發(fā),路由用來配置組件對應(yīng)展示路徑,本文給大家介紹vue中路由router配置步驟,創(chuàng)建路由文件——使用路由——配置路由出口,使路由配置內(nèi)容展示在頁面上,感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • vue input輸入框模糊查詢的示例代碼

    vue input輸入框模糊查詢的示例代碼

    本篇文章主要介紹了vue input輸入框模糊查詢的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05

最新評論

耒阳市| 新乡县| 沅江市| 双流县| 凌云县| 贵港市| 东台市| 泰安市| 新龙县| 普宁市| 天峨县| 临朐县| 大庆市| 虹口区| 义乌市| 舞阳县| 沁阳市| 长海县| 浮梁县| 小金县| 临湘市| 阿鲁科尔沁旗| 安多县| 广州市| 河北省| 玛沁县| 富阳市| 秭归县| 恩平市| 休宁县| 文登市| 溧水县| 巨鹿县| 兰考县| 苍南县| 元氏县| 曲靖市| 黎城县| 开鲁县| 和静县| 密云县|