-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vue 3.0 #5
Comments
翻译:为什么放弃Class Api提案原因主要有两个原因:
目前Class提案的的问题类型复杂性本来引入Class提案是为了提供一个TS接口支持更友好的供选择API, 然而,Vue的组件需要merge不同来源声明的的属性,导致对Class Api来说也是不小的挑战
并且,目前没有方法对class方法的参数进行上下文类型推断——意味着不能通过Class的其他属性来对传递给Class render函数的参数进行推断。
给预留方法的命名空间已经讨论过了class声明下如何处理预留方法,目前没有一个提案方案比较完美。 实现的复杂性支持Class带来了一系列需要额外代码来cover的边际问题,主要是因为我们对使用了一个Proxy作为this来进行响应式,事实是在constructor(所有class初始化)的this将与所有其他地方的this不一致 This mostly has to do with our need of using a Proxy as this for reactivity tracking, and the fact that the this in the constructor (and thus all class field initializers) will be different from the this in all other places. 收益质疑综合上面的问题,引入class api的收益是需要质疑的
替代:Composition Api在Class Api之后,有两个新的API提案,[Advanced Reactivity API]和[动态生命周期注入],通过结合这些我们发现了一个新的方法来声明组件逻辑: // everything tree-shakable
import {
value,
computed,
watch,
onMounted,
inject
} from 'vue'
const App = {
// same as before
props: {
a: String,
b: Number
},
// same as before
components: {
// ...
},
setup(props) {
// data
const count = value(1)
// computed
const plusOne = computed(() => count.value + 1)
// methods
function inc() {
count.value++
}
// watch
watch(() => props.b + count.value, val => {
console.log('changed: ', val)
})
// lifecycle
onMounted(() => {
console.log('mounted!')
})
// dependency injection
const injected = inject(SomeSymbol)
// other options like el, extends and mixins are no longer necessary
// expose bindings on render context
// any value containers will be unwrapped when exposed
// any non-containers will be exposed as-is, including functions
return {
count,
plusOne,
inc,
injected
}
},
// template: `same as before`,
render({ state, props, slots }) {
// `this` points to the render context and works same as before (exposes everything)
// `state` exposes bindings returned from `setup()` (with value wrappers unwrapped)
}
} 更好的TypeScript支持上面的例子可以很好的进行推行推断,并且没有边界问题,只要放在参数位置 import { createComponent } from 'vue'
const App = createComponent({
props: {
// ...
},
setup(props) {
// props type inferred from `props` option
// composition functions are all easily typed since they
// don't rely on `this`
return {
// ...
}
},
render({ state, props }) {
// `state` type inferred from return value of setup()
// `this` type inferred from a merge of state and props
}
})
// The resulting type of `App` also supports TSX props inference 内部的类型同样更加简单 更好的模块能力Compostion 能够简单的通过逻辑而不是类型来区分,同时能增强复用性,并且能避免Mixins的缺点(命名冲突与不确定的属性来源) |
The text was updated successfully, but these errors were encountered: