1 methods: { 2 //搜索按钮的回调 3 goSearch() { 4 //路由的跳转,采用的是编程式导航. 5 //路由传递参数 6 7 //第一种传递query参数 8 // this.$router.push({path:'/search',query:{keyword:this.keyword}}); 9 10 //第二种传递params参数 [一定要注意,面试的时候经常问] 11 // this.$router.push({name:'search',params:{keyword:this.keyword}}) 12 13 //第三种传递query+params 14 // this.$router.push({ 15 // name: "search", 16 // params: { keyword: this.keyword }, 17 // query: { keyword: "ABC" }, 18 // }); 19 20 //验证Vue-Router引入Promise技术,最笨的方法,给push传递第二个、第三个参数【回调函数】 21 //下面这种写法:治标不治本!!!! 22 // let result = this.$router.push({name: "search",params: { keyword: this.keyword|| undefined}},()=>{},()=>{}); 23 24 //问题1:push方法,里面this是谁? vueRouter类的实例 25 // this.$router.push({name:'search',params:{keyword:this.keyword}}); 26 //问题2:push方法里面的this是谁?windows 27 // let result = this.$router.push; 28 // result({name:'search',params:{keyword:this.keyword}}) 29 30 //点击搜索按钮之前,如果路径当中有query参数,需要携带给search 31 32 let locations = { 33 name: "search", 34 params: { keyword: this.keyword || undefined }, 35 }; 36 //确定路径当中有query参数 37 if (this.$route.query.categoryName) { 38 locations.query = this.$route.query; 39 } 40 41 this.$router.push(locations); 42 }, 43 //退出登录的按钮的回调 44 logout(){ 45 //派遣action退出登录 46 this.$store.dispatch('logout'); 47 } 48 }, 49 mounted() { 50 //清除关键字 51 this.$bus.$on("clearKeyword", () => { 52 console.log(123); 53 this.keyword = ""; 54 }); 55 }, 56 };
标签:面试题,vue,keyword,search,params,router,push,query From: https://www.cnblogs.com/hs20011205/p/16820360.html