聲明式:<router-link :to="{name:'index'}}"> 或者 <router-link to='/index'>
編程式:router.push(...)
方法一: this.$router.push({path:'路徑')};(自己常用)
方法二:this.$router.push({name:'組件名')};
2.需求:點擊按鈕,跳轉到任意頁面(傳參)
聲明式:<router-link :to="{name:'index',query:{id:'xxx',name:'xxx'}}">
編程式:router.push(...)
方法一:this.$router.push({path:'xxx',query:{aa:xx, bb: xx}}); //帶查詢參數,類似於 “?” 的形式傳值
方法二:this.$router.push({path:'xxx',params:{aa:xx, bb: xx}});
注:以上兩種方法的query跳轉路徑也可以寫成name:'組件名'的形式
在query中放入需要傳遞的參數即可,多個參數之間用逗號隔開;
取值:this.$route.query.xx (可在跳轉的頁面取得所傳遞的值);
eg1:
跳轉並傳值:this.$router.push({path:'index',query:{id:'123'}); // 帶查詢參數,變成/index?id=123
取值:this.$route.query.id ;
eg2:
跳轉並傳值:this.$router.push({path:'xxx',params:{id:'123'});
取值:this.$route.params.id ;