vue使用的细节

1.is的使用

在h5中规定<table>标签里面包裹的必须是<tr>,如果像普通组件一样在页面上直接引用<row></row>,那么模板渲染出来的会出错,解决方法就是用is,类似的标签还有<ul><li></li></ul>,<ol><li></li></ol>,<select><option></option></select>,都是通过is去解决

<table>
      <tbody>
        <tr is="row"></tr>
        <tr is="row"></tr>
        <tr is="row"></tr>
      </tbody>
    </table>

<script>
//注册一个全局组件
 Vue.component('row', {
      data: function () {
        return {
          content: "this is rows"
        }
      },
      template: '<tr><td>{{content}}</td></tr>'
    })
</script>

2.ref,通过这个对dom进行操作

在普通的html标签中,ref获取的是标签对应的dom元素,在组件中,ref获取的是子组件的引用,接下来可以对dom进行一些操作

<div ref="hello" @click="handleClick">hello world</div>
<counter ref="one" @change="handleChange">0</counter>
<counter ref="two" @change="handleChange">0</counter>
<div>{{total}}</div>
<script>
Vue.component('counter',{
      template:'<div @click="handleNumberClick">{{number}}</div>',
      data:function(){
        return {
          number:0
        }
      },
      methods:{
        handleNumberClick:function(){
          this.number++
          this.$emit('change')
        }
      }
    })

    var vm = new Vue({
      el: "#app",
      data: {
        total:0
      },
      methods:{
        handleClick:function(){
         console.log( this.$refs.hello)//<div>hello</div>
        },
        handleChange:function(){
          console.log(this.$refs.one.number)//输出counter里面的number
          console.log(this.$refs.two.number)//输出counter里面的number
          this.total = this.$refs.one.number+this.$refs.two.number
        }
       
      }
    })
</script>

3.动态组件和v-once

动态组件<component></component>标签是vue底层自带的标签,可以按需加载组件,v-once可以让组件只加载一次

<div id=app>
    <component :is="show"></component>
    <p><button @click="handleClick">change</button></p>
  </div>

  <script>

    Vue.component('child-one', {
      template: `<div v-once>child-one</div>`
    })

     Vue.component('child-two', {
      template: `<div v-once>child-two</div>`
    })

    var vm = new Vue({
      el: "#app",
      data:{
        show:'child-one'
      },
      methods:{
        handleClick:function(){
         // alert('1')
         console.log(this.show)
        //  this.show = this.show ==='child-one' ? 'child-two' : 'child-one'
          if(this.show==='child-one'){
            this.show='child-two'
          }else{
            this.show='child-one'
          }
        }
      }
    })
  </script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 14,298评论 3 20
  • 今晚我在捕捉一轮月亮 我躲在黑夜里 悄然不动 生怕惊着它被它逃脱 谁知道—— 时间居然出卖了我 你看 月亮随着明天...
    Atuia阅读 1,603评论 0 0
  • 海棠社(25) 文/寒霜 腊梅 小巧玲珑体 端庄又大气 淡香浮鼻息 不争群芳丽 火红小身姿 翩翩风中起 如若白蝶...
    刘寒霜阅读 2,953评论 3 6
  • 2001年工作以来,除开2011陪老婆产假,这次休息近10天是最长的一次,抛开工作,抛开烦恼,带上爱人孩子,度过工...
    阿甘的坚持阅读 4,039评论 2 50
  • 无论走到哪里,习惯性的泡壶茶来。浓有浓的醇厚,淡有淡的清雅。习惯有茶的日子,不求轰轰烈烈精彩,但求平平淡淡的真实。...
    激扬文字绘山川阅读 3,511评论 5 12

友情链接更多精彩内容