前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue中常用API

Vue中常用API

原创
作者头像
愤怒的小鸟
修改2021-03-29 17:37:00
5710
修改2021-03-29 17:37:00
举报
文章被收录于专栏:web shareweb shareweb share

一、slot 插槽

普通插槽

let AppLayout = {
  template: '<div class="container">' 
    + '<header> <slot name="header"></slot>< /header>' 
    + '<main> <slot>默认内容</slot> </main>' 
    + '<footer> <slot name="footer"></slot> </footer>' 
    + '</div>'
}

let vm = new Vue({
  el: '#app',
  template: '<div>' 
    + '<app-layout>' 
      + '<h1 slot="header">{{title}}</h1>' 
      + '<p>{{msg}}</p>' 
      + '<p slot="footer">{{desc}}</p>' 
    + '</app-layout>' +
  '</div>',
  data() {
    return {
      title: '我是标题',
      msg: '我是内容', 
      desc: '其它信息'
    } 
  },
  components: {
    AppLayout
  } 
})

作用域插槽

let Child = {
  template: '<div class="child">' +
  '<slot text="Hello " :msg="msg"></slot>' +
  '</div>',
  data() {
    return {
      msg: 'Vue'
    } 
  }
}

let vm = new Vue({
  el: '#app',
  template: '<div>' 
    + '<child>' 
    + '<template slot-scope="props">' 
      + '<p>Hello from parent</p>' 
      + '<p>{{ props.text + props.msg}}</p>' 
    + '</template>' 
    + '</child>' + 
  '</div>',
  components: {
    Child 
  }
})

二、 <keep-alive> 的组件实现

let A = {
  template: '<div class="a">' +
  '<p>A Comp</p>' +
  '</div>',
  name: 'A'
}

let B = {
  template: '<div class="b">' +
  '<p>B Comp</p>' +
  '</div>',
  name: 'B'
}

let vm = new Vue({
  el: '#app',
  template: '<div>' +
  '<keep-alive>' +
  '<component :is="currentComp">' + '</component>' +
  '</keep-alive>' +
  '<button @click="change">switch</button>' + '</div>',
  data: {
    currentComp: 'A'
  },
  methods: {
    change() {
      this.currentComp = this.currentComp === 'A' ? 'B' : 'A' 
    }
  },
  components: {
    A,
    B 
  }
})

三、过渡动画相关的组件

<transition> 组件

  1. web 平台独有的;
  2. 和<keep-alive> 组件类似,同样是抽象组件,同样直接实现render函数,同样利用了默认插槽。

在下列情形中,可以给任何元素和组件添加 entering/leaving 过渡:

  • 条件渲染 (使用v-if)
  • 条件展示 (使用v-show)
  • 动态组件
  • 组件根节点
let vm = new Vue({
  el: '#app',
  template: '<div id="demo">' +
    '<button v-on:click="show = !show">' 
        + 'Toggle' +
    '</button>' +
    '<transition :appear="true" name="fade">' 
        + '<p v-if="show">hello</p>' + 
    '</transition>' +
  '</div>',
  data() {
    return {
      show: true
    } 
  }
})

// css
.fade-enter-active, .fade-leave-active { 
  transition: opacity .5s;
}

.fade-enter, .fade-leave-to {
  opacity: 0; 
}

<transition-group> 组件

  • 对列表元素进行添加和删除时,实现了列表的过渡效果;
  • 也是由 render 函数渲染生成 vnode;
  • 不同于<transition> 组件,<transition-group> 组件是非抽象组件,它会渲染成一个真实元素, 默认 tag 是 span 。
let vm = new Vue({
    el: '#app',
    template: '<div id="list-complete-demo" class="demo">' +
        '<button v-on:click="add">Add</button>' +
        '<button v-on:click="remove">Remove</button>' +
        '<transition-group name="list-complete" tag="p">' +
            '<span v-for="item in items" v-bind:key="item" class="list-complete-item">' 
                + '{{ item }}' +
            '</span>' +
        '</transition-group>' +
    '</div>',
    data: {
        items: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        nextNum: 10
      },
      methods: {
        randomIndex: function () {
            return Math.floor(Math.random() * this.items.length) 
        },
        add: function () {
            this.items.splice(this.randomIndex(), 0, this.nextNum++)
        },
        remove: function () {
            this.items.splice(this.randomIndex(), 1) 
        }
      } 
   })
   
   // css
   .list-complete-item { 
       display: inline-block; 
       margin-right: 10px;
    }
    
    .list-complete-move {
      transition: all 1s;
    }
    
    .list-complete-enter, .list-complete-leave-to { 
        opacity: 0;
        transform: translateY(30px);
    }
    
    .list-complete-enter-active {
        transition: all 1s;
    }
    
    .list-complete-leave-active { 
        transition: all 1s; 
        position: absolute;
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、slot 插槽
    • 普通插槽
      • 作用域插槽
      • 二、 <keep-alive> 的组件实现
      • 三、过渡动画相关的组件
        • <transition> 组件
          • <transition-group> 组件
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档