1024programmer Blog New components of VUE3.0_Xavier-Xiao’s Blog

New components of VUE3.0_Xavier-Xiao’s Blog

class=”markdown_views prism-atom-one-dark”>

1.Fragment (fragment)

  • In Vue2: Components must have a root tag
  • In Vue3: components can have no root tag, and multiple tags will be included in a Fragment virtual element
  • Benefits: Reduce label levels, reduce memory usage
<template
     h2aaaa</h2>
     h2aaaa</h2>
 </template
 

2. Teleport

  • Teleport provides a clean way to insert and display the html of the component under a specific label (probably body) outside the interface of the parent component

2.1App.vue

<template
   h2App</h2>
   modal-button</modal-button
 </template

 script lang="ts"
 import ModalButton from './ModalButton.vue'

 export default {
      
   setup() {
      
     return {
      
     }
   },

   components: {
      
     ModalButton
   }
 }
 </script>
 

2.2 Subcomponents

<template
   h1Subcomponent</h1>
   button @click  ="modalOpen = true">Display dialog box</button  span>
   
   Teleport to="body"
     div class="modal" v-if=  "modalOpen"
       pDialog box</p>
       button @click  ="modalOpen = false"Close dialog</button
     </div
   </Teleport
 </template

 script
 import {
       ref } from "  vue";

 function useFeatureX(foo) {
      
   const length = computed(() =>  {
      
     return foo.value * 2;
   });

   return length;
 }
 export default defineComponent({
      
   name: "test component",
   props: {
      },
   setup(props, {
       attrs, emit, slots }) {
      
     //Control dialog box display or hide
     let modalOpen = ref(false);
     return {
      
       modalOpen,
     };
   },
   mounted() {
      },
   methods: {
      },
 });
 </script>
 

3.Suspense (not sure)

  • They allow our application to render some fallback content while waiting for an asynchronous component, allowing us to create a smooth user experience

3.1App.vue

<template
   Suspense
     template v-slot:default
       AsyncComp
       <!--  -->
     </template

     template v-slot:fallback
       h1LOADING...</h1
     </template
   </Suspense
 </template

 script lang="ts"
 /*
 Asynchronous Components + Suspense Components
 */
 // import synchronously
 // import AsyncComp from './AsyncComp.vue';
 // import synchronously
 import AsyncAddress from './AsyncAddress.vue';
 import {
       defineAsyncComponent } from '  vue';
 // Vue3.0 dynamically imports component writing method
 const AsyncComp = defineAsyncComponent(() =>  import('./AsyncComp.vue'));
 export default {
      
   setup() {
      },
   components: {
      
     AsyncComp,
     AsyncAddress
   }
 }
 </script>
 

3.2AsyncComp.vue

<template
   h2AsyncComp22</h2>
   p{
    {msg}}</p>
 </template

 script lang="ts"

 export default {
      
   name: 'AsyncComp',
   setup () {
      
     // return new Promise((resolve, reject) => {
      
     // setTimeout(() => {
      
     // resolve({
      
     // msg: 'abc'
     // })
     // }, 2000)
     // })
     return {
      
       msg: 'abc'
     }
   }
 }
 </script>
 

3.3AsyncAddress.vue

<template
 h2{
    {data}}</h2>
 </template

 script lang="ts"
 import axios from 'axios'
 export default {
      
   async setup() {
      
     const result = await axios.get('/data/address  .json')
     return {
      
       data: result.data
     }
   }
 }
 </script>
 

></template

script lang=ts
import axios from ‘axios’
export default {

async setup() {

const result = await axios.get(‘/data/address .json’)
return {

data: result.data
}
}
}
</script>

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/new-components-of-vue3-0_xavier-xiaos-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索