Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 3.06 KB

README.zh-CN.md

File metadata and controls

41 lines (31 loc) · 3.06 KB

简单的 Vue 类型 困难 #this #application #vue

by Anthony Fu @antfu

接受挑战    English 日本語

由谷歌自动翻译,欢迎 PR 改进翻译质量。

实现类似Vue的类型支持的简化版本。

通过提供函数名称SimpleVue(类似于Vue.extenddefineComponent),它应该正确地推断出计算和方法内部的this类型。

在此挑战中,我们假设SimpleVue接受带有datacomputedmethods字段的Object作为唯一参数,

-data是一个简单的函数,它返回一个公开上下文this的对象,但是您无法访问data中的数据本身或其他计算机值或方法。

-computed是将上下文作为this的函数的对象,进行一些计算并返回结果。计算结果应作为简单的返回值而不是函数显示给上下文。

-methods是函数的对象,其上下文也与this相同。方法可以访问datacomputed以及其他methods公开的字段。 computedmethods的不同之处在于按原样公开的功能。

SimpleVue的返回值类型可以是任意的。

const instance = SimpleVue({
  data() {
    return {
      firstname: 'Type',
      lastname: 'Challenges',
      amount: 10,
    }
  },
  computed: {
    fullname() {
      return this.firstname + ' ' + this.lastname
    }
  },
  methods: {
    hi() {
      alert(this.fullname.toLowerCase())
    }
  }
})

返回首页 分享你的解答 查看解答

相关挑战

213・Vue Basic Props