创新实训(二十三)

在测试时我们又发现了一个bug,就是在搜索景点时如果切换城市重新搜索,那么景点的名字和地址(页面直接传进去的部分)会改变,但是景点图片、评分和介绍(子组件自己调用方法的部分)不会改变。

后来我发现这是因为我把子组件调用方法写在了组件创建时,而页面刷新时子组件并不会重新创建,也就不会重新调获取信息的方法。解决这个问题也不难,就是在子组件中加入监听器监听组件中地址属性的改变,改变时再次调用获取信息的方法即可。

代码如下:

  methods: {
    getInfos() {
      this.loading=true;
      this.$axios({
        method: "get",
        url: "http://127.0.0.1:8000/api/getSpotsInfo",
        params: {
          city: this.city,
          name: this.name,
        },
      })
        .then((res) => {
          // console.log(res.data.results); //在console中看到数据
          var array = res.data.data;
    this.loading=false;
          if (array == undefined || array.length <= 0) {
            console.log("no");
          } else {
            this.imgsrc = array.imgsrc;
            this.rank = array.rank;
            this.score = array.score;
    if(this.rank&&this.rank!=0){
      this.hasRank=true;
    }
    if(this.score&&this.score!="0.0"){
      this.hasScore=true;
    }
            this.description = array.description;
          }
        })
        .catch((res) => {
          // alert("出错了!");
        });
    },

  },

  watch:{
    address:'getInfos'
  },
  mounted() {
    this.loading=true;
    this.getInfos();
  },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值