vue 内置组件 component 的用法

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>` 的参数传递 在 Vue 中,动态组件 `<component>` 可以通过 `:is` 属性来指定要渲染的具体组件名称。为了向动态组件传递参数,可以利用 Vue 提供的标准属性绑定机制实现数据传输。 以下是具体的实现方式: #### 使用 `v-bind` 或者简写语法 `:` 进行参数传递 可以通过 `v-bind` 将父级的数据作为 props 传递到子组件中。例如,在模板部分可以直接使用以下代码完成参数传递[^1]: ```vue <template> <keep-alive> <!-- 绑定 is 属性并传递 parentProp 参数 --> <component :is="currentComponent" :parent-prop="parentProp"></component> </keep-alive> </template> <script> export default { props: { parentProp: { type: String, required: true } }, data() { return { currentComponent: 'YourDynamicComponent' // 当前显示的动态组件名 }; } }; </script> ``` 在此示例中,`:is` 被用来决定当前应该加载哪个组件,而 `:parent-prop` 则用于将来自父组件的数据传递至目标动态组件。 #### 关于 `keep-alive` 缓存的作用 如果希望减少不必要的重新渲染操作,则可以在外部嵌套一层 `<keep-alive>` 来缓存那些未被激活但仍需保留状态的组件实例[^3]。这有助于提升性能表现,尤其是在频繁切换多个相似功能模块的情况下尤为有用。 #### 配合路由使用的场景说明 尽管本案例并未涉及具体路径配置相关内容,但在实际开发过程中,有时也会结合 vue-router 实现更复杂的交互逻辑[^4]。不过需要注意的是,此时应区分清楚两者之间的职责边界——前者负责局部区域内的灵活变化处理;后者则主要针对全局导航层面提供支持服务。 综上所述,上述方法展示了如何借助标准 prop 系统以及特殊指令(如 `:is`),轻松达成跨不同类型的 Vue 子部件间的信息共享需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值