Skip to content

Commit 2bd655e

Browse files
committed
a
1 parent 94b2a26 commit 2bd655e

File tree

9 files changed

+30
-33
lines changed

9 files changed

+30
-33
lines changed

docs/en/class-component/reference/code-usage.ts

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ import { Ref, Component, Vue } from 'vue-facing-decorator'
77
<div ref="refEl"></div>
88
</template>
99
10-
Vue options API
11-
{
12-
computed:{
13-
refEl(){
14-
return this.refs['refEl']
15-
}
16-
}
17-
}
1810
*/
1911

2012
@Component
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Usage
22

3-
Use `Ref` decorator to define a reference to `this.$refs['foo']` in vue's `computed`. The reference name is the name of property.
3+
Use `Ref` decorator to define a property getter `this.$refs[name]` on vue component instance. The reference name is the name of property.
44

55
[](./code-usage.ts ':include :type=code typescript')

docs/zh-cn/class-component/reference/code-usage.ts

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ import { Ref, Component, Vue } from 'vue-facing-decorator'
77
<div ref="refEl"></div>
88
</template>
99
10-
Vue options API
11-
{
12-
computed:{
13-
refEl(){
14-
return this.refs['refEl']
15-
}
16-
}
17-
}
1810
*/
1911

2012
@Component
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## 用法
22

3-
使用装饰器`Ref`在vue `computed`中定义一个引用`this.$refs['foo']`。引用名是这个属性的名字。
3+
装饰器`Ref`在vue实力上定义一个指定引用`this.$refs[name]`的访问器。引用名是这个属性的名字。
44

55
[](./code-usage.ts ':include :type=code typescript')

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-facing-decorator",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "Vue typescript class and decorator based component.",
55
"main": "dist/index.js",
66
"keywords": [

src/component.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ export interface Cons { new(): any, prototype: any }
2525
function ComponentOption(cons: Cons, extend?: any) {
2626
const optionBuilder: OptionBuilder = {}
2727
optionVModel(cons,optionBuilder)
28-
optionComputed(cons, optionBuilder)
28+
optionComputed(cons, optionBuilder)//after VModel
2929
optionWatch(cons, optionBuilder)
3030
optionProps(cons, optionBuilder)
3131
optionInject(cons, optionBuilder)
3232
optionEmit(cons, optionBuilder)
33-
optionMethodsAndLifecycle(cons, optionBuilder)
34-
optionRef(cons, optionBuilder)
33+
optionRef(cons, optionBuilder)//after Computed
34+
optionMethodsAndLifecycle(cons, optionBuilder)//after Ref
35+
3536

3637
const raw = {
3738
data() {

src/option/ref.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ export const decorator = optoinNullableMemberDecorator(function (proto: any, nam
99

1010

1111
export function build(cons: Cons, optionBuilder: OptionBuilder) {
12-
optionBuilder.computed ??= {}
1312
const slot = obtainSlot(cons.prototype)
1413
const names = slot.obtainMap<Map<string, any>>('ref')!
1514
if (names) {
16-
names.forEach((value, name) => {
17-
optionBuilder.computed![name] = function (this: any) {
18-
return this.$refs[name]
19-
}
15+
optionBuilder.beforeCreateCallbacks??=[]
16+
optionBuilder.beforeCreateCallbacks.push(function(this:any){
17+
18+
names.forEach((value, name) => {
19+
20+
Object.defineProperty(this,name,{
21+
get(this:any){
22+
console.log('vvv',name)
23+
return this.$refs[name]
24+
}
25+
})
26+
27+
})
2028
})
29+
2130
}
2231
}

test/component.ts

+3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ describe('Component',
5858
Object.keys(EmptyContext).forEach(key => {
5959
if (key === 'data' && typeof EmptyContext[key] === 'function') {
6060
expect(true).to.equal(isEmptyObject(EmptyContext[key]()))
61+
}else if(key === 'beforeCreate' && typeof EmptyContext[key] === 'function'){
62+
expect(true).to.equal(isEmptyObject(EmptyContext[key]()))
6163
} else {
64+
6265
expect(true).to.equal(isEmptyObject(EmptyContext[key]))
6366
}
6467
})

test/option/ref.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const CompContext = Comp as any
1414
describe('decorator Ref',
1515
() => {
1616
it('default', () => {
17-
expect('function').to.equal(typeof CompContext?.computed?.refName)
18-
expect('refValue').to.equal(CompContext.computed.refName.apply({
19-
$refs: {
20-
refName: 'refValue'
21-
}
22-
}))
17+
expect('function').to.equal(typeof CompContext?.beforeCreate)
18+
// expect('refValue').to.equal(CompContext.computed.refName.apply({
19+
// $refs: {
20+
// refName: 'refValue'
21+
// }
22+
// }))
2323
})
2424
}
2525
)

0 commit comments

Comments
 (0)