【每日学习记录】【vue组件】is 解决tr或ul下使用模板的问题

2020-11-19记录

vue组件

1.is 解决tr或ul下使用模板的问题

<body>
  <div id="app">
    <table>
      <tbody>
        <tr is="row"></tr>
        <tr is="row"></tr>
        <tr is="row"></tr>
      </tbody>
    </table>
  </div>
</body>
<script>
  Vue.component('row',{
    template: '<tr><td>this is a row</td></tr>'
  })
  var vm= new Vue({
    el: '#app'
  })
</script>

第五到七行代码直接写成<row></row>会出现结构错误,ul同理
使用is绑定模板可以解决这个问题

2.子组件中的data必须是一个函数而不能是对象

  Vue.component('row',{
    data:function(){
      return {
        content: "this is row"
      }
    },
    template: '<tr><td>{{content}}</td></tr>'
  })

3.ref的使用

ref——引用

<body>
  <div id="app">
    <div ref='hello'
     @click="handleClick"
     >
      hello
    </div>
  </div>
</body>
<script>
  var vm= new Vue({
    el: '#app',
    methods:{
      handleClick: function(){
        console.log(this.$refs.hello.innerHTML)
      }
    }
  })
</script>

4.计数器、父组件对子组件的计数

<body>
  <div id="app">
    <counter ref="one" @change='handleChange'></counter>
    <counter ref="two" @change='handleChange'></counter>
    <div>{{total}}</div>
  </div>
</body>
<script>
  Vue.component('counter',{
    template:'<div @click="handleClick">{{number}}</div>',
    data: function(){
      return {
        number:0
      }
    },
    methods: {
      handleClick:function(){
        this.number ++
        this.$emit('change')
      }
    }
  })
  var vm= new Vue({
    el: '#app',
    data : {
      total:0
    },
    methods:{
      handleChange: function(){
        this.total=this.$refs.one.number+this.$refs.two.number;
      }
    }
  })
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值