Replies: 5 comments 5 replies
-
You may need to use @vue/reactivity,Because const defaultWatcherValue = Symbol()
ctx.scope.$watch = (
expOrFn: string,
cb: any,
options?: {
immediate?: boolean
}
) => {
let lastValue: any = defaultWatcherValue
ctx.effect(() => {
const value = evaluate(ctx.scope, expOrFn);
const isActive =
lastValue === defaultWatcherValue ? options?.immediate : value !== lastValue
if (isActive) {
cb.call(ctx.scope, value, lastValue)
}
lastValue = value
})
} But it will be easier to use @vue/reactivity |
Beta Was this translation helpful? Give feedback.
-
An easier way is to use a getter in the scope, e.g. for a square number: or another approach:
|
Beta Was this translation helpful? Give feedback.
-
The lack of Sure I can use data setters to intercept local data changes but I really want to intercept changes to a global-state-management reactive model. Like @7iomka, I don't understand the solution offered by @nooooooom - any chance of a simple plain javascript (not typescript) - not sure what P.S. Somebody (more knowledgable than me) should ideally create a pull request to update the README re what is not supported by petite-vue. Saying "ref(), computed() etc." is pretty vague - watch() should be added to that list. |
Beta Was this translation helpful? Give feedback.
-
@nooooooom is likely correct that for a robust implimentation you may need to utilize the reactive core package: @vue/reactivity Anthony Fu (antfu) did a write up on just that a while back. function traverse(value, seen = new Set()) {
if (!isObject(value) || seen.has(value))
return value
seen.add(value) // prevent circular reference
if (isArray(value)) {
for (let i = 0; i < value.length; i++)
traverse(value[i], seen)
}
else {
for (const key of Object.keys(value))
traverse(value[key], seen)
}
return value
}
const watch = (source, fn, { deep, lazy = true }) => {
let getter = isRef(source)
? () => source.value
: isReactive(source)
? () => source
: source
if (deep)
getter = () => traverse(getter())
const runner = effect(getter, {
lazy,
scheduler: fn
}
return () => stop(runner)
} This simply provides base functionality. the forms of watch() that most vue users are accustomed to is more integrated into the render lifecycle via component/effect scopes, flush timing, render type checks, etc.
Wrapped forms of this primitive are then exported for typical use. He has already done the work of packaging it here: |
Beta Was this translation helpful? Give feedback.
-
petite-vue-pro https://github.com/604587986/petite-vue-pro |
Beta Was this translation helpful? Give feedback.
-
I have var that depends on another var. In normal vue we have watch method. What I should do for that?
Beta Was this translation helpful? Give feedback.
All reactions