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 use vue dynamic components

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

Share

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

This article mainly introduces "how to use vue dynamic components". In daily operation, I believe many people have doubts about how to use vue dynamic components. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use vue dynamic components". Next, please follow the editor to study!

1. Basic use

Create a new component Article.vue

Thirty miles southeast of Huangzhou is Shahu, also known as Luoshi Dian. During the period of buying land, he got sick when he went to Xiangtian.

Pang an, a native of Wen Maqiao, is often good at medicine and deaf. So he went to seek treatment.

Although Anchang is deaf, and Yingwu is a wonderful person, he draws words on paper, but the book does not have numbers, which is very popular.

Yu Opera said: "I take the hand as the mouth, the king takes the eye as the ear, all are strangers for a while."

Jiyu, visit Qingquan Temple together with it.

Temple in Qishui Guo Gate two miles outside, there are Wang Yishao wash pen spring, the water is extremely sweet, under the Lanxi, the stream flows to the west.

Yu Zuoyun said: "the orchid sprouts at the foot of the mountain are short in the stream, the sand road between the pines is clean, and the rustling twilight rain is crowing."

Who says there is nothing less in life? When you see that running water can still be used to the west, Hugh sings the yellow chicken with white hair. "

I came home from a Japanese drama.

Create a new component Poetry.vue

Spring night

Su Shi

The spring night is worth thousands of gold, and the flowers have fragrance and the moon is overcast.

The sound of the singing tube tower is thin, and the night is heavy in the swing yard.

Create a new Learn.vue

And introduce Article.vue and Poetry.vue into Learn.vue

Learn.vue, Article.vue and Poetry.vue in this article are in the same folder.

Modify the component import Article from'. / Article.vue'import Poetry from'. / Poetry.vue'export default {components: {Article, Poetry}, data () {return {currentComponent: 'Article'}}, methods: {changeComponent () {this.currentComponent =' Poetry'}

Dynamic components, that is, use the component tag to switch between different components by the name specified by the is attribute

Running effect

2. Use with keep-alive

Keep-alive can maintain the state of these components, and if you need to maintain the state of the components, you need to use them with keep-alive

First look at the situation where you need to stay in state instead of using keep-alive.

Create a new Manuscript.vue

Modify Learn.vue

Poetry contribution import Poetry from'. / Poetry.vue'import Manuscript from'. / Manuscript.vue'export default {components: {Poetry, Manuscript}, data () {return {currentComponent: 'Poetry'}} Methods: {changeComponent (type) {if (type = = 1) {this.currentComponent = 'Poetry'} if (type = = 2) {this.currentComponent =' Manuscript'}

Running effect

If you look at the running effect, you will find that after entering the text in the manuscript, you will switch back to the poem, and then go back to the manuscript, the previous manuscript information will be gone.

The reason for this is that every time you switch a new component, Vue creates a new component. Therefore, if you need to save the original component information, use it with keep-alive

Learn.vue after adding keep-alive

Use tags to wrap dynamic components

Poetry contribution import Poetry from'. / Poetry.vue'import Manuscript from'. / Manuscript.vue'export default {components: {Poetry, Manuscript}, data () {return {currentComponent: 'Poetry'}} Methods: {changeComponent (type) {if (type = = 1) {this.currentComponent = 'Poetry'} if (type = = 2) {this.currentComponent =' Manuscript'}

Running effect

At this point, the study on "how to use vue dynamic components" is over. I hope to be able to solve your 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