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

Support for explicit import global components #13413

Closed
xiaoxiangmoe opened this issue Feb 24, 2022 · 13 comments · Fixed by nuxt/framework#4150
Closed

Support for explicit import global components #13413

xiaoxiangmoe opened this issue Feb 24, 2022 · 13 comments · Fixed by nuxt/framework#4150
Assignees

Comments

@xiaoxiangmoe
Copy link

xiaoxiangmoe commented Feb 24, 2022

Can we use

<script setup lang="ts">
import { NuxtLink } from 'some-nuxt-lib-location'
</script>
<template>
  <div>
    <nuxt-link :to="`/test/123`">
      Random path
    </nuxt-link>
  </div>
</template>

Additional context

We use https://eslint.vuejs.org/rules/no-undef-components.html and all component should be registered locally.

@danielroe
Copy link
Member

danielroe commented Feb 24, 2022

Would you provide a reproduction? I'm not sure what you mean.

@xiaoxiangmoe
Copy link
Author

Not a bug. It's a feature request or question.

In vue-router, we can use import { RouterLink, RouterView } from 'vue-router'

<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
</script>

<template>
  <header> 
    <div class="wrapper"> 
      <nav>
        <RouterLink to="/">Home</RouterLink>
        <RouterLink to="/about">About</RouterLink>
      </nav> 
  </header>
  <RouterView />
</template>

see

https://github.com/vuejs/create-vue-templates/blob/284fcf591115e5cf39e4461e2fd4cd2c5ea009bb/typescript-router/src/App.vue#L1-L2

Which lib should I import when I use <NuxtLink>?

Can I write import { NuxtLink } from 'nuxt3' or something else?

<script setup lang="ts">
import { NuxtLink } from 'nuxt3'
</script>

<template>
  <header> 
    <div class="wrapper"> 
      <nav>
        <NuxtLink to="/">Home</NuxtLink>
        <NuxtLink to="/about">About</NuxtLink>
      </nav> 
  </header> 
</template>

@lperez22
Copy link

You shouldn't have to import NuxtLink it will be auto imported by using the component. I have an example here https://stackblitz.com/edit/nuxt-starter-g2m6nk

@Atinux
Copy link
Member

Atinux commented Feb 25, 2022

You can learn more about the auto-import in https://v3.nuxtjs.org/concepts/auto-imports

@Atinux Atinux closed this as completed Feb 25, 2022
@danielroe
Copy link
Member

What do you think @pi0 - would it be useful to expose a #components import for directly importing from scanned components?

@xiaoxiangmoe
Copy link
Author

xiaoxiangmoe commented Feb 26, 2022

We use https://eslint.vuejs.org/rules/no-undef-components.html and all component should be registered locally. So I can't use auto import in our project.

@pi0
Copy link
Member

pi0 commented Feb 27, 2022

Seems a good idea @danielroe. We had same possibility with Nuxt2 (https://github.com/nuxt/components/blob/main/templates/components/index.js)

Would be nice if we could also expose possible component names as an array as well.

@pi0 pi0 reopened this Feb 27, 2022
@baryla
Copy link

baryla commented Feb 27, 2022

I think this will definitely be useful, especially for peeps using JSX components. At the moment, doing something like this actually works:

// App.tsx
import { defineComponent } from 'vue';

export default defineComponent(() => {
  return () => (
    <NuxtLayout>
      <NuxtPage />
    </NuxtLayout>
  )
});

but because there are no imports, TS actually complains with Cannot find name 'NuxtLayout'. ts(2304) etc. This I guess could be fixed with something like a .d.ts file and registering a global type for them but it seems like a hacky workaround to be honest.

@danielroe
Copy link
Member

@baryla FYI, there are global types registered in .nuxt/types/components.d.ts for auto-scanned components and also, for built-in Nuxt components: https://github.com/nuxt/framework/blob/dbab979a2ed28b8f3d02044079f6b9039114d5ff/packages/nuxt3/src/pages/runtime/router.ts#L18-L28.

@baryla
Copy link

baryla commented Feb 28, 2022

@danielroe thanks for the reply. I just checked my .nuxt/types/components.d.ts and all I have in there is:

// Generated by components discovery
declare module 'vue' {
  export interface GlobalComponents {
    'NuxtWelcome': typeof import("../../node_modules/nuxt3/dist/app/components/nuxt-welcome.vue")['default'],
    'ClientOnly': typeof import("../../node_modules/nuxt3/dist/app/components/client-only")['default']
  }
}
export {}

I'm not sure if I should turn anything on in the settings? My nuxt.config.ts file has no settings turned on.

When I created a component in /components, it did generate a global type for it however, TS still complained that no imports have been defined when I use it so I'm not sure if I'm doing something wrong or...?

btw. 👆 this is from a completely fresh installation of Nuxt.

@misaon
Copy link
Contributor

misaon commented Mar 6, 2022

+1

@zsilbi
Copy link

zsilbi commented Apr 14, 2023

My dev server hangs on specific routes on page reload when I import and use components imported from #components. It seems like a Vite issue, but if I import the same component from it's direct location the issue is gone. It also applies to built-in components like NuxtLink.

I was able to create a small reproduction of the issue, you can find it here: https://stackblitz.com/edit/nuxt-starter-azudzq?file=pages%2Findex.vue

@slacktracer
Copy link

Did you find a way to do it, @xiaoxiangmoe? Right now having to import things from #imports or #app is the worst thing about Nuxt for me. In particular it messes with auto sorting of imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants