起因
我需要在页面created时获得商家的类型id值,然后监听id值,动态的改变请求接口的地址。请求接口的函数在mounted中执行。业务逻辑写完后发现,页面在第一次加载时,接口没有数据,原因接口地址为空,即接口地址没有改变。
代码
created() {
this.typeId = this.$route.params.id;
console.log("this.typeId的值发生改变,触发watch");
},
mounted() {
console.log(this.typeUrl, "mounted中的typeUrl的值");
//访问接口的函数
this.getData();
},
watch
watch: {
// 监听订单类型 给出对应面包屑
typeId: function (val) {
if (val == 1) {
console.log("watch触发了");
this.selfBreadcrumb[1].name = "核销订单";
this.typeUrl = "settlements";
console.log(this.typeUrl, "这里是watch");
} else if (val == 2) {
this.selfBreadcrumb[1].name = "物流订单";
this.typeUrl = "logistics-orders";
} else if (val == 4) {
this.selfBreadcrumb[1].name = "外卖订单";
this.typeUrl = "takeout-orders";
}
},
控制台
说明
watch的触发会在created和mounted结束后。
先占个坑,具体原理如何只有等我项目完成了再来康康。