您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Vue项目中使用继承extends实现复用代码

彭世瑜 发布时间:2019-11-14 22:56:43 ,浏览量:2

parent.vue 定义了1个方法和2个数据


  
      这是父组件
      {{message}}
  



export default {
  name: 'Parent',

  data() { 
    return {
        message: "Hello Vue",
        sayHello: "Parent say hello"
    }
  },
  methods:{
      showMessage(){
          alert(this.sayHello + this.message);
      }
  },
 }

child.vue 继承父组件,使用父组件中的方法,并覆写父组件中的数据


  
      这是子组件
      {{message}}
      点击        
  



import Parent from "./Parent.vue";

export default {
  name: 'Child',
  
  extends: Parent,

  data(){
      return{
          sayHello: "Child say hello"
      }
  }
  
 }

about.vue 调用child组件


  
    这是about
    
  



import Child from "./Child.vue";

export default {
  name: "About",
  components:{
    Child
    },
}

实现效果,父组件的方法取得了子组件覆写过的数据

在这里插入图片描述

关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0960s