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

