一文淺析Vue中父子組件間傳值問題
vue父子元件之間怎麼傳值?以下這篇文章帶大家了解Vue中父元件以及子元件傳值問題,希望對大家有幫助!
前言:在一些頁面中不單單的純純的一個vue文件,vue講究組件化開發,但是一般的肯定會產生交互事件,今天了解了這個傳值,特此的來記錄一下。
一.父元件傳送值
#父元件傳送值給子元件會用到:Prop,一般的我們需要在子元件中進行相關的聲明,如下所示:
子元件為HellowWorld.vue
##
<script>export default { name: 'HelloWorld', //接收的变量 props: { //声明相关的类型 msg: String, count:Number, options:[] }, data(){ return{ } }, methods:{ }}</script>
在父元件App.vue中
<template> <div> <!-- msg为字符串类型,count为数字,options为数组 --> <helloworld></helloworld> </div></template><script>//引入组件import HelloWorld from './components/HelloWorld.vue'export default { name: 'App', components: { HelloWorld }, data(){ return{ count:0, options:[], } }, methods:{ }}</script>
那麼在頁面上效果就是:##當然我們也可以寫一些事件來進行動態的資料交互,例如:
#二.子元件傳送值
##在子元件傳值時會用到$emit,值得注意的是:在子元件傳值時候的方法要與父元件中監聽的方法名稱相同,也就是範例中的listenToChild。 【相關推薦:
vuejs影片教學###、###web前端開發###】##########Helloworld.vue子元件:######<template> <div> <!-- 文字信息 --> <h1 id="msg">{{ msg }}</h1> <!-- 数字信息 --> <h2 id="count">{{count}}</h2> <!-- 渲染数组信息 --> <ul> <li>{{item}}</li> </ul> <!-- 进行传值 --> <button>点击</button> </div></template><script>export default { name: 'HelloWorld', props: { msg: String, count:Number, options:[] }, data(){ return{ } }, methods:{ SendMsg(){ // listenToChild 注意 this.$emit('listenToChild',this.options) } }}</script><!-- Add "scoped" attribute to limit CSS to this component only --><style>h3 { margin: 40px 0 0;}ul { list-style-type: none; padding: 0;}/* li { display: inline-block; margin: 0 10px; } */a { color: #42b983;}</style>
<template> <div> <img src="/static/imghw/default1.png" data-src="./assets/logo.png" class="lazy" alt="一文淺析Vue中父子組件間傳值問題" > <!-- listenToChild 为子组件传来的方法 --> <helloworld></helloworld> <button>+</button> <button>置零</button> <button>-</button> <ul> <li>{{index}},{{item}}</li> </ul> </div></template><script>import HelloWorld from './components/HelloWorld.vue'export default { name: 'App', components: { HelloWorld }, data(){ return{ // 要传去子组件的参数 count:0, options:[], // 子组件传来的参数 data:[] } }, methods:{ Add(){ this.count=Number(this.count)+1 this.options.push('添加一次,当前数值为:'+this.count) }, Sub(){ if(this.count<=0){ this.options.push('当前数值不能变化了'+this.count) }else{ this.count=Number(this.count)-1 this.options.pop() } }, show(data){ console.log(data) this.data=data }, restart(){ this.count=0 this.options=[] } }}</script><style>#app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px;}button{ margin: 20px;}ul { list-style-type: none; padding: 0;}</style>
以上是一文淺析Vue中父子組件間傳值問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

可以通過以下步驟為 Vue 按鈕添加函數:將 HTML 模板中的按鈕綁定到一個方法。在 Vue 實例中定義該方法並編寫函數邏輯。

Vue.js 中的 watch 選項允許開發者監聽特定數據的變化。當數據發生變化時,watch 會觸發一個回調函數,用於執行更新視圖或其他任務。其配置選項包括 immediate,用於指定是否立即執行回調,以及 deep,用於指定是否遞歸監聽對像或數組的更改。

Vue 多頁面開發是一種使用 Vue.js 框架構建應用程序的方法,其中應用程序被劃分為獨立的頁面:代碼維護性:將應用程序拆分為多個頁面可以使代碼更易於管理和維護。模塊化:每個頁面都可以作為獨立的模塊,便於重用和替換。路由簡單:頁面之間的導航可以通過簡單的路由配置來管理。 SEO 優化:每個頁面都有自己的 URL,這有助於搜索引擎優化。

在 Vue.js 中引用 JS 文件的方法有三種:直接使用 <script> 標籤指定路徑;利用 mounted() 生命週期鉤子動態導入;通過 Vuex 狀態管理庫進行導入。

Vue.js 返回上一頁有四種方法:$router.go(-1)$router.back()使用 <router-link to="/"> 組件window.history.back(),方法選擇取決於場景。

Vue.js 遍歷數組和對像有三種常見方法:v-for 指令用於遍歷每個元素並渲染模板;v-bind 指令可與 v-for 一起使用,為每個元素動態設置屬性值;.map 方法可將數組元素轉換為新數組。

Vue 中 div 元素跳轉的方法有兩種:使用 Vue Router,添加 router-link 組件。添加 @click 事件監聽器,調用 this.$router.push() 方法跳轉。
