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

[Feature Request] Access to global configuration in setup context #1074

Closed
ayZagen opened this issue Apr 29, 2020 · 5 comments
Closed

[Feature Request] Access to global configuration in setup context #1074

ayZagen opened this issue Apr 29, 2020 · 5 comments

Comments

@ayZagen
Copy link

ayZagen commented Apr 29, 2020

What problem does this feature solve?

We may need to access global configured properties in setup(). As there is no this in the setup it would be great to access global configured properties from the SetupContext.

What does the proposed API look like?

Please have a look at the simple example below just to give an idea about the usage.

//main.ts
const app = createApp(Root);
app.config.globalProperties.someProp = "value";
//AnyComponent.ts
export default defineComponent({
  name: 'AnyComponent',
  setup(props, ctx){
    // access global defined property
    console.log(ctx.$config.someProp)
    return { }
  }
})
@ayZagen ayZagen changed the title Access to global configuration in setup context [Feature Request] Access to global configuration in setup context Apr 29, 2020
@yyx990803
Copy link
Member

config.globalProperties are meant as an escape hatch for replicating the behavior of Vue.prototype. In setup functions, simply import what you need or explicitly use provide/inject to expose properties to app.

@johnsoncodehk
Copy link
Member

johnsoncodehk commented Sep 15, 2020

@yyx990803 Seems unable to inject css module $style, has any alternative method?

@posva
Copy link
Member

posva commented Sep 15, 2020

@johnsoncodehk you have useCssModules and useCssVars 😉 They still have to be added to the docs.
Remember you can use the Discord chat to ask questions!

@johnsoncodehk
Copy link
Member

@posva I have been troubled by this problem for a long time! I always to use getCurrentInstance().proxy.$style before. Thanks a lot!!

@bhabgs
Copy link

bhabgs commented Apr 7, 2021

config.globalProperties are meant as an escape hatch for replicating the behavior of Vue.prototype. In setup functions, simply import what you need or explicitly use provide/inject to expose properties to app.

有没有什么方式能过改善这种写法?

import { defineComponent, getCurrentInstance } from 'vue';

export default defineComponent({
  setup() {
    const { proxy } = getCurrentInstance(); // 这样写会报告 proxy会错误,只能一步步写成如下方式
    const that = getCurrentInstance();
    const mitt = that?.proxy?.$mitt;
    return () => (
      <div
        class='test'
        onClick={() => {
          mitt?.emit('vite-tabspage-add', { a: 1 });
        }}
      >
        emit
      </div>
    );
  },
});

image

@github-actions github-actions bot locked and limited conversation to collaborators Oct 22, 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

5 participants