vue的this.$emit和props的用法

迈不过友情╰ 2022-12-15 12:59 256阅读 0赞

vue的this.$emit和props的用法

vue组件数据传递:

1.父组件传递给子组件:在父组件引用的子组件标签上增加属性就可以传递,子组件在props: {父子间属性名称:数据类型}中接收

2.子组件监听父组件方法 ,使用this.$emit()触发父组件属性

以vue的公共头部为例

父组件

App.vue父组件,引用子组件nav-header。属性headData属性

  1. <template>
  2. <div id='app'>
  3. <nav-header :headData="headData" ></nav-header>
  4. <router-view @headFun="headFun" ></router-view>
  5. </div>
  6. </template>
  7. <script>
  8. import NavHeader from '@/components/NavHeader'
  9. export default {
  10. name: 'app',
  11. components: {
  12. NavHeader
  13. },
  14. data() {
  15. return {
  16. headData: [],
  17. }
  18. },
  19. mounted() {
  20. },
  21. methods: {
  22. headFun(data){
  23. //data是App.vue的子组件通过this.$emit触发他的headFun函数传过来的值
  24. this.headData=data;
  25. }
  26. }
  27. }
  28. </script>

子组件 通过props接收父组件App.vue的参数

  1. <template>
  2. <div>
  3. <header class="ui-header is-fixed" v-show="hidshow">
  4. <div class="ui-header-left">
  5. <i class="mint-toast-icon mintui mintui-back" v-if="headData.goIcon==true"></i>
  6. </div>
  7. <span class="ui-head-title">{
  8. {headData.title}}</span>
  9. <div class="ui-header-right">
  10. <img src="" alt="" v-if="headData.cartIcon==true">
  11. <img src="" alt="" v-if="headData.perIcon==true">
  12. <i class="mint-toast-icon mintui mintui-more" v-if="headData.moreIcon==true"></i>
  13. </div>
  14. </header>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. name: "CityAlphabet",
  20. props: ['headData'],
  21. computed: {
  22. },
  23. data() {
  24. return {
  25. hidshow: false
  26. }
  27. },
  28. methods: {
  29. }
  30. }
  31. </script>
  32. <style scoped>
  33. .ui-header .is-fixed{top:0;left :0;right:0;position: fixed;z-index: 1}
  34. .ui-header{width:100%;height:50px;line-height:50px;text-align:center;background-color:#d23a27;box-sizing: border-box;color: #fff;font-size: 17px;position: relative;}
  35. .mintui{font-size: 25px;display: inline-block;}
  36. .ui-head-left{position: absolute;left: 0;top: 0;bottom: 0;padding: 0 10px;}
  37. .ui-head-right{position: absolute;right: 0;top: 0;bottom: 0;padding: 0 10px;display: -webkit-box;line-height: 50px;padding-top: 10px;}
  38. .ui-head-right i{color: #fff;}
  39. .ui-head-right .cart-img{width: 30px;height: 30px;}
  40. </style>

另一个相对于App.vue子组件 利用this.$emit去触发父组件中的headFun函数

  1. <template>
  2. <div>
  3. </div>
  4. </template>
  5. <script>
  6. export default {
  7. name: "home",
  8. computed: {
  9. },
  10. data() {
  11. return {
  12. }
  13. },
  14. mounted(){
  15. this.$emit("headFun",
  16. {
  17. title:'表通',
  18. goback:'',
  19. perIcon:true,
  20. cartIcon:true,
  21. moreIcon:false
  22. })
  23. },
  24. methods: {
  25. }
  26. }
  27. </script>

借鉴:https://blog.csdn.net/yang1393214887/article/details/104871682

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vueprop

    prop的大小写 HTML 中的 `attribute` 名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符。这意味着当你使用 DOM 中的模板时,camelC