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>

猜你喜欢

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