vue慕课网(去哪儿)学习(第四章 1.Vue组件使用细节)

119 阅读1分钟

组件使用过程中的细节:

  1. is属性的使用
  <div id="root">
    <table>
      <tbody>
        <tr is="row" ></tr>
        <row></row>
      </tbody>
    </table>
  </div>
  <script>

    Vue.component('row', {
      template: '<tr><td>this is a row</td></tr>'
    })

    let vm = new Vue({
      el:'#root'
    })
  </script>
  1. 根组件定义data为一个对象可以,但是子组件的data应该是一个函数,子组件可能被多次调用,因此每个子组件所使用的数据都是自己的一套。通过函数返回对象可以让每个子组件都有自己独立的组件。
    Vue.component('row', {
      // 
      data: function() {
        return {
          content: 'is '
        }
      },
      template: '<tr><td>{{content}}</td></tr>'
    })
  1. vue中操作dom可通过ref获取dom节点
    <div @ref="hello" ></div>
     // this.$refs.hello 可获取到该dom
  1. 发布订阅模式,