vue-(watch/mode/scrollBehavior)应用

一、watch:实时监听路由传参数时做相应数据变化

data(){
  return{
     shopid:1,     
  }
},
created(){
    this.fetchData()
  },
watch:{
  // 如果路由有变化,会再次执行该方法
   '$route': 'fetchData'
   },
methods:{
  fetchData () {            
    this.shopid=this.$route.query.shopid;
    //因为实时监听路由的变化执行该方法 当没有传入时console会报错  解决方法是没有传入参数时 给设置默认参数
    if(this.$route.query.shopid==undefined){
      console.log('shopid=='+this.$route.query.shopid);
      this.shopid=1;
    }      
    
    axios.get('http://172.16.2.43:8080/static/data/mt_index_detail.json').then((res)=>{   
                     
    })                          
  },
  goback(){  //返回
    this.$router.go(-1);

  },

二、路由跳转切换页面时滚动条不置顶 保持原来位置的解决方法(可以设置滚动到相应的位置)

路由文件 router/index.js

export default new Router({
 mode:'history',
 scrollBehavior: function (to, from, savedPosition) {
      return savedPosition || { x: 0, y: 0 }
   },

  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld

    },

....}

)}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值