Utilizing dynamic imports with Vue files in TypeScript in a recursive manner

I ran into an issue while trying to dynamically import a single file vue component within itself in TypeScript. The error message I received was:

/Users/lilei/Desktop/designer-web/node_modules/fork-ts-checker-webpack-plugin/lib/service.js:22
        throw error;
        ^

RangeError: Maximum call stack size exceeded
    at getSymbolLinks (/Users/lilei/Desktop/designer-web/node_modules/typescript/lib/typescript.js:31794:32)
    at getDeclaredTypeOfTypeAlias (/Users/lilei/Desktop/designer-web/node_modules/typescript/lib/typescript.js:36457:25)

...

The file in question is named myComp.vue

<template>
  <div>
    <my-comp v-if="someCondition"></my-comp>
  </div>
</template>
<script lang="ts">
export default Vue.extend({
  data(){
    return{};
  },
  components:{
    myComp:()=>import('./myComp.vue')//Which is the name of current file, this is a official usage for dynamic loading vue component, and every thing is right in native JavaScript(not in TypeScript)
  }
})
</script>

It seems that there are recursive tasks being executed during TypeScript compilation, leading to the error. However, I have ensured that the component does not infinitely nest itself by controlling the someCondition in my logic. How can I dynamically import a .vue file recursively in TypeScript?

vue doc

Answer №1

When you include myComp within itself, it leads to an endless recursion resulting in a StackOverflow error.

To resolve this issue, you can assign a name to your component and refer to it by that name without the need for importing the component again. Here is an example:

 <template>
  <div>
   <my-comp v-if="someCondition"></my-comp>
  </div>
 </template>


<script lang="ts">
 export default Vue.extend({
   name: 'my-comp',
   data(){
    return{};
   },
   components:{
   }
 })
</script>

Additionally, consider changing Vue.extend to Vue.component.

Answer №2

Global registration of the component is essential in order to use it by its assigned name.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Add a unique class to an element within a main component

Within our application root, there exists a sidebar consisting of a list. We utilize uiSrefActive to apply an active class to the selected list item upon clicking it. However, once you navigate away from that component, the original list item loses its a ...

I encountered a TypeError [ERR_UNKNOWN_FILE_EXTENSION] while trying to run the express server. It seems to be caused by an unfamiliar file extension ".ts"

As I set up my Express server with a Postgres database connection, I realized that my understanding of how the compiler handles Typescript is still at a novice level. Upon starting the server, I encountered the following error: TypeError [ERR_UNKNOWN_FI ...

Error message indicating that `vue-loader` could not be located when attempting to run `npm run

I keep encountering an issue when trying to run npm run dev. I'm getting the following error and have no idea how to resolve it. I've tried reinstalling Node.js to the latest version and reinstalling all packages via the command line: [webpack-cl ...

Troubleshooting a Vue.js formatting problem in Visual Studio 2019

Encountering an issue with VS2019 while attempting to format this code section. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="milestone.ascx.cs" Inherits="uc.dms.milestone" %> <section class="content-header"> <h1> ...

In my Vue application, I've noticed that when I implement a select tag, the drop-down icon mysteriously vanishes in Chrome

I'm currently working on a Vue.js project with Vuetify. However, I've encountered an issue where the drop-down icon disappears when using a select tag. <template> <select class="select"> <option v-for="item ...

Encountered difficulties when trying to incorporate SCSS into my rollup build

Desired Outcome I aim to develop a small library for personal use, focusing on separating code into library (product) and application (project) code. All my source code resides in the /src folder, consisting of React, TypeScript, and SCSS code. I would l ...

When attempting to access a specific video, the link redirects to a different URL where the video automatically begins playing. However, the server does not return the expected "disposable content type."

Is there a way to download videos with just one click? I've tried creating a Button and attaching a Function for triggering the download, but all it does is download the link of the video, not the video itself. I can easily download videos using an ex ...

Determine the appropriate data type for an object property using its key

I have a constant object with known keys. Each item within the object may or may not have a specific property. I need a function that, when given a key, will return the value along with its corresponding type. My development environment is TypeScript 4.6. ...

Ensuring Data Consistency: Using TypeScript to Strongly Type Arrays with Mixed Variable Types

I have a JSON array that may contain objects of two types, defined by IPerson and ICompany. [ { "Name" : "Bob", "Age" : 50, "Address": "New Jersey"}, { "Name" : "AB ...

What is the best approach for developing an npm package containing multiple Vue directives? Should each directive have its own separate package, or should they

While I have successfully created an npm package by exporting a single vue directive in the src/index.js file, I am now faced with the challenge of creating a package that allows for the use of multiple vue directives. Unfortunately, I have been unable t ...

Working with Yarn and Webpack: Incorporating a TypeScript .ts file from an external Node directory not controlled by Yarn/Webpack

I am currently working on a k6 project for load testing, utilizing Yarn and Webpack. This project is stored within a sub-folder of a larger repository that primarily uses npm Node modules. My goal is to access a secret from AWS's Secrets Manager in t ...

The use of '-' in v-bind:style within Vue.js

I'm having trouble figuring out how to use CSS code with dashes in v-bind:style. When I attempt something like this: <DIV style="width:100px;height: 100px;background-color: red;cursor: pointer;" v-bind:style="{ margin-left: margin + 'px' ...

Extracting decimal numbers from JSON data

I am currently facing an issue in my Angular project where I am attempting to read a JSON file. This file is stored in the assets folder and contains decimal values such as: { "valueA": 0.40000000000002, "valueB": 23.999999999999999 ...

Exploring Angular and Typescript - attempting to adjust cursor position for multiple child elements within a contenteditable div despite researching numerous articles on the topic

I could use some assistance in resolving this issue. Here is the stackblitz code I am currently working with If you have any workarounds, please share them with me. The caret/cursor keeps moving to the starting position and types backward. Can anyone hel ...

Is there a way to customize the text color highlight for a specific react component only?

I'm working with React and need to adjust the text highlight color. Initially, I tried this approach: Highlight.css ::selection { background:#fbc308; color: black } somecomponent.tsx import './highlight.css' However, this ende ...

The Element type does no feature a Typescript property

Despite my attempts to include a declaration file and various other solutions, I'm still struggling with this issue: The goal is to define the visible property as a function on the HTML Element object. However, the linter keeps flagging visible with ...

What could be the reason behind the Vue property being globally undefined in the component at the time of mounting?

Creating a custom Vue plugin was essential for me, which establishes a global property on Vue in this way: function (Vue, options) { Vue.$detector = new TranslatableStringDetector(); } Utilizing it within a computed property in my component is done l ...

What circumstances allow @Inject to be optional in Angular?

My Angular service is simple yet causing errors. Here's the code snippet: // service.ts export class SimpleService { // ... } // component.ts @Component({ selector: 'my-component', templateUrl: 'components/mycomp/mycomp.ht ...

The element is implicitly assigned an 'any' type as the expression of type 'any' cannot be used to index a type with createStyles

My stylesheet looks like this: const badgeStyle = createStyles({ badge: { borderRadius: "12px", padding: "5px 12px", textTransform: "uppercase", fontSize: "10px", fontWeight: 700, lineHeight ...

The styles from bootstrap.css are not displaying in the browser

Currently in the process of setting up my angular 2 project alongside gulp by following this helpful tutorial: I've added bootstrap to the package.json, but unfortunately, it's not reflecting in the browser. I can see it in the node_modules and ...