vue router路由中的next()用法

学习VUE3,在看到一些源码中,内容如下:

router.beforeEach(async (to, from, next) => {
    if (to.path === '/login') {
      next()
    } else {
         //...... 一些处理
          next({ ...to, replace: true })
        } 
    }

其中 next({...to,replace:true})做何解?

阅读 2.9k
1 个回答

对象展开运算符。

const src = {
  k1: 'hello',
  k2: 123,
  k3: {},
  k4: [],
  k5: function() {}
}
const dest = [...src, { k1: 'world' }];

// 等效于:
const dest = {
  k1: 'world',
  k2: src.k2,
  k3: src.k3,
  k4: src.k4,
  k5: src.k5
}
// 注意 src 本身没有被修改,得到的 dest 是一个新的对象

当然你也可以选择使用 Object.assign 来替代。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题