vue动态组件component原理_component注解

vue动态组件component原理_component注解componentis内置组件切换方法一:component组件(单独拿出一个组件来专门进行切换使用)使用is来绑定你的组件:如下面的reviewedPlanplanDetailsListattachmentList等引入的组件名changeViewFun是用来切换组件的方法通过给is绑定的currentView来实现切换组件pathUrl就是当前的路由…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

component is 内置组件切换方法一:

component组件(单独拿出一个组件来专门进行切换使用)

使用is来绑定你的组件:如下面的reviewedPlan  planDetailsList   attachmentList等引入的组件名

changeViewFun 是用来切换组件的方法 通过给is绑定的currentView来实现切换组件

pathUrl就是当前的路由

 
<template>
    <div class="reviewed">
        <component
            :is="currentView"
            @changeview="changeViewFun"
            :pathUrl="pathUrl"
        ></component>
    </div>
</template>
<script>
     //引入三个需要切换的组件
    import reviewedPlan from '../modules/reviewedPlan.vue';
    import planDetailsList from './planDetailsList';
    import attachmentList from './attachmentList.vue';
    export default {
        name: "reviewed",
        data() {
            return {
                currentView:'reviewedPlan',
                pathUrl:'',
                hrefIndex:"",
            }
        },
        components: {
            reviewedPlan,
            planDetailsList,
            attachmentList
        },
        created () {
              this.hrefIndex=window.location.href.indexOf('jxjh')-1;
              this.pathUrl=window.location.href.substring(this.hrefIndex);
              if(this.$route.query.currentView){
                  this.$route.query.currentView = this.$route.query.currentView===this.currentView?this.$route.query.currentView:this.currentView;
              }
          },
        methods:{
          //组件切换方法
            changeViewFun(val){
                this.currentView = val;
            }
        },
    }
</script>
<style lang="less" scoped>
    @import "~@/libs/less/theme/theme.less";

</style>

每个切换的组件

this.$emit("changeview","planDetailsList");  //父组件监听到changeview,给is绑定的currentView重新赋值
this.$router.push({
       path: this.pathUrl,  //通过props接收  props:{pathUrl:String}
       query: {
          id: params.row.id,   //参数名
          from:"reviewedPlan"  //这里加from原因是要区分多个组件的情况下通过路由from参数来区分是通过那个组件切换过来的
       }
 })

返回组件内部方法  (点击返回的时候执行的操作)

var url =  this.$route.query.from;  //取路由from,区分是那个通过那个组件传递过来的,返回的时候可返回到对应的组件
this.$emit("changeview",url);
this.$router.push({
      path: this.pathUrl,
      query: {
             currentView:url,
        }
})

component is 内置组件切换方法二:

实现的结果是:组件A调转组件B,组件A里面有个查看按钮,点击查看,跳转到组件B,组件B里面点击返回跳转到组件A,使用component,从组件A跳到组件B,在组件B里面刷新之后还是停留在组件B,还有就是点击tab切换的时候也可以,点击那个tab,当前tab发请求。具体实现:

1、封装routePlugin.js插件

const addQuery=function(queryDate){
    var query={};
    Object.assign(query,this.$route.query,queryDate);
    this.$router.push({
        path:this.$route.path,
        query:query
    });
};
const delQuery=function(){
    var query={};
    var arg=Array.prototype.slice.call(arguments);
    Object.assign(query,this.$route.query);
    arg.forEach(item=>{
        delete query[item];//删除参数
    })
    this.$router.push({
        path:this.$route.path,
        query:query
    });
};
var install = {
    install(Vue) {
        Vue.mixin({
            beforeCreate() {
                var self=this;
                this.$routePlugin={
                    addQuery:addQuery.bind(self),
                    delQuery:delQuery.bind(self)
                }
            }
        })
    }
}
export default install;

2、在main.js中注册到全局,
            import routePlugin from “./libs/js/vueExtend/routePlugin.js”;

            Vue.use(routePlugin); //修改参数方法

3、在组件内部使用

    说明:需要三个组件:第一个:component主控制组件、第二个:初始化组件内容、第三个:跳转过去的组件

第一个:studentIndex.vue

<template>
    <component
        :is="viewName"
        @updateView="updateView"
    >
    </component>
</template>
<script>

import studentGrowthPortfolio from './studentGrowthPortfolio.vue';  //学生 index
import fileDetails from './fileDetails.vue';  //成长档案 详情
export default {
    data(){
        return{
            viewName:"studentGrowthPortfolio",
        }
    },
    components:{
        studentGrowthPortfolio,
        fileDetails
    },
    mounted(){
        this.viewName=this.$route.query.viewName?this.$route.query.viewName:this.viewName;
    },
    created () {
    },
    methods:{
        /**
         * 接收子组件数据
         * @param data {Object}
         * @return {Void} 无
         */
         updateView(name){
             this.viewName = name
             if(!name){
                 this.$routePlugin.delQuery('viewName');
             }else{
                 this.$routePlugin.addQuery({viewName:name});
             }
         },
    },
}
</script>
<style scoped lang="less">
    @import "~@/libs/less/theme/theme.less";

</style>

4、第二个:studentGrowthPortfolio.vue,点击查看需要执行的代码

click: () => {
        this.$emit("updateView","fileDetails");
        this.$routePlugin.addQuery({
               viewName:'fileDetails',
               identity:'student'
          })
 }

5、第三个:fileDetails.vue,点击返回时需要执行的代码

click:()=>{
     this.$emit('updateView', 'studentGrowthPortfolio')
}

fileDetails.vue添加beforeDestoy,当离开当前组件时,销毁路由上的identity,和viewName参数

beforeDestroy(){

            this.$routePlugin.delQuery(‘identity’,’viewName’)

 },

一切都ok了

 交流

1、QQ群:可添加qq群共同进阶学习: 进军全栈工程师疑难解  群号:   856402057

2、公众号:公众号「进军全栈攻城狮」 ,对前端技术保持学习爱好者。我会经常分享自己所学所看的干货,在进阶的路上,共勉

vue动态组件component原理_component注解

红包天天领

vue动态组件component原理_component注解              vue动态组件component原理_component注解

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193028.html原文链接:https://javaforall.net

(0)
上一篇 2025年12月3日 下午3:22
下一篇 2025年12月3日 下午4:01


相关推荐

  • SpringBoot登录用户权限拦截器「建议收藏」

    SpringBoot登录用户权限拦截器「建议收藏」1.创建自定义拦截器类并实现HandlerInterceptor接口packagecom.xgf.online_mall.interceptor;importcom.xgf.online_mall.system.domain.User;importlombok.extern.slf4j.Slf4j;importorg.springframework.stereotype.Component;importorg.springframework.web.servlet.Handler

    2022年7月17日
    20
  • redis 乐观锁_数据库乐观锁实现

    redis 乐观锁_数据库乐观锁实现文章目录GeospatialHyperloglogBitmapsRedis事务悲观锁和乐观锁JedisSpringboot继承RedisGeospatial存储地理位置的数据结构应用场景朋友的定位,附近的人,打车距离计算Geospatial底层使用的是Zset127.0.0.1:6379> geoadd city 116.23 40.22 beijing 添加一个数据127.0.0.1:6379> geoadd city 121.47 31.23 shanghai 118.77

    2022年8月9日
    4
  • 如何保证缓存与数据库的双写一致性?

    作者 | 你是我的海啸 来源 | https://blog.csdn.net/chang384915878 分布式缓存是现在很多分布式应用中必不可少的组件,但是…

    2021年6月22日
    112
  • laaS 、paaS和SaaS区别

    laaS 、paaS和SaaS区别laaS 基础设施服务 Infrastructu as a service 比如数据库服务 paaS 平台服务 Platform as a service 比如别人的设备接入到我们的平台 客户在我们平台基础上做客户端 这种就叫做提供 paaS 服务 SaaS 软件服务 Software as a service 比如别人的设备接入到我们的平台 同时也使用我们开发的客户端 客户不用管技术问题 这种就叫做提供 saaS 服务 我们日常使用的微信 钉钉都是第三方为我们提供的 saaS 服务

    2026年3月19日
    3
  • IMA和腾讯元宝在技术架构上有何区别?

    IMA和腾讯元宝在技术架构上有何区别?

    2026年3月12日
    2
  • 分布式架构php,php分布式架构

    分布式架构php,php分布式架构一起来谈谈 php 分布式结构有什么具体的实现的方式 这个值得讨论哦 回复内容 一起来谈谈 php 分布式结构有什么具体的实现的方式 这个值得讨论哦 举个例子 比如你有一个完整的系统 系统有几个大模块 一般小的规划是几个小模块都在同一台 WEB 服务器上 那么等 PV UP 上来的时候 压力扛不住了 那么可以做适当的拆分 一个模块一台服务器 模块之间用 RPC 框架来实现数据间的共享 分布式一句话概括 在保证整个

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号