Skip to content
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

feat: v-scope #7218

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

feat: v-scope #7218

wants to merge 19 commits into from

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Nov 26, 2022

@sxzz
Copy link
Member

sxzz commented Nov 26, 2022

It's great! I'd like to migrate it to Vue Macros for early experience.

@netlify
Copy link

netlify bot commented Nov 27, 2022

Deploy Preview for vuejs-coverage failed.

Name Link
🔨 Latest commit ab4a415
🔍 Latest deploy log https://app.netlify.com/sites/vuejs-coverage/deploys/63832143f2c6e80009ed5887

@edison1105 edison1105 marked this pull request as ready for review November 27, 2022 08:46
@edison1105 edison1105 changed the title WIP: feat v-let feat v-let Nov 27, 2022
@edison1105 edison1105 changed the title feat v-let feat: v-let Nov 27, 2022
@Tyh2001
Copy link

Tyh2001 commented Dec 2, 2022

This is a good idea, but the pure string method is not very friendly.

Maybe we can?

<h1 v-let.a="msg + ` Vue`">{{ a }}</h1>

@Justineo
Copy link
Member

Justineo commented Dec 2, 2022

Shall we align the design with petite-vue (which uses v-scope and an object carrying the local variables)?

@edison1105
Copy link
Member Author

Shall we align the design with petite-vue (which uses v-scope and an object carrying the local variables)?

Excellent advice. I will try it later.

@edison1105 edison1105 changed the title feat: v-let feat: v-scope Dec 3, 2022
@xieyuheng
Copy link

How is this PR now?

Really really really want to use petite-vue's v-scope in vue3 :)

@xieyuheng
Copy link

Is it possible to define v-scope as an user defined directive?

@xieyuheng
Copy link

Hi @edison1105, sorry to bug you ~

I want to ask, is there any timeline about when will this PR be merged?
I found myself visit this page every day,
really like this feature,
thanks for your work :)

@xieyuheng
Copy link

I care about this feature, because I recently taught a friend frontend programming.

He know a little js, but almost had no experience about html, css and reactive programming.

We used alpinejs for the teaching.

With alpine's x-data, the concept about reactive data is received very well.

But actually, we want to use vue, because alpine has no good component support.

This is why I care about this feature :)

Wish v-scope be supported soon, and we can teach in vue.

@edison1105
Copy link
Member Author

@xieyuheng
I'm not sure when will merge, sorry~
It needs to be discussed by the core team.

@mon-jai
Copy link

mon-jai commented Mar 23, 2023

Will the language server be updated to integrate v-scope with TypeScript?

@xieyuheng
Copy link

xieyuheng commented Mar 25, 2023

@edison1105

I found that I can use component to do the following:

components/Scope.vue:

<script setup lang="ts">
import { reactive } from 'vue'

defineProps<{ scope: any }>()
</script>

<template>
  <slot :scope="reactive(scope)" />
</template>

Example.vue:

<Scope :scope="{ count: 1 }" v-slot="{ scope }">
  <div>{{ scope.count }}</div>
  <button @click="scope.count++">add1</button>
</Scope>

@edison1105
Copy link
Member Author

@edison1105

I found that I can use component to do the following:

components/Scope.vue:

<script setup lang="ts">

import { reactive } from 'vue'



defineProps<{ scope: any }>()

</script>



<template>

  <slot :scope="reactive(scope)" />

</template>

Example.vue:

<Scope :scope="{ count: 1 }" v-slot="{ scope }">

  <div>{{ scope.count }}</div>

  <button @click="scope.count++">add1</button>

</Scope>

yep, mentioned in #7201 (comment)

@edison1105
Copy link
Member Author

Will the language server be updated to integrate v-scope with TypeScript?

Not sure

@100100101
Copy link

@xieyuheng
Based on your example, made a similar component

// Scope/index.vue

<script setup lang="ts">
import { reactive } from 'vue'
</script>
<template>
    <slot :="(reactive($attrs) as any)" />
</template>
// ForExampleMemoryStat/index.vue

<script lang="ts" setup>
import Scope from '@/shared/components/Scope/index.vue'
import { MemoryInfo, useMemory } from '@vueuse/core'
const { isSupported, memory } = useMemory()
const getBToMbString = (kb: number) => `${Math.ceil(kb / 1024 / 1024)} MB`
</script>
<template>
    <Scope
        v-if="isSupported && memory"
        :="memory"
        v-slot="{
            usedJSHeapSize,
            jsHeapSizeLimit,
            totalJSHeapSize,
        }: MemoryInfo"
    >
        <div class="grid grid-cols-2 grid-gap">
            <div>usedJSHeapSize</div>
            <div>{{ getBToMbString(usedJSHeapSize) }}</div>
        </div>
        <div class="grid grid-cols-2 grid-gap">
            <div>totalJSHeapSize</div>
            <div>{{ getBToMbString(totalJSHeapSize) }}</div>
        </div>
        <div class="grid grid-cols-2 grid-gap">
            <div>jsHeapSizeLimit</div>
            <div>{{ getBToMbString(jsHeapSizeLimit) }}</div>
        </div>
    </Scope>
</template>

Instead of

// ForExampleMemoryStat/index.vue

<script lang="ts" setup>
import { useMemory } from '@vueuse/core'
const { isSupported, memory } = useMemory()
const getBToMbString = (kb: number) => `${Math.ceil(kb / 1024 / 1024)} MB`
</script>
<template>
    <template v-if="isSupported && memory">
        <div class="grid grid-cols-2 grid-gap">
            <div>usedJSHeapSize</div>
            <div>{{ getBToMbString(memory.usedJSHeapSize) }}</div>
        </div>
        <div class="grid grid-cols-2 grid-gap">
            <div>totalJSHeapSize</div>
            <div>{{ getBToMbString(memory.totalJSHeapSize) }}</div>
        </div>
        <div class="grid grid-cols-2 grid-gap">
            <div>jsHeapSizeLimit</div>
            <div>{{ getBToMbString(memory.jsHeapSizeLimit) }}</div>
        </div>
    </template>
</template>

Of course, I understand that in this example the benefit is dubious in brevity. However, it may be useful to someone and in other scenarios it will be justified.

@github-actions
Copy link

github-actions bot commented Oct 20, 2023

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB 38 kB 34.2 kB
vue.global.prod.js 160 kB (+726 B) 58.1 kB (+239 B) 51.6 kB (+194 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.9 kB 18.3 kB 16.7 kB
createApp 55 kB 21.3 kB 19.4 kB
createSSRApp 59 kB 23 kB 20.9 kB
defineCustomElement 59.8 kB 22.8 kB 20.8 kB
overall 68.7 kB 26.3 kB 24 kB

@louiss0
Copy link

louiss0 commented May 23, 2024

I'm glad this feature is coming

@louiss0
Copy link

louiss0 commented May 23, 2024

I think v-scope should only be allowed in conditionals and blocks.
Allowing people to use v-scope anywhere will only allow abuse.
For that reason Svelte doesn't allow this.
image

I have a playground that demonstrates what I mean here.

@edison1105
Copy link
Member Author

@louiss0
the current behavior aligns with https://github.com/vuejs/petite-vue#usage
There may still be changes before the merge.

@louiss0
Copy link

louiss0 commented May 24, 2024

@louiss0 the current behavior aligns with https://github.com/vuejs/petite-vue#usage There may still be changes before the merge.

Please consider doing this change there is no point in being able to declare variables in the template with random tags. It makes code soo confusing!

@mon-jai
Copy link

mon-jai commented May 24, 2024

@louiss0 That could be a replacement for computed variables, but the variable is declared close to where it is used to improve readability.

Copy link

pkg-pr-new bot commented Oct 17, 2024

Open in Stackblitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@7218

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@7218

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@7218

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@7218

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@7218

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@7218

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@7218

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@7218

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@7218

vue

pnpm add https://pkg.pr.new/vue@7218

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@7218

commit: facd0e8

@edison1105 edison1105 marked this pull request as draft October 17, 2024 09:10
@edison1105 edison1105 marked this pull request as ready for review October 17, 2024 09:16
@edison1105 edison1105 added the ready for review This PR requires more reviews label Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review This PR requires more reviews version: minor
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

Variable assignment in loops in template
9 participants