vue?內(nèi)置組件?component?的用法示例詳解
component is 內(nèi)置組件切換方法一:
component組件(單獨拿出一個組件來專門進(jìn)行切換使用)
使用is來綁定你的組件:如下面的reviewedPlan planDetailsList attachmentList等引入的組件名
changeViewFun 是用來切換組件的方法 通過給is綁定的currentView來實現(xiàn)切換組件
pathUrl就是當(dāng)前的路由
<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"); //父組件監(jiān)聽到changeview,給is綁定的currentView重新賦值
this.$router.push({
path: this.pathUrl, //通過props接收 props:{pathUrl:String}
query: {
id: params.row.id, //參數(shù)名
from:"reviewedPlan" //這里加from原因是要區(qū)分多個組件的情況下通過路由from參數(shù)來區(qū)分是通過那個組件切換過來的
}
})返回組件內(nèi)部方法 (點擊返回的時候執(zhí)行的操作)
var url = this.$route.query.from; //取路由from,區(qū)分是那個通過那個組件傳遞過來的,返回的時候可返回到對應(yīng)的組件
this.$emit("changeview",url);
this.$router.push({
path: this.pathUrl,
query: {
currentView:url,
}
})component is 內(nèi)置組件切換方法二:
實現(xiàn)的結(jié)果是:組件A調(diào)轉(zhuǎn)組件B,組件A里面有個查看按鈕,點擊查看,跳轉(zhuǎn)到組件B,組件B里面點擊返回跳轉(zhuǎn)到組件A,使用component,從組件A跳到組件B,在組件B里面刷新之后還是停留在組件B,還有就是點擊tab切換的時候也可以,點擊那個tab,當(dāng)前tab發(fā)請求。具體實現(xiàn):
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];//刪除參數(shù)
})
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); //修改參數(shù)方法
3、在組件內(nèi)部使用
說明:需要三個組件:第一個:component主控制組件、第二個:初始化組件內(nèi)容、第三個:跳轉(zhuǎn)過去的組件
第一個:studentIndex.vue
<template>
<component
:is="viewName"
@updateView="updateView"
>
</component>
</template>
<script>
import studentGrowthPortfolio from './studentGrowthPortfolio.vue'; //學(xué)生 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:{
/**
* 接收子組件數(shù)據(jù)
* @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,點擊查看需要執(zhí)行的代碼
click: () => {
this.$emit("updateView","fileDetails");
this.$routePlugin.addQuery({
viewName:'fileDetails',
identity:'student'
})
}5、第三個:fileDetails.vue,點擊返回時需要執(zhí)行的代碼
click:()=>{
this.$emit('updateView', 'studentGrowthPortfolio')
}fileDetails.vue添加beforeDestoy,當(dāng)離開當(dāng)前組件時,銷毀路由上的identity,和viewName參數(shù)
beforeDestroy(){
this.$routePlugin.delQuery('identity','viewName')
},到此這篇關(guān)于vue內(nèi)置組件component的用法的文章就介紹到這了,更多相關(guān)vue內(nèi)置組件component內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中mixins的使用方法以及實際項目應(yīng)用指南
vue中提供了一種混合機(jī)制--mixins,用來更高效的實現(xiàn)組件內(nèi)容的復(fù)用,下面這篇文章主要給大家介紹了關(guān)于Vue中mixins的使用方法以及實際項目應(yīng)用指南,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Vue3后臺管理系統(tǒng)之創(chuàng)建和配置項目
后臺管理系統(tǒng)是我們?nèi)粘i_發(fā)學(xué)習(xí)經(jīng)常遇到的一個項目,下面這篇文章主要給大家介紹了關(guān)于Vue3后臺管理系統(tǒng)之創(chuàng)建和配置項目的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
element-ui el-dialog嵌套table組件,ref問題及解決
這篇文章主要介紹了element-ui el-dialog嵌套table組件,ref問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
vue大屏自適應(yīng)的實現(xiàn)方法(cv就能用)
最近在用VUE寫大屏頁面,遇到屏幕自適應(yīng)問題,下面這篇文章主要給大家介紹了關(guān)于vue大屏自適應(yīng)的實現(xiàn)方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
vue3.0 Reactive數(shù)據(jù)更新頁面沒有刷新的問題
這篇文章主要介紹了vue3.0 Reactive數(shù)據(jù)更新頁面沒有刷新的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

