Vue高级特性「七」--动态组件\异步组件***

使用场景:
需要根据数据(新闻详情页),动态渲染的场景,即组件类型不确定。

动态组件基本使用
<component :is="nextTickName"></component>

import nextTick from './nextTick'
export default {
  components:{
    nextTick,
  },
  data() {
    return {
      nextTickName:"nextTick"
    };
  },
  
};
用动态组件实现切换
<div id="example">
  <button @click="change">切换页面</button>
  <component :is="currentView"></component>
</div>

new Vue({
  el: '#example',
  data:{
    index:0,
    arr:[
      {template:`<div>我是页面A</div>`},
      {template:`<div>我是页面B</div>`},
      {template:`<div>我是页面C</div>`}
    ],
  },
  computed:{
    currentView(){
        return this.arr[this.index];
    }
  },
  methods:{
    change(){
      this.index = (++this.index)%3;
    }
  }
})
什么是异步组件?

异步组件就是定义的时候什么都不做,只在组件需要渲染(组件第一次显示)的时候进行加载渲染并缓存,缓存是以备下次访问。

Vue实现按需加载
  • import() 函数
  • 按需加载,异步加载大组件

注册方式:

components:{
  FromDemo:()=>import('../FromDemo')
}

组件调用方式:

<FromDemo v-if="show"/>
<button @click="show=true"></button>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3组件 `el-upload` 中,上传和删除是异步操作。为了保证数据的正确性,我们需要在上传和删除的回调函数中使用异步操作来更新文件列表。 下面是 `el-upload` 组件上传、删除异步处理的示例代码: ```vue <template> <el-upload class="upload-demo" action="/api/upload" :on-success="onSuccess" :on-remove="onRemove" :file-list="fileList" list-type="picture-card" :auto-upload="false"> <i class="el-icon-plus"></i> </el-upload> </template> <script> import { reactive } from 'vue' import axios from 'axios' export default { setup() { const state = reactive({ fileList: [] }) const onSuccess = async (response, file, fileList) => { // 上传成功的回调函数 console.log('上传成功') await updateFileList() } const onRemove = async (file, fileList) => { // 删除文件的回调函数 await axios.delete(`/api/file/${file.name}`) console.log('删除成功') await updateFileList() } const updateFileList = async () => { // 更新文件列表 const response = await axios.get('/api/files') state.fileList = response.data.files } // 组件挂载时,获取文件列表 updateFileList() return { state, onSuccess, onRemove } } } ``` 在这个示例代码中,我们使用了 `async/await` 来处理上传和删除操作的异步回调函数。在上传成功或者删除成功后,我们使用 `await` 关键字等待文件列表的更新完成。 在更新文件列表时,我们使用 `async/await` 来等待获取文件列表的异步操作完成。在获取文件列表的异步操作完成后,我们将返回的文件列表设置为 `fileList` 对象的值。 需要注意的是,在组件挂载时,需要获取文件列表并设置初始值。在这个示例代码中,我们将获取文件列表的操作封装到了 `updateFileList` 函数中,并在组件挂载时调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值