Vue - watch monitor the routing control address - component changes ---- '$ route.path': function (newVal, oldVal)

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/vue-2.4.0.js"></script>
  <! - package 1. Import ->
  <script src="./lib/vue-router-3.0.1.js"></script>
</head>

<body>
  <div id="app">
    <router-link to="/login">登录</router-link>
    <router-link to="/register">注册</router-link>

    <! - containers ->
    <router-view></router-view>

  </div>

  <script>
    // 2. Create a sub-assemblies
    were login = {
      template: '<h3> This is the login sub-components, this component is rushing Pa development. </ H3> '
    }

    was Directory = {
      template: '<h3> This subassembly is registered, this component was developed by Ben Pa wave. </ H3> '
    }

    // 3. Create a route object
    var = new router VueRouter ({
      routes: [// array of routing rules
        { path: '/', redirect: '/login' },
        { path: '/login', component: login },
        { path: '/register', component: register }
      ],
      linkActiveClass: 'myactive' // activation and related classes
    })

    // Create Vue instance, get ViewModel
    was vm = new Vue ({
      the '#app'
      data: {},
      methods: {},
      // router: router
      router,
      watch: {
        //  this.$route.path
        '$route.path': function (newVal, oldVal) {
          // console.log(newVal + ' --- ' + oldVal)
          if (newVal === '/login') {
            console.log ( 'Welcome to the login page')
          } else if (newVal === '/register') {
            console.log ( 'Welcome to the registration page')
          }
        }
      }
    });
  </script>
</body>

</html>

Guess you like

Origin www.cnblogs.com/fdxjava/p/11617789.html