当前位置:网站首页>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 …
边栏推荐
- Review of MySQL (I): go deep into MySQL
- 2022.6.12-----leetcode.890
- Kali2022 how to install w3af
- 实验10 Bezier曲线生成-实验提高-交互式生成B样条曲线
- uniapp使用阿里图标
- 笔记本电脑清灰打硅脂后,开机一直黑屏,如何破?
- leetcode:5259. 计算应缴税款总额【简单模拟 + 看看在哪个区间】
- 深圳3月14日起全市停工停业7天居家办公心得|社区征文
- Basic SQL statement - select (single table query)
- 232-ch579m learning and development Ethernet routine TCP server (project application package, LAN or WAN test)
猜你喜欢

How to download proxystrike in China

Hash hash
![[matrix theory & graph theory] final exam review mind map](/img/7f/ea23970c613bbb366d9d840c6c175b.png)
[matrix theory & graph theory] final exam review mind map

Review of MySQL (VI): usage of Union and limit

Kali2022 how to install w3af

论大型政策性银行贷后,如何数字化转型 ?-亿信华辰

To understand Devops, you must read these ten books!

Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of swimming fins in the global market in 2022

国内如何下载ProxyStrike

Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers
随机推荐
Why my order by create_ Time ASC becomes order by ASC
MySQL数据库实验一 数据定义
Delivery lead time lightweight estimation practice - Notes
leetcode:6094. 公司命名【分组枚举 + 不能重复用set交集 + product笛卡儿积(repeat表示长度)】
On how to make digital transformation after the loan of large policy banks- Yixinhuachen
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of swimming fins in the global market in 2022
美团智能配送系统的运筹优化实战-笔记
观察网站的页面
Quickly copy the request in browser F12 to postman/ or generate the corresponding code of the relevant language
从源码解析 MobX 响应式刷新机制
Operational research optimization of meituan intelligent distribution system - Notes
liunx部署Seata(Nacos版)
leetcode:98. Count the number of subarrays whose score is less than k [double pointers + number of calculated subsets + de duplication]
笔记本电脑清灰打硅脂后,开机一直黑屏,如何破?
Use of nexttick function
Review of MySQL (VI): usage of Union and limit
Leetcode topic [string] -151- flip words in string
国内如何下载ProxyStrike
To understand Devops, you must read these ten books!
在思科模擬器Cisco Packet Tracer實現自反ACL