当前位置:网站首页>Vue - Advanced Vue router routing (2) (replace attribute, programming route navigation, caching route components, and exclusive hooks for routes)
Vue - Advanced Vue router routing (2) (replace attribute, programming route navigation, caching route components, and exclusive hooks for routes)
2022-06-12 18:48:00 【-Baymax-】
List of articles
One 、router-link Of replace attribute
1. effect
Controls the mode of operating browser history when routing jumps .
2. Two ways to write
- push: Append history .( default setting )
- replace: Replace current record .
3. Turn on replace Pattern
// Complete writing :
<router-link :replace="true" ...>News</router-link>
// Abbreviation :
<router-link replace ...>News</router-link>
4. example :【 The horns of a dilemma 】
replace Pattern
App.vue
- replace There are two ways to write , Generally use the abbreviation .
<router-link :replace="true" class="list-group-item" active-class="active" :to="{name: 'guanyu'}">About</router-link>
<router-link replace class="list-group-item" active-class="active" to="/home">Home</router-link>
Two 、 Programming route navigation
1. effect
- Don't use
<router-link>Implement route jump , Make routing jump more flexible .- In addition to
<a>, Other tags can also use route navigation ( Such as :button etc. )
2. Specific code
push
this.$router.push({
name: 'xiangqing',
params: {
id: xxx,
title: xxx
}
})
replace
this.$router.replace({
name: 'xiangqing',
params: {
id: xxx,
title: xxx
}
})
Forward
this.$router.forward()
back off
this.$router.back()
Forward or backward ( Custom steps )
this.$router.go(n) //n Integers ( just / negative )
3. example : Use the button to jump
Programming route navigation
Banner.vue
<div class="page-header"><h2>Vue Router Demo</h2></div>
<button @click="back"> back off </button>
<button @click="forward"> Forward </button>
<button @click="test"> Two steps back </button>
methods: {
back() {
this.$router.back();
},
forward() {
this.$router.forward();
},
test() {
this.$router.go(-2);
}
}
Message.vue
<button @click="pushShow(m)">push</button>
<button @click="replaceShow(m)">replace</button>
methods:{
pushShow(m) {
this.$router.push({
name: "xiangqing",
params: {
id: m.id,
title: m.title,
}
})
},
replaceShow(m) {
this.$router.replace({
name: "xiangqing",
params: {
id: m.id,
title: m.title,
}
})
}
}
3、 ... and 、 Cache routing components
1. effect
By default , Routing components that are not displayed will be automatically destroyed , Caching the routing components is to keep the routing components that are not displayed mounted , Not destroyed .
2. Specific code
Use
<keep-alive>label , Note that it is written in the component for presentation .
include="myNews"To say only to News Component cache ,myNewsIt's defined News Component's name name .
// Cache a routing component
<keep-alive include="myNews">
<router-view></router-view>
</keep-alive>
// Cache multiple routing components
<keep-alive :include="['myNews', 'myMessage']">
<router-view></router-view>
</keep-alive>
3. example : Cache for specified routes
The routing cache
News.vue
<template>
<ul>
<li>news001 <input type="text"></li>
<li>news002 <input type="text"></li>
<li>news003 <input type="text"></li>
</ul>
</template>
<script>
export default {
name: "myNews",
beforeDestroy(){
console.log('New Component is about to be destroyed ');
}
};
</script>
Home.vue
- If not
<keep-alive>Middle configuration include attribute , that Home All routes shown in the component will be cached .- adopt
include='xxx', We can specify the route to cache .
<template>
<div>
<h2>Home Component content </h2>
<div>
<ul class="nav nav-tabs">
<li>
<router-link class="list-group-item" active-class="active" to="/home/news">News</router-link>
</li>
<li>
<router-link class="list-group-item" active-class="active" to="/home/message">Message</router-link>
</li>
</ul>
<keep-alive include="myNews">
<router-view></router-view>
</keep-alive>
</div>
</div>
</template>
<script>
export default {
name: "myHome",
};
</script>
Four 、 Two new life cycle hooks
1. effect
Routing components
UniqueTwo hooks , Used to capture the activation status of routing components .
2. Specific name
- activated: Routing component is
Activation timeTrigger .- deactivated: Routing component
When inactivatedTrigger .
3. example : The route is not destroyed and the timer is deactivated
Two hooks for routing
News.vue
<template>
<ul>
<li :style="{opacity}"> Study carefully Vue</li>
<li>news001 <input type="text"></li>
<li>news002 <input type="text"></li>
<li>news003 <input type="text"></li>
</ul>
</template>
<script>
export default {
name: "myNews",
data(){
return{
opacity: 1
}
},
activated() {
console.log('News The component is activated ');
this.timer = setInterval(() => {
console.log('@');
this.opacity -= 0.01
if(this.opacity <=0 ) this.opacity = 1
},16)
},
deactivated() {
console.log('News The component is deactivated ');
clearInterval(this.timer)
}
};
</script>
Home.vue
<keep-alive include="myNews">
<router-view></router-view>
</keep-alive>
Every step is worth a thousand miles Not small flow into the sea
Don't get lost by paying attention , Ongoing update …
边栏推荐
- Research Report on the overall scale, major manufacturers, major regions, products and applications of Electric Screwdrivers in the global market in 2022
- SCI Writing - Methodology
- Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
- Hugo blog building tutorial
- [matrix theory & graph theory] final exam review mind map
- leetcode:6096. Success logarithm of spells and potions [sort + dichotomy]
- 【历史上的今天】6 月 12 日:美国进入数字化电视时代;Mozilla 的最初开发者出生;3Com 和美国机器人公司合并
- 吃饭咯 干锅肥肠 + 掌中宝!
- 快速复制浏览器F12中的请求到Postman/或者生成相关语言的对应代码
- Free measurement of rectangular card [manual drawing ROI] Based on Halcon
猜你喜欢

快速复制浏览器F12中的请求到Postman/或者生成相关语言的对应代码

leetcode:5270. 网格中的最小路径代价【简单层次dp】

被八股文害惨了。。。

Problems that the sap Spartacus e-commerce cloud UI shipping method does not display in the unit test environment

How to download Vega in China

leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】

Basic SQL statement - select (single table query)

Redis(三十二)-用Redis做分布式锁
![leetcode:98. Count the number of subarrays whose score is less than k [double pointers + number of calculated subsets + de duplication]](/img/97/67d74ae93be2cd4c0153a764cbe4c1.png)
leetcode:98. Count the number of subarrays whose score is less than k [double pointers + number of calculated subsets + de duplication]

Quickly copy the request in browser F12 to postman/ or generate the corresponding code of the relevant language
随机推荐
国内如何下载Vega
【sql语句基础】——查(select)(单表查询)
leetcode:5270. 网格中的最小路径代价【简单层次dp】
Analyzing mobx responsive refresh mechanism from source code
OpenGL shadow implementation (soft shadow)
Why my order by create_ Time ASC becomes order by ASC
MySQL - > > symbol usage JSON related
Review of MySQL (3): query operation
Difference between rxjs of() and of ({})
SCI Writing - Methodology
2022.6.12-----leetcode.890
Leetcode 416. Split equal sum subset
机器学习在美团配送系统的实践:用技术还原真实世界-笔记
SCI Writing - Results
lua记录
Hugo 博客搭建教程
kali局域网ARP欺骗(arpspoof)并监听(mitmproxy)局域内其它主机上网记录
笔记本电脑清灰打硅脂后,开机一直黑屏,如何破?
实验10 Bezier曲线生成-实验提高-控制点生成B样条曲线
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update