Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that import () syntax cannot pass in variables in vue

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to solve the problem that import () syntax cannot be passed into variables in vue". In daily operation, I believe many people have doubts about how to solve the problem that import () syntax cannot be passed into variables in vue. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt that "import () syntax can not be passed into variables in vue". Next, please follow the editor to study!

Import () syntax cannot pass in variable solution

When you must use variables, you can provide some information to webpack through a string template.

For example, import (`. / path/$ {myFile}`), so that all modules under. / path will be compiled at compile time, but will not be loaded until the value of myFile is determined at runtime, thus lazy loading can be achieved.

Import (`. / path/$ {myFile}`), problem

Can be used

Const cc = () = > import ('. / aa.vue');

The following code reports an error

Let name ='@ / views/aa.vue';const cc = () = > import (name)

Or

Function jikj () {return'. / gg.js';} const cc = () = > import (jikj ()); dynamic introduction of import () variable invalidation

Our usual usage of import is

Import ('@ / pages/demo') .then (item= > {})

But now there is a requirement that must be passed into the path dynamically. It is found that it cannot be recognized after passing in variables. The code is as follows

Const modelpath ='@ / pages/demo'import (modelpath) .then (item = > {})

Because the current implementation of webpack cannot be completely dynamic, you can provide some information to webpack through a string template, as follows

Const modelpath ='/ demo'import (`@ / pages$ {modelpath}`). Then (item = > {}), this is the end of the study on "how to solve the problem that import () syntax cannot be passed into variables in vue", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report