Vue三種常用傳值示例(父傳子、子傳父、非父子)
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方法代碼詳解
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ī)器人 ),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03ElementUI中<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-01vue開發(fā)中的base和publicPath的區(qū)別
本文主要介紹了vue開發(fā)中的base和publicPath的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07Vue Element UI 表單自定義校驗規(guī)則及使用
這篇文章主要介紹了Vue Element UI 表單自定義效驗規(guī)則及使用,文中通過代碼介紹了常見表單效驗規(guī)則,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02