前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >高级vue教程 源码分析

高级vue教程 源码分析

作者头像
lilugirl
发布2020-03-19 10:05:21
4390
发布2020-03-19 10:05:21
举报
文章被收录于专栏:前端导学

学习getter setter

代码语言:javascript
复制
 function convert(obj) {
      Object.keys(obj).forEach(key => {
        let internalValue = obj[key]
        Object.defineProperty(obj, key, {
          get() {
            console.log(`getting key "${key}": ${internalValue}`)
            return internalValue
          },
          set(newValue) {
            console.log(`setting key "${key}" to: ${newValue}`)
            internalValue = newValue
          }
        })
      })
    }
代码语言:javascript
复制
let student = {
      name: 'liuyi',
      age: 12,
      sex: 'female'
    }

    convert(student)
    Object.keys(student).forEach(key => {
      console.log('student.key', student[key]);
    })

    console.log('student.name', student.name);
    console.log('student.age', student.age);
    student.name = "xiaowang";
    student.age = student.age + 1;
    console.log('student.name', student.name);

Plugins

Render Functions

Render Function API

代码语言:javascript
复制
export default {
  render (h) {
    return h(‘div’,{},[…])
  }
}

The h function

h can directly render a component

做一个练习

代码语言:javascript
复制
<example tags="['h1','h2','h3']"></example>
<script>

Vue.component('example',{
  props:['tags],
  render:(h){
    return h('div',this.tags.map((tag,i)=>h(tag,i)))
  }
});

new Vue({el:"#app"})
</script>

或者

代码语言:javascript
复制
<example tags="['h1','h2','h3']"></example>
<script>
Vue.component('example',{
  functonal:true,
  render:(h,{props:{tags}}){
    return h('div',tags.map((tag,i)=>h(tag,i)))
  }
});

new Vue({el:"#app"})
</script>

另一个练习 Dynamically Render Components

代码语言:javascript
复制
<div id="app">
 <example :ok="ok"></example>
 <button @click="ok = !ok">toggle</button>
</div>

<script>

const Foo = {
  render:h=>h('div','foo')
}

const Bar = {
  render:h=>h('div','bar')
}

Vue.component('example',{
  props:['ok'],
  render(h){
    return this.ok?h(Foo):h(Bar)
  }
})




new Vue({el:"#app",
data:{ok:true}})

</script>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Plugins
  • Render Functions
    • Render Function API
      • The h function
    • 做一个练习
      • 另一个练习 Dynamically Render Components
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档