您的位置:首页 > 游戏 > 手游 > 深圳最好的网站建设公司_竞价sem托管公司_最有效的推广方式_太原网站关键词排名

深圳最好的网站建设公司_竞价sem托管公司_最有效的推广方式_太原网站关键词排名

2024/10/6 6:02:01 来源:https://blog.csdn.net/J080624/article/details/142204151  浏览:    关键词:深圳最好的网站建设公司_竞价sem托管公司_最有效的推广方式_太原网站关键词排名
深圳最好的网站建设公司_竞价sem托管公司_最有效的推广方式_太原网站关键词排名

【1】Vue2编程式路由导航

① router.push

除了使用 <router-link> 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。

router.push(location, onComplete?, onAbort?)

注意:在 Vue 实例内部,你可以通过 $router 访问路由实例。因此你可以调用 this.$router.push

想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

当你点击 <router-link> 时,这个方法会在内部调用,所以说,点击 <router-link :to="..."> 等同于调用 router.push(...)

声明式编程式
<router-link :to="...">router.push(...)

该方法的参数可以是一个字符串路径,或者一个描述地址的对象。例如:

// 字符串
router.push('home')// 对象
router.push({ path: 'home' })// 命名的路由
router.push({ name: 'user', params: { userId: '123' }})// 带查询参数,变成 /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})

注意:如果提供了 path,params 会被忽略,上述例子中的 query 并不属于这种情况。取而代之的是下面例子的做法,你需要提供路由的 name 或手写完整的带有参数的 path:

const userId = '123'
// params与name组合使用
router.push({ name: 'user', params: { userId }}) // -> /user/123
//拼接path
router.push({ path: `/user/${userId}` }) // -> /user/123// 这里的 params 不生效  params不可以与path组合使用
router.push({ path: '/user', params: { userId }}) // -> /user

同样的规则也适用于 router-link 组件的 to 属性。

在 2.2.0+,可选的在 router.pushrouter.replace 中提供 onComplete 和 onAbort 回调作为第二个和第三个参数。这些回调将会在导航成功完成 (在所有的异步钩子被解析之后) 或终止 (导航到相同的路由、或在当前导航完成之前导航到另一个不同的路由) 的时候进行相应的调用。在 3.1.0+,可以省略第二个和第三个参数,此时如果支持 Promise,router.push 或 router.replace 将返回一个 Promise

注意: 如果目的地和当前路由相同,只有参数发生了改变 (比如从一个用户资料到另一个 /users/1 -> /users/2),你需要使用 beforeRouteUpdate 来响应这个变化 (比如抓取用户信息)。


② router.replace

router.replace(location, onComplete?, onAbort?)

router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。

声明式编程式
<router-link :to="..." replace>router.replace(...)

使用实例如下:

<li v-for="m in messages" :key="m.id"><router-link :to="`/home/message/detail/${m.id}`">{{m.title}}</router-link><button @click="pushShow(m.id)">push查看</button><button @click="replaceShow(m.id)">replace查看</button>
</li>
methods: {pushShow (id) {this.$router.push(`/home/message/detail/${id}`)},replaceShow(id) {this.$router.replace(`/home/message/detail/${id}`)}}

总结如下:

1) this.$router.push(path): 相当于点击路由链接(可以返回到当前路由界面)
2) this.$router.replace(path): 用新路由替换当前路由(不可以返回到当前路由界面)
3) this.$router.back(): 请求(返回)上一个记录路由
4) this.$router.go(-1): 请求(返回)上一个记录路由
5) this.$router.go(1): 请求下一个记录路由

③ 替换路由参数并刷新路由

this.$router.replace({name: 'concertDetail',query: {concertId: id}
})
/**  兼容性好*/
window.location.reload()

【2】Vue3编程式路由导航

路由组件的两个重要的属性:$route$router变成了两个hooks 。route表示当前路由,router表示路由器实例。

import {useRoute,useRouter} from 'vue-router'const route = useRoute()
const router = useRouter()
//当前路由信息
console.log(route.query)
console.log(route.parmas)
//路由器实例
console.log(router.push)
console.log(router.replace)

① router.replace

<template><div class="news"><!-- 导航区 --><ul><li v-for="news in newsList" :key="news.id"><button @click="showNewsDetail(news)">查看新闻</button><RouterLink :to="{name:'xiang',query:{id:news.id,title:news.title,content:news.content}}">{{news.title}}</RouterLink></li></ul><!-- 展示区 --><div class="news-content"><RouterView></RouterView></div></div>
</template><script setup lang="ts" name="News">import {reactive} from 'vue'import {RouterView,RouterLink,useRouter} from 'vue-router'const newsList = reactive([{id:'asfdtrfay01',title:'很好的抗癌食物',content:'西蓝花'},{id:'asfdtrfay02',title:'如何一夜暴富',content:'学IT'},{id:'asfdtrfay03',title:'震惊,万万没想到',content:'明天是周一'},{id:'asfdtrfay04',title:'好消息!好消息!',content:'快过年了'}])const router = useRouter()interface NewsInter {id:string,title:string,content:string}function showNewsDetail(news:NewsInter){router.replace({name:'xiang',query:{id:news.id,title:news.title,content:news.content}})}</script>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com