vue仿知乎日报项目笔记

1、router.push(location)

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

// 字符串
router.push('home')

// 对象
this.$router.push({path: '/login?url=' + this.$route.path});

// 命名的路由
router.push({ name: 'user', params: { userId: 123 }})

// 带查询参数,变成/backend/order?selected=2
this.$router.push({path: '/backend/order', query: {selected: "2"}});

// 设置查询参数
this.$http.post('v1/user/select-stage', {stage: stage})
      .then(({data: {code, content}}) => {
            if (code === 0) {
                // 对象
                this.$router.push({path: '/home'});
            }else if(code === 10){
                // 带查询参数,变成/login?stage=stage
                this.$router.push({path: '/login', query:{stage: stage}});
           }
});

// 设计查询参数对象
let queryData = {};
if (this.$route.query.stage) {
    queryData.stage = this.$route.query.stage;
}
if (this.$route.query.url) {
    queryData.url = this.$route.query.url;
}
this.$router.push({path: '/my/profile', query: queryData});
View Code

 

 2、$emit()

使用$emit()函数可以沿着作用域链向上派送(从子作用域到父作用域),$emit只能向parent controller传递event与data。

 3、router.go(n)

在vue中,router.go(n)====window.history.go

// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)
 
// 后退一步记录,等同于 history.back()
router.go(-1)
 
// 前进 3 步记录
router.go(3)
View Code

 

posted @ 2017-08-16 10:39  ljshu  阅读(548)  评论(0编辑  收藏  举报