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

Vue三種常用傳值示例(父傳子、子傳父、非父子)

 更新時間:2018年07月24日 10:49:34   作者:1461433958  
這篇文章主要介紹了Vue傳值-三種常用傳值示例,主要介紹了父組件向子組件進(jìn)行傳值,子組件向父組件傳值和非父子組件進(jìn)行傳值,感興趣的小伙伴們可以參考一下

1、父組件向子組件進(jìn)行傳值:

父組件:

  <template>
  <div>
  父組件:
  <input type="text" v-model="name">
  <br>
  <br>
  <!-- 引入子組件 -->
  <child :inputName="name"></child>
  </div>
 </template>
 <script>
  import child from './child'
  export default {
  components: {
   child
  },
  data () {
   return {
   name: ''
   }
  }
  }
 </script>

子組件:

 <template>
  <div>
  子組件:
  <span>{{inputName}}</span>
  </div>
 </template>
 <script>
  export default {
  // 接受父組件的值
  props: {
   inputName: String,
   required: true
  }
  }
 </script>

2.子組件向父組件傳值

子組件:

  <template>
   <div>
   子組件:
   <span>{{childValue}}</span>
   <!-- 定義一個子組件傳值的方法 -->
   <input type="button" value="點擊觸發(fā)" @click="childClick">
   </div>
  </template>
  <script>
   export default {
   data () {
    return {
    childValue: '我是子組件的數(shù)據(jù)'
    }
   },
   methods: {
    childClick () {
    // childByValue是在父組件on監(jiān)聽的方法
    // 第二個參數(shù)this.childValue是需要傳的值
    this.$emit('childByValue', this.childValue)
    }
   }
   }
  </script> 

父組件:

 <template>
 <div>
 父組件:
 <span>{{name}}</span>
 <br>
 <br>
 <!-- 引入子組件 定義一個on的方法監(jiān)聽子組件的狀態(tài)-->
 <child v-on:childByValue="childByValue"></child>
 </div>
</template>
<script>
 import child from './child'
 export default {
 components: {
  child
 },
 data () {
  return {
  name: ''
  }
 },
 methods: {
  childByValue: function (childValue) {
  // childValue就是子組件傳過來的值
  this.name = childValue
  }
 }
 }
</script>

3.非父子組件進(jìn)行傳值。(非父子組件之間傳值,需要定義個公共的公共實例文件bus.js,作為中間倉庫來傳值,不然路由組件之間達(dá)不到傳值的效果。)

公共bus.js

//bus.js
import Vue from 'vue'
export default new Vue()

組件A:

<template>
 <div>
 A組件:
 <span>{{elementValue}}</span>
 <input type="button" value="點擊觸發(fā)" @click="elementByValue">
 </div>
</template>
<script>
 // 引入公共的bug,來做為中間傳達(dá)的工具
 import Bus from './bus.js'
 export default {
 data () {
  return {
  elementValue: 4
  }
 },
 methods: {
  elementByValue: function () {
  Bus.$emit('val', this.elementValue)
  }
 }
 }
</script>

組件B:

 <template>
  <div>
  B組件:
  <input type="button" value="點擊觸發(fā)" @click="getData">
  <span>{{name}}</span>
  </div>
 </template>
 <script>
  import Bus from './bus.js'
  export default {
  data () {
   return {
   name: 0
   }
  },
  mounted: function () {
   var vm = this
   // 用$on事件來接收參數(shù)
   Bus.$on('val', (data) => {
   console.log(data)
   vm.name = data
   })
  },
  methods: {
   getData: function () {
   this.name++
   }
  }
  }
 </script>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue+Element-UI中el-table動態(tài)合并單元格:span-method方法代碼詳解

    Vue+Element-UI中el-table動態(tài)合并單元格:span-method方法代碼詳解

    el-table是element-ui提供的表格組件,可以用于展示和操作數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Vue+Element-UI中el-table動態(tài)合并單元格:span-method方法的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • 基于Vue2實現(xiàn)的仿手機(jī)QQ單頁面應(yīng)用功能(接入聊天機(jī)器人 )

    基于Vue2實現(xiàn)的仿手機(jī)QQ單頁面應(yīng)用功能(接入聊天機(jī)器人 )

    這篇文章主要介紹了基于Vue2實現(xiàn)的仿手機(jī)QQ單頁面應(yīng)用功能(接入聊天機(jī)器人 ),非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • vue使用echarts實現(xiàn)水平柱形圖實例

    vue使用echarts實現(xiàn)水平柱形圖實例

    這篇文章主要介紹了vue使用echarts實現(xiàn)水平柱形圖實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • WebPack配置vue多頁面的技巧

    WebPack配置vue多頁面的技巧

    這篇文章主要介紹了WebPack配置vue多頁面的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-05-05
  • Vite項目搭建與環(huán)境配置的完整版教程

    Vite項目搭建與環(huán)境配置的完整版教程

    Vite?使用?Rollup?作為默認(rèn)的構(gòu)建工具,可以將應(yīng)用程序的源代碼打包成一個或多個優(yōu)化的靜態(tài)文件,本文就來為大家介紹一下Vite如何進(jìn)行項目搭建與環(huán)境配置吧
    2023-09-09
  • ElementUI中<el-form>標(biāo)簽中ref、:model、:rules的作用淺析

    ElementUI中<el-form>標(biāo)簽中ref、:model、:rules的作用淺析

    Element官方文檔中寫到,model是表單數(shù)據(jù)對象,rules是表單驗證規(guī)則,下面這篇文章主要給大家介紹了關(guān)于ElementUI中<el-form>標(biāo)簽中ref、:model、:rules作用的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • vue開發(fā)中的base和publicPath的區(qū)別

    vue開發(fā)中的base和publicPath的區(qū)別

    本文主要介紹了vue開發(fā)中的base和publicPath的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • Vue Element UI 表單自定義校驗規(guī)則及使用

    Vue Element UI 表單自定義校驗規(guī)則及使用

    這篇文章主要介紹了Vue Element UI 表單自定義效驗規(guī)則及使用,文中通過代碼介紹了常見表單效驗規(guī)則,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • vue項目實戰(zhàn)總結(jié)篇

    vue項目實戰(zhàn)總結(jié)篇

    離放假還有1天,今天小編抽空給大家分享前端時間小編做的vue項目,非常完整,需要的朋友參考下
    2018-02-02
  • vue-router 4使用實例詳解

    vue-router 4使用實例詳解

    雖然 vue-router 4 大多數(shù) API 保持不變,但是在 vue3 中以插件形式存在,所以在使用時有一定的變化。接下來就學(xué)習(xí)學(xué)習(xí)它是如何使用的
    2021-11-11

最新評論