1. 程式人生 > >Vue路由引數及監聽路由

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>