Vue路由参数及监听路由

路由参数

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./vue.js"></script>
    <!-- 1. 先引入vue-router文件 -->
    <script src="./vue-router.js"></script>
    <style>
      ul {
        display: flex;
      }
      li {
        list-style-type: none;
        padding: 0 20px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <ul>
        <li><router-link to="/home">首页</router-link></li>
        <li><router-link to="/product/one">蔬菜</router-link></li>
        <li><router-link to="/product/tow">水果</router-link></li>
        <li><router-link to="/product/33">肉类</router-link></li>
      </ul>

      <router-view></router-view>
    </div>
    <script>
      let myhome = Vue.component('home', {
        template: '<div>这是首页</div>'
      })
      let myproduct = Vue.component('product', {
        // 2. 页面中获取路由参数通过    $route.params.参数名    获取
        template: `
              <div>
                <p>商品分类,该分类的编号是{{$route.params.cid}}</p>
              </div>
        `,
        mounted () {
          // 3. js中通过this.$route.params.参数名来获取路由参数
          console.log(this.$route.params.cid);
        }
      })
      let router = new VueRouter({
        routes: [
          {name: 'index', path: '/home', component: myhome},
          // 1. 路由参数 :参数名  就可以定义了,⭐这个冒号的作用是将后面的变量变成参数
          {name: 'productCate', path: '/product/:cid', component: myproduct}
        ]
      })
      var vm = new Vue({
        el: '#app',
        router,
        data: {

        }
      })
    </script>
  </body>
</html>

监听路由

<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./vue.js"></script>
    <!-- 1. 先引入vue-router文件 -->
    <script src="./vue-router.js"></script>
    <script src="./axios.js"></script>
    <style>
      .myul {
        display: flex;
      }
      .navitem {
        list-style-type: none;
        padding: 0 20px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <ul class="myul">
        <li class="navitem"><router-link to="/home">首页</router-link></li>
        <li class="navitem"><router-link to="/product/11">蔬菜</router-link></li>
        <li class="navitem"><router-link to="/product/22">水果</router-link></li>
        <li class="navitem"><router-link to="/product/33">肉类</router-link></li>
      </ul>

      <router-view></router-view>
    </div>
    <script>
      let myhome = Vue.component('home', {
        template: '<div>这是首页</div>'
      })
      let myproduct = Vue.component('product', {
        template: `
              <div>
                <p>这是商品分类,该分类的编号是{{$route.params.cid}}</p>
                <ul>
                  <li v-for="(item, index) in products" :key="index">{{item}}</li>
                </ul>
              </div>
        `,
        data () {
          return {
            products: []
          }
        },
        // watch: {
        //   '$route' (to, from) {
        //     // 对路由变化作出响应...
        //     // to 表示到哪里去的路由对象
        //     // console.log(to);
        //     // from 表示从哪个路由对象跳
        //     // console.log(from);
        //     let cid = to.params.cid
        //     console.log(cid);
        //   }
        // },
        watch: {
          '$route': {
            handler(to, from) {
              let cid = to.params.cid
              console.log(cid);
              axios.get(`http://192.168.75.84:3000/list/${cid}`)
                .then(res => {
                  // 将获取回来的数据保存起来
                  this.products = res.data.products
                })
            },
            // immediate: true表示立马执行一次
            immediate: true
          }
        }
        // mounted () {
        //   console.log(this.$route.params.cid);
        // }
      })
      let router = new VueRouter({
        routes: [
          {name: 'index', path: '/home', component: myhome},
          {name: 'productCate', path: '/product/:cid', component: myproduct}
        ]
      })
      var vm = new Vue({
        el: '#app',
        router,
        data: {

        }
      })
    </script>
  </body>
</html>

猜你喜欢

转载自blog.csdn.net/Q_MUMU/article/details/85223515