script中data的2种写法

vue中,在script中data有2种写法。

//1.使用return
data(){
return {

}

//2.
data: {

}
data(){

}

获取路由的id值

//获取路由的id值【ps:id即是浏览器的参数,不要改成跟url中的占位名称】
if(this.$route.params && this.$route.params.id){
this.$route.params.id
}

路由跳转

this.$router.push({path:'uri地址'})

路由跳转重复页面

由于多次路由跳转到同一个页面,在页面中cretaed方法只会执行第一次,后面再进行跳转不会执行的。所以需要一个watch监听路由变化

watch:{ //监听
$route(to,form){ //路由变化方式,路由发生变化,方法方法执行
this.init()
}
}