Vue的$emit傳值

2023-4-6    前端達人

$emit,父組件傳data給子組件,子組件通過$emit來觸發(fā)父組件中綁定在子組件身上的事件,達到改變父組件中的data的方法。下面介紹$emit傳值的幾種方法:

一:$emit傳遞單值

子組件Test.vue:


  1. <template>
  2. <div>
  3. <div>子組件</div>
  4. <button @click="changeFather">點擊我向父組件傳遞參數(shù)</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. changeFather() {
  11. this.$emit("changeEvent",'1');
  12. }
  13. }
  14. };
  15. </script>
  16. <style>
  17. </style>

父組件:App.vue


  1. <template>
  2. <div id="app">
  3. <p>這是父組件</p>
  4. <div>{{myString}}</div>
  5. <Test @changeEvent="changeMyString" />
  6. </div>
  7. </template>
  8. <script>
  9. import Test from "./components/Test";
  10. export default {
  11. name: "App",
  12. components: { Test },
  13. data: function() {
  14. return {
  15. myString: ''
  16. };
  17. },
  18. methods: {
  19. changeMyString(val) {
  20. console.log(val);
  21. this.myString=val;
  22. }
  23. }
  24. };
  25. </script>
  26. <style>
  27. #app {
  28. font-family: Avenir, Helvetica, Arial, sans-serif;
  29. -webkit-font-smoothing: antialiased;
  30. -moz-osx-font-smoothing: grayscale;
  31. text-align: center;
  32. color: #2c3e50;
  33. margin-top: 60px;
  34. }
  35. </style>

點擊按鈕效果如圖:

二:$emit傳遞多個值

子組件Test.vue:


  1. <template>
  2. <div>
  3. <div>子組件</div>
  4. <button @click="changeFather">點擊我向父組件傳遞參數(shù)</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. changeFather() {
  11. this.$emit("changeEvent",'1','2');
  12. }
  13. }
  14. };
  15. </script>
  16. <style>
  17. </style>

父組件App.vue:


  1. <template>
  2. <div id="app">
  3. <p>這是父組件</p>
  4. <div>{{myString}}</div>
  5. <Test @changeEvent="changeMyString" />
  6. </div>
  7. </template>
  8. <script>
  9. import Test from "./components/Test";
  10. export default {
  11. name: "App",
  12. components: { Test },
  13. data: function() {
  14. return {
  15. myString: ''
  16. };
  17. },
  18. methods: {
  19. changeMyString(val0,val1) {
  20. console.log(val0,val1);
  21. this.myString=val0+val1;
  22. }
  23. }
  24. };
  25. </script>
  26. <style>
  27. #app {
  28. font-family: Avenir, Helvetica, Arial, sans-serif;
  29. -webkit-font-smoothing: antialiased;
  30. -moz-osx-font-smoothing: grayscale;
  31. text-align: center;
  32. color: #2c3e50;
  33. margin-top: 60px;
  34. }
  35. </style>

點擊按鈕,效果如下:

$emit傳遞多個值時,還可以采用數(shù)組的形式:

修改子組件Test.vue:


  1. <template>
  2. <div>
  3. <div>子組件</div>
  4. <button @click="changeFather">點擊我向父組件傳遞參數(shù)</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. changeFather() {
  11. this.$emit("changeEvent",['1','2']);
  12. }
  13. }
  14. };
  15. </script>
  16. <style>
  17. </style>

父組件App.vue:


  1. <template>
  2. <div id="app">
  3. <p>這是父組件</p>
  4. <div>{{myString}}</div>
  5. <Test @changeEvent="changeMyString" />
  6. </div>
  7. </template>
  8. <script>
  9. import Test from "./components/Test";
  10. export default {
  11. name: "App",
  12. components: { Test },
  13. data: function() {
  14. return {
  15. myString: ''
  16. };
  17. },
  18. methods: {
  19. changeMyString(val) {
  20. console.log(val);
  21. this.myString=val[0]+val[1];
  22. }
  23. }
  24. };
  25. </script>
  26. <style>
  27. #app {
  28. font-family: Avenir, Helvetica, Arial, sans-serif;
  29. -webkit-font-smoothing: antialiased;
  30. -moz-osx-font-smoothing: grayscale;
  31. text-align: center;
  32. color: #2c3e50;
  33. margin-top: 60px;
  34. }
  35. </style>

點擊按鈕,效果如下:

三:子組件通過$emit傳遞給父組件傳遞值,并且父組件有自定義參數(shù)時:

子組件Test.vue:


  1. <template>
  2. <div>
  3. <div>子組件</div>
  4. <button @click="changeFather">點擊我向父組件傳遞參數(shù)</button>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. changeFather() {
  11. this.$emit("changeEvent",1,2);
  12. }
  13. }
  14. };
  15. </script>
  16. <style>
  17. </style>

父組件:App.vue


  1. <template>
  2. <div id="app">
  3. <p>這是父組件</p>
  4. <div>{{myString}}</div>
  5. <Test @changeEvent="changeMyString('myParameter',...arguments)" />
  6. </div>
  7. </template>
  8. <script>
  9. import Test from "./components/Test";
  10. export default {
  11. name: "App",
  12. components: { Test },
  13. data: function() {
  14. return {
  15. myString: ''
  16. };
  17. },
  18. methods: {
  19. changeMyString(...args) {
  20. console.log(args);
  21. this.myString=args;
  22. }
  23. }
  24. };
  25. </script>
  26. <style>
  27. #app {
  28. font-family: Avenir, Helvetica, Arial, sans-serif;
  29. -webkit-font-smoothing: antialiased;
  30. -moz-osx-font-smoothing: grayscale;
  31. text-align: center;
  32. color: #2c3e50;
  33. margin-top: 60px;
  34. }
  35. </style>

點擊按鈕,效果圖如下:





藍藍設計建立了UI設計分享群,每天會分享國內(nèi)外的一些優(yōu)秀設計,如果有興趣的話,可以進入一起成長學習,請加微信ban_lanlan,報下信息,藍小助會請您入群。歡迎您加入噢~~

希望得到建議咨詢、商務合作,也請與我們聯(lián)系01063334945。 



分享此文一切功德,皆悉回向給文章原作者及眾讀者. 免責聲明:藍藍設計尊重原作者,文章的版權歸原作者。如涉及版權問題,請及時與我們?nèi)〉寐?lián)系,我們立即更正或刪除。 



藍藍設計m.yvirxh.cn )是一家專注而深入的界面設計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網(wǎng)站建設 、平面設計服務UI設計公司、界面設計公司、UI設計服務公司、數(shù)據(jù)可視化設計公司、UI交互設計公司、高端網(wǎng)站設計公司、UI咨詢、用戶體驗公司、軟件界面設計公司。

日歷

鏈接

個人資料

藍藍設計的小編 http://m.yvirxh.cn

存檔