今天在实现一个小功能的时候,遇到一个问题,使用vue-router获取上一页面的url信息,我尝试了多种方式,发现使用vue-router的canDeactivate钩子实现这个功能最为方便,现在将我的实现代码总结如下:

           项目使用的是vue-cli,直接贴代码         

Vue2.0 探索之路——vue-router入门教程和总结
export default {
    mixins: [],
    vuex: {
      actions: {fetchCertificates},
    },
    data() {
      return {}
    },
    route: {
      data() {
        this.$root.showLoading();
        return this.fetchCertificates().then((res) => {
          this.$root.dismissLoading();
          if (res.error) return this.$root.toastError(res.error);
          if(res.data.certificates.length >0){
            return res.data;
          }else{
            console.log(this.$route ,"----当前页面的url信息----");
          }
        });
      },
      canActivate (transition) {
        console.log(transition,"======上一个页面的url信息=======");
        transition.next();
      }
    },
  }
Vue2.0 探索之路——vue-router入门教程和总结

 Vue2.0 探索之路——vue-router入门教程和总结

      

Vue2.0 探索之路——vue-router入门教程和总结

转载http://www.cnblogs.com/chenyablog/p/6951530.html

相关文章: