欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中組件傳參的幾種常用方法舉例

 更新時(shí)間:2023年08月23日 09:23:37   作者:尋源千鶴  
這篇文章主要給大家介紹了關(guān)于vue中組件傳參的幾種常用方法,Vue組件傳參方也是面試最??嫉膬?nèi)容,文中通過代碼實(shí)例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.父子組件

  • Props:通過在父組件中定義props屬性,將數(shù)據(jù)傳遞給子組件。子組件通過props屬性接收數(shù)據(jù)。例如:
// 父組件
<template>
  <child-component :message="hello"></child-component>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      hello: 'Hello World!'
    }
  }
}
</script>
// 子組件
<template>
  <div>{{ message }}</div>
</template>
<script>
export default {
  props: {
    message: String
  }
}
</script>
  • $emit:通過在子組件中觸發(fā)事件,將數(shù)據(jù)傳遞給父組件。父組件通過在子組件上監(jiān)聽事件,接收數(shù)據(jù)。例如:
// 子組件
<template>
  <button @click="sendMessage">Send Message</button>
</template>
<script>
export default {
  methods: {
    sendMessage() {
      this.$emit('message-sent', 'Hello World!')
    }
  }
}
</script>
// 父組件
<template>
  <child-component @message-sent="handleMessage"></child-component>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  methods: {
    handleMessage(message) {
      console.log(message) // 'Hello World!'
    }
  }
}
</script>
  • Provide/Inject:通過在父組件中提供數(shù)據(jù),讓子孫組件可以注入數(shù)據(jù)。例如:
// 父組件
<template>
  <div>
    <provide-message :message="hello">
      <child-component></child-component>
    </provide-message>
  </div>
</template>
<script>
import ProvideMessage from './ProvideMessage.vue'
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ProvideMessage,
    ChildComponent
  },
  data() {
    return {
      hello: 'Hello World!'
    }
  }
}
</script>
// ProvideMessage組件
<template>
  <div>
    <slot></slot>
  </div>
</template>
<script>
export default {
  provide() {
    return {
      message: this.message
    }
  },
  props: {
    message: String
  }
}
</script>
// 子組件
<template>
  <div>{{ message }}</div>
</template>
<script>
export default {
  inject: ['message']
}
</script>

以上是Vue中組件傳參的三種常用方法,分別是Props、$emit和Provide/Inject。通過這些方法,我們可以在組件之間傳遞數(shù)據(jù),實(shí)現(xiàn)組件之間的通信。

2.兄弟組件

  • 通過共同的父組件傳遞數(shù)據(jù):如果兩個(gè)兄弟組件有共同的父組件,可以通過在父組件中定義數(shù)據(jù),然后通過props屬性分別傳遞給兩個(gè)兄弟組件。例如:
// 父組件
<template>
  <div>
    <child-component-1 :message="hello"></child-component-1>
    <child-component-2 :message="world"></child-component-2>
  </div>
</template>
<script>
import ChildComponent1 from './ChildComponent1.vue'
import ChildComponent2 from './ChildComponent2.vue'
export default {
  components: {
    ChildComponent1,
    ChildComponent2
  },
  data() {
    return {
      hello: 'Hello',
      world: 'World'
    }
  }
}
</script>
// ChildComponent1組件
<template>
  <div>{{ message }}!</div>
</template>
<script>
export default {
  props: {
    message: String
  }
}
</script>
// ChildComponent2組件
<template>
  <div>{{ message }}!</div>
</template>
<script>
export default {
  props: {
    message: String
  }
}
</script>
  • 通過事件總線傳遞數(shù)據(jù):可以在Vue實(shí)例中創(chuàng)建一個(gè)事件總線,然后在兄弟組件中分別觸發(fā)和監(jiān)聽事件,從而實(shí)現(xiàn)數(shù)據(jù)傳遞。例如:
// Vue實(shí)例
<script>
export default {
  data() {
    return {
      message: ''
    }
  },
  created() {
    this.$on('message-sent', (message) => {
      this.message = message
    })
  }
}
</script>
// ChildComponent1組件
<template>
  <button @click="sendMessage">Send Message</button>
</template>
<script>
export default {
  methods: {
    sendMessage() {
      this.$root.$emit('message-sent', 'Hello')
    }
  }
}
</script>
// ChildComponent2組件
<template>
  <div>{{ message }} World!</div>
</template>
<script>
export default {
  computed: {
    message() {
      return this.$root.message
    }
  }
}
</script>
  • 通過Vuex狀態(tài)管理傳遞數(shù)據(jù):可以使用Vuex來管理應(yīng)用程序的狀態(tài),從而實(shí)現(xiàn)兄弟組件之間的數(shù)據(jù)傳遞。例如:
// Vuex store
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    message: ''
  },
  mutations: {
    setMessage(state, message) {
      state.message = message
    }
  }
})
// ChildComponent1組件
<template>
  <button @click="sendMessage">Send Message</button>
</template>
<script>
export default {
  methods: {
    sendMessage() {
      this.$store.commit('setMessage', 'Hello')
    }
  }
}
</script>
// ChildComponent2組件
<template>
  <div>{{ message }} World!</div>
</template>
<script>
export default {
  computed: {
    message() {
      return this.$store.state.message
    }
  }
}
</script>

以上是Vue中兄弟組件傳參的三種常用方法,分別是通過共同的父組件傳遞數(shù)據(jù)、通過事件總線傳遞數(shù)據(jù)和通過Vuex狀態(tài)管理傳遞數(shù)據(jù)。根據(jù)具體的場景和需求,選擇合適的方法來實(shí)現(xiàn)兄弟組件之間的數(shù)據(jù)傳遞。

3.祖先后代

  • 通過props和 e m i t 傳遞數(shù)據(jù):祖先組件可以通過 p r o p s 屬性將數(shù)據(jù)傳遞給中間組件,中間組件再通過 p r o p s 屬性將數(shù)據(jù)傳遞給后代組件。后代組件可以通過 emit傳遞數(shù)據(jù):祖先組件可以通過props屬性將數(shù)據(jù)傳遞給中間組件,中間組件再通過props屬性將數(shù)據(jù)傳遞給后代組件。后代組件可以通過 emit傳遞數(shù)據(jù):祖先組件可以通過props屬性將數(shù)據(jù)傳遞給中間組件,中間組件再通過props屬性將數(shù)據(jù)傳遞給后代組件。后代組件可以通過emit事件將數(shù)據(jù)傳遞回祖先組件。例如:
// 祖先組件
<template>
  <div>
    <middle-component :message="hello"></middle-component>
  </div>
</template>
<script>
import MiddleComponent from './MiddleComponent.vue'
export default {
  components: {
    MiddleComponent
  },
  data() {
    return {
      hello: 'Hello'
    }
  }
}
</script>
// 中間組件
<template>
  <div>
    <child-component :message="message" @message-sent="sendMessage"></child-component>
  </div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  props: {
    message: String
  },
  methods: {
    sendMessage(message) {
      this.$emit('message-sent', message)
    }
  }
}
</script>
// 后代組件
<template>
  <div>{{ message }} World!</div>
</template>
<script>
export default {
  props: {
    message: String
  }
}
</script>
  • 通過provide和inject傳遞數(shù)據(jù):祖先組件可以通過provide方法提供數(shù)據(jù),后代組件可以通過inject方法注入數(shù)據(jù)。例如:
// 祖先組件
<template>
  <div>
    <middle-component>
      <child-component></child-component>
    </middle-component>
  </div>
</template>
<script>
import MiddleComponent from './MiddleComponent.vue'
export default {
  components: {
    MiddleComponent
  },
  provide() {
    return {
      message: 'Hello'
    }
  }
}
</script>
// 中間組件
<template>
  <div>
    <slot></slot>
  </div>
</template>
// 后代組件
<template>
  <div>{{ message }} World!</div>
</template>
<script>
export default {
  inject: ['message']
}
</script>
  • 通過Vuex狀態(tài)管理傳遞數(shù)據(jù):祖先組件和后代組件都可以通過Vuex來管理應(yīng)用程序的狀態(tài),從而實(shí)現(xiàn)數(shù)據(jù)傳遞。例如:
// Vuex store
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    message: 'Hello'
  },
  mutations: {
    setMessage(state, message) {
      state.message = message
    }
  }
})
// 祖先組件
<template>
  <div>
    <middle-component>
      <child-component></child-component>
    </middle-component>
  </div>
</template>
<script>
import MiddleComponent from './MiddleComponent.vue'
export default {
  components: {
    MiddleComponent
  }
}
</script>
// 中間組件
<template>
  <div>
    <slot></slot>
  </div>
</template>
// 后代組件
<template>
  <div>{{ message }} World!</div>
</template>
<script>
export default {
  computed: {
    message() {
      return this.$store.state.message
    }
  }
}
</script>

以上是Vue中祖先組件和后代組件傳遞數(shù)據(jù)的三種常用方法,分別是通過props和$emit傳遞數(shù)據(jù)、通過provide和inject傳遞數(shù)據(jù)和通過Vuex狀態(tài)管理傳遞數(shù)據(jù)。根據(jù)具體的場景和需求,選擇合適的方法來實(shí)現(xiàn)祖先組件和后代組件之間的數(shù)據(jù)傳遞。

4.參數(shù)傳遞和插槽之間的聯(lián)系

在Vue中,傳參和插槽是兩個(gè)不同的概念,但它們之間有一定的關(guān)系。

傳參是指在組件之間傳遞數(shù)據(jù),可以通過props和$emit、provide和inject、Vuex等方式實(shí)現(xiàn)。傳參的目的是讓組件之間可以共享數(shù)據(jù),從而實(shí)現(xiàn)組件之間的通信。

插槽是指在組件中定義一個(gè)或多個(gè)插槽,然后在使用該組件時(shí),可以在插槽中插入任意內(nèi)容。插槽的目的是讓組件更加靈活,可以根據(jù)使用場景動態(tài)地插入不同的內(nèi)容。

傳參和插槽之間的關(guān)系在于,傳參可以用來控制插槽中的內(nèi)容。例如,可以通過props屬性將數(shù)據(jù)傳遞給子組件,在子組件中使用插槽來展示這些數(shù)據(jù)。又例如,可以通過$emit事件將子組件中的數(shù)據(jù)傳遞給父組件,在父組件中使用插槽來展示這些數(shù)據(jù)。

下面是一個(gè)示例,演示了如何通過傳參和插槽來控制組件中的內(nèi)容:

// 父組件
<template>
  <div>
    <child-component :message="hello">
      <template #default="{ message }">
        <div>{{ message }} World!</div>
      </template>
    </child-component>
  </div>
</template>
<script>
import ChildComponent from './ChildComponent.vue'
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      hello: 'Hello'
    }
  }
}
</script>
// 子組件
<template>
  <div>
    <slot :message="message"></slot>
  </div>
</template>
<script>
export default {
  props: {
    message: String
  },
  data() {
    return {
      message: this.message
    }
  }
}
</script>

在上面的示例中,父組件通過props屬性將數(shù)據(jù)傳遞給子組件,子組件通過插槽將數(shù)據(jù)傳遞給父組件。具體來說,父組件將數(shù)據(jù)hello傳遞給子組件,子組件將數(shù)據(jù)message傳遞給插槽,父組件通過插槽接收數(shù)據(jù)并展示在頁面上。

總之,傳參和插槽是兩個(gè)不同的概念,但它們之間有一定的關(guān)系。傳參可以用來控制插槽中的內(nèi)容,從而實(shí)現(xiàn)組件之間的通信和動態(tài)渲染。

總結(jié)

到此這篇關(guān)于vue中組件傳參的幾種常用方法的文章就介紹到這了,更多相關(guān)vue組件傳參內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解vue-cli項(xiàng)目在IE瀏覽器打開報(bào)錯(cuò)解決方法

    詳解vue-cli項(xiàng)目在IE瀏覽器打開報(bào)錯(cuò)解決方法

    這篇文章主要介紹了詳解vue-cli項(xiàng)目在IE瀏覽器打開報(bào)錯(cuò)解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • vue中使用axios post上傳頭像/圖片并實(shí)時(shí)顯示到頁面的方法

    vue中使用axios post上傳頭像/圖片并實(shí)時(shí)顯示到頁面的方法

    今天小編就為大家分享一篇vue中使用axios post上傳頭像/圖片并實(shí)時(shí)顯示到頁面的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue3中Element-Plus分頁(Pagination)組件的使用

    Vue3中Element-Plus分頁(Pagination)組件的使用

    Element-Plus分頁(Pagination)組件在開發(fā)過程中數(shù)據(jù)展示會經(jīng)常使用到,同時(shí)分頁功能也會添加到頁面中,下面我們就來學(xué)習(xí)一下它的具體使用,需要的可以參考一下
    2023-11-11
  • 詳解Nuxt內(nèi)導(dǎo)航欄的兩種實(shí)現(xiàn)方式

    詳解Nuxt內(nèi)導(dǎo)航欄的兩種實(shí)現(xiàn)方式

    這篇文章主要介紹了詳解Nuxt內(nèi)導(dǎo)航欄的兩種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Vue2.x Todo之自定義指令實(shí)現(xiàn)自動聚焦的方法

    Vue2.x Todo之自定義指令實(shí)現(xiàn)自動聚焦的方法

    我們希望用戶雙擊 todo 進(jìn)入編輯狀態(tài)后輸入框自動獲取焦點(diǎn),而不是需要先手動點(diǎn)一下。這篇文章主要介紹了Vue 2.x Todo之自定義指令實(shí)現(xiàn)自動聚焦,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2019-01-01
  • ant design vue中table表格滾動加載實(shí)現(xiàn)思路

    ant design vue中table表格滾動加載實(shí)現(xiàn)思路

    在處理一寫數(shù)據(jù)量特別大的情況下,我們不能把后端的數(shù)據(jù)一次性全部拿到前端在table表格中展示,為了考慮性能優(yōu)化,使用了滾動加載表格數(shù)據(jù),這篇文章主要介紹了ant design vue中table表格滾動加載實(shí)現(xiàn)思路,需要的朋友可以參考下
    2024-07-07
  • vue3整合SpringSecurity加JWT實(shí)現(xiàn)登錄認(rèn)證

    vue3整合SpringSecurity加JWT實(shí)現(xiàn)登錄認(rèn)證

    本文主要介紹了vue3整合SpringSecurity加JWT實(shí)現(xiàn)登錄認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • Element中el-form表單舉例詳解

    Element中el-form表單舉例詳解

    Form組件提供了表單驗(yàn)證的功能,只需要通過屬性傳入約定的驗(yàn)證規(guī)則,并將Form-Item的屬性設(shè)置為需校驗(yàn)的字段名即可,下面這篇文章主要給大家介紹了關(guān)于Element中el-form表單的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • vue 使用插槽分發(fā)內(nèi)容操作示例【單個(gè)插槽、具名插槽、作用域插槽】

    vue 使用插槽分發(fā)內(nèi)容操作示例【單個(gè)插槽、具名插槽、作用域插槽】

    這篇文章主要介紹了vue 使用插槽分發(fā)內(nèi)容操作,結(jié)合實(shí)例形式總結(jié)分析了vue.js使用單個(gè)插槽、具名插槽、作用域插槽相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2020-03-03
  • vue實(shí)現(xiàn)無縫輪播效果(跑馬燈)

    vue實(shí)現(xiàn)無縫輪播效果(跑馬燈)

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)無縫輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05

最新評論