[Vue] 路由vue-router两种跳转方式

<template>
  <div class="goods-list">
    <!-- 两种跳转方式 -->
    <!-- 1.标签跳转 ,即使用 a 标签形式跳转 --> 
    <!-- <router-link :to="'/home/goodsInfo/'+g.id" tag="div" class="goods-item" v-for="g in goodsList" :key="g.id" >
      <img :src="g.img_url" />
      <h1 class="title">{{g.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">¥{{g.market_price}}</span>
          <span class="old">¥{{g.sell_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{g.stock_quantity}}件</span>
        </p>
      </div>
    </router-link> -->
    <!-- 2.编程式跳转 ,即使用 JS 编程跳转 window.location.href --> 
    <div class="goods-item" v-for="g in goodsList" :key="g.id" @click="goDetails(g.id)">
      <img :src="g.img_url" />
      <h1 class="title">{{g.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">¥{{g.market_price}}</span>
          <span class="old">¥{{g.sell_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{g.stock_quantity}}件</span>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageindex: 1,
      goodsList: [  ],
      loading: false
    };
  },
  created() {
    this.getGoodsList();
    this.scroll();
  },
  methods: {
    scroll() {
      var that = this;
      window.onscroll = function() {
        // 变量scrollTop是滚动条距离顶部的距离,windowHeight是可视区的高度,scrollHeight是滚动条的总高度
        var scrollTop =
          document.documentElement.scrollTop || document.body.scrollTop;
        var windowHeight =
          document.documentElement.clientHeight || document.body.clientHeight;
        var scrollHeight =
          document.documentElement.scrollHeight || document.body.scrollHeight;
        //滚动条到底部的条件
        // console.log("距顶部" + scrollTop + "\n可视区高度" + windowHeight + "\n滚动条总高度" + scrollHeight);
        if (scrollTop + windowHeight == scrollHeight) {
          if (that.loading) return; //当上一次获取到的数据长度为0时,该判断 return 加载不执行
          that.loading = true;
          that.pageindex++;
          var timeout = setTimeout(function() {
            that.getGoodsList();
            that.loading = false;
          }, 500);
        }
      };
    },
    getGoodsList() {
      this.$http
        .get(
          "url/api/getgoods?pageindex=" +
            this.pageindex
        )
        .then(result => {
          console.log(result.body);
          if (result.body.status === 0 && result.body.message.length > 0) {
            //   this.goodsList = result.body.message;
            console.log(this.goodsList);
            if (this.goodsList.length > 0) {
              this.goodsList = this.goodsList.concat(result.body.message);
            } else {
              this.goodsList = result.body.message;
            }
            console.log(this.goodsList);
          } else {

            this.loading = true;
          }
        });
    },
    goDetails(id){ 
      // 使用JS的方式进行跳转(路由导航)

      // 注意:区分 this.$route 和this.%router
      // this.$route 是路由【参数对象】,所有路由中的参数,params,query 都属于它
      // this.$router 是路由【导航对象】,用它可以方便的使用JS代码,实现路由的前进、后退、跳转到新的URL地址
      // this.$router.push({ name: 'user', params: { userId }}) 给路由命名
      // 带参数 this.$router.push({ path: `/user/${userId}` })
      // this.$router.replace() 不会向 history 添加新记录,而是跟它的方法名一样,替换掉当前的 history 记录
      // this.$router.go(n)  在 history 记录中向前或者后退多少步,类似 window.history.go(n)。

      console.log(this);
      // 1.最简单的
      // this.$router.push('/home/goodsInfo/'+id);
      // 2.传递对象
      // this.$router.push({path:"/home/goodsInfo/"+id}) //字符串拼接
      // this.$router.push({path:`/home/goodsInfo/${id}`}) //ES6 模板字符串 ${expression}占位符
      // 3.命名的路由
      this.$router.push({name:"goodsInfo",params:{id}})
      // 4. 带查询参数,变成 /register?plan=private
      // this.$router.push({ path: "goodsInfo", query: { plan: 'private' }})
    }
  }
};
</script>

<style lang="scss" scroped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 5px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    border: 1px solid #eee;
    border-radius: 10px 10px;
    box-shadow: 0 0 5px #eee;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin: 5px 0px;
    min-height: 295px;
    img {
      width: 95%;
      margin: 0 auto;
      margin-top: 10px;
    }
    .title {
      font-size: 14px;
      margin: 10px 5px;
    }
    .info {
      background: #eee;
      padding: 10px;
      border-radius: 0 0 10px 10px;
      p {
        margin: 0 0;
      }
      .price {
        .now {
          color: red;
          font-size: 16;
          font-weight: bold;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>

Vue Router文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-router提供了多种路由方式。其中一种是通过声明式导航router-link来实现。在APP.vue中,可以使用<router-link>标签来创建链接,通过设置to属性指定的路径。例如,使用<router-link to="/home">home</router-link>可以到名为home的路由页面。同样地,可以使用<router-link>标签来创建其他路由链接。\[1\] 另一种方式是通过编程式导航来实现路由。在使用vue-router之前,需要先安装vue-router插件并在main.js中引入和注册。然后,可以在代码中使用VueRouter的实例来进行路由。\[2\] 在路由配置项中,可以定义路由的路径和对应的组件。通过在<router-view>标签中引入组件,可以实现路由内容的显示。需要注意的是,<router-view>标签必须作为容器来显示路由内容。\[3\] #### 引用[.reference_title] - *1* *2* [vue路由router的基本使用及路由方式](https://blog.csdn.net/jjjh968/article/details/120785547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v12^insert_chatgpt"}} ] [.reference_item] - *3* [vue中实现路由的三种方式(超详细整理)](https://blog.csdn.net/sebeefe/article/details/126080415)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v12^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值