【Vue】路由跳转

清疚 2021-12-15 06:55 560阅读 0赞

路由跳转有几种方式,我用的最多的是$router.push的方法:
实践:
例如下面的页面,要求点详情跳转到详情页。
在这里插入图片描述
那么在列表页代码如下:

  1. //详情
  2. detail (id) {
  3. this.$router.push({
  4. path: '/cms-storeoperationcontent/storeoperationcontent-detail',
  5. query: {
  6. id
  7. }
  8. })
  9. }

详情页代码如下:

  1. activated () {
  2. this.$nextTick(() => {
  3. this.operationId = this.$route.query.id;
  4. this.init(this.operationId);
  5. });
  6. }
  7. init (id) {
  8. this.dataForm.id = id || 0
  9. this.$nextTick(() => {
  10. this.$refs['dataForm'].resetFields()
  11. if (this.dataForm.id) {
  12. this.$http({
  13. url: this.$http.adornUrl(`/store/operation/info/` + id),
  14. method: 'get',
  15. params: this.$http.adornParams()
  16. }).then(({data}) => {
  17. if (data && data.code === 0) {
  18. this.dataForm.operateType = data.operationContentVo.operateType,
  19. this.dataForm.title = data.operationContentVo.title,
  20. this.dataForm.url = data.operationContentVo.url,
  21. this.dataForm.viedoDesc = data.operationContentVo.viedoDesc
  22. }
  23. })
  24. }
  25. })
  26. }

注意这里要定义:

  1. data () {
  2. return {
  3. operationId: ''
  4. }
  5. }

路由要配置:

  1. { path: '/cms-storetrainlive/storetrainlive-detail', name: 'storetrainlive-detail', component: _import('modules/cms/storetrainlive-detail'), meta: { title: '培训直播详情', isTab: true, isDynamic: false } }

遇到的问题:
1、$router 和 $route 的区别:
在push的时候用$router,获取参数的时候用$route,千万不能搞错。

  • $router是VueRouter的一个对象
  • $route是跳转路由的对象

2、activated()和mounted()方法加载顺序:

  • mounted()是挂载vue后的钩子函数
  • activated()是组件被激活后的钩子函数
    在这里插入图片描述
    从表中看出:mounted() 可以理解为发生在activated()之前。
    源码解析,渲染组件时,会判断组件是否被挂载,如果没有会先执行mount的hook函数。

3、还有一种路由跳转传参的方式:

  1. <router-link :to="{ path: 'yourPath', params: { name: 'name', dataObj: data }, query: { name: 'name', dataObj: data } }"> </router-link>

4、路由跳转同一页面,显示不同名称
同一个页面,添加和编辑的时候tab要显示不同的名称,以上的路由配置方法只会显示“培训直播详情”,我们稍加修改,成以下代码:
在全局定义的路由里定义title:

  1. router.beforeEach((to, from, next) => {
  2. if(from.path === '/cms-storetrainlive' && to.name === 'storetrainlive-add-or-update'){
  3. const flag = to.query.flag;
  4. if(flag===1){
  5. to.meta.title="详情"
  6. } else if(flag === 0){
  7. to.meta.title ="编辑内容"
  8. } else {
  9. to.meta.title ="新增内容"
  10. }
  11. }
  12. }

从这里改又出现一个问题,刷新页面,tab的名称就空了,干脆我们直接修改路由配置,问题巧妙解决:

  1. { path: '/cms-storetrainlive/add', name: 'storetrainlive-add', component: _import('modules/cms/storetrainlive-add-or-update'), meta: { title: '新增培训直播', isTab: true, isDynamic: false } },
  2. { path: '/cms-storetrainlive/edit', name: 'storetrainlive-edit', component: _import('modules/cms/storetrainlive-add-or-update'), meta: { title: '编辑培训直播', isTab: true, isDynamic: false } },
  3. { path: '/cms-storetrainlive/detail', name: 'storetrainlive-detail', component: _import('modules/cms/storetrainlive-add-or-update'), meta: { title: '培训直播详情', isTab: true, isDynamic: false } }

path和name自己起名字,component要写自己要跳转的页面,这样就不会出现刷新页面的问题了。
注意在跳转页面的时候配置路径和路由配置的一致:

  1. // 新增 / 修改 / 详情
  2. //加个flag,标记是编辑or详情
  3. addOrUpdateHandle (id,state,flag) {
  4. this.flag = flag;
  5. if (!id) {
  6. // 新增
  7. this.$router.push({
  8. path: '/cms-storetrainlive/add',
  9. query: {
  10. id,
  11. state,
  12. flag
  13. }
  14. })
  15. } else {
  16. if (flag === 1) {
  17. // 详情
  18. this.$router.push({
  19. path: '/cms-storetrainlive/detail',
  20. query: {
  21. id,
  22. state,
  23. flag
  24. }
  25. })
  26. }else {
  27. // 修改
  28. this.$router.push({
  29. path: '/cms-storetrainlive/edit',
  30. query: {
  31. id,
  32. state,
  33. flag
  34. }
  35. })
  36. }
  37. }
  38. }

总结:
知识积少成多,但也许由点成面,面动成块,对vue还欠一次整体规划学习,加油!

发表评论

表情:
评论列表 (有 0 条评论,560人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue

    所谓的router就是动态的往根里面挂载组件,而不是手动挂载。 vue路由配置的步骤: https://router.vuejs.org/ 1.安

    相关 Vue

    [【Vue】路由跳转][Vue] 路由跳转有几种方式,我用的最多的是$router.push的方法: 实践: 例如下面的页面,要求点详情跳转到详情页。 ![在这里

    相关 Vue

    路由跳转有几种方式,我用的最多的是$router.push的方法: 实践: 例如下面的页面,要求点详情跳转到详情页。 ![在这里插入图片描述][watermark_

    相关 Vue+传参

    之前在原生JS的开发中,我们经常会用到根据某一状态进行页面的跳转。 比如:登录成功跳到首页,点击商品列表的某个商品跳转商品详情等。 而常见的写法就是: locat