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

How to use app.config.globalProperties.xxx in setup? #2233

Closed
XingXiaoWu opened this issue Sep 25, 2020 · 2 comments
Closed

How to use app.config.globalProperties.xxx in setup? #2233

XingXiaoWu opened this issue Sep 25, 2020 · 2 comments

Comments

@XingXiaoWu
Copy link

Version

3.0.0

Reproduction link

https://github.com/XingXiaoWu/vue-question

Steps to reproduce

1.main.js

app.config.globalProperties.name = '123';

2.APP.vue

<template>
  <img alt="Vue logo" src="./assets/logo.png">
  <button @click="touchme">touchme</button>
</template>

<script>
import { getCurrentInstance } from 'vue';

export default {
  name: 'App',
  components: {
  },
  setup() {
    const { ctx } = getCurrentInstance();

    const touchme = () => {
      console.log(ctx.name);
    };
    return {
      touchme,
    };
  },
};
</script>

What is expected?

Build can print the name later

What is actually happening?

ctx.name is undefiend


In the url globalproperties .

It tells how it is used in the mounted.

but don't say how to use in setup.

If convenient, can you document how to use it in setup

@Shouhua
Copy link

Shouhua commented Sep 25, 2020

here is my code(https://codesandbox.io/s/youthful-gauss-h452m?file=/src/components/Greet.vue), use "proxy" can work, "ctx" is internal property of "ComponentInternalInstance".
In addition, pay attention to local variable name conflicts, local variables have higher priority, uncomment local variable will print local variable

@posva
Copy link
Member

posva commented Sep 25, 2020

Hi, thanks for your interest but Github issues are for bug reports and feature requests only. You can ask questions on the forum, the Discord server or StackOverflow.


The global properties are meant to be used by the option api. Create useSomething() functions like vue-router's useRoute() for setup usage instead of using getCurrentInstance() vuejs/rfcs#117

@posva posva closed this as completed Sep 25, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Nov 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants