Vue-Router-编程式导航

2021/4/24 20:25:58

本文主要是介绍Vue-Router-编程式导航,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

编程式导航

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="http://unpkg.com/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  <title>Vue-Router</title>
</head>
<body>
  <div id="box">
    <p>
      <button @click="jump('home')">home</button>
      <button @click="jump('news')">news</button>
      <button @click="replaceJump('home')">replace-home</button>
      <button @click="replaceJump('news')">replace-news</button>
      <button @click="goJump(1)">前进一步</button>
      <button @click="goJump(-1)">后退一步</button>
      <button @click="goJump(6)">N步</button>
    </p>
    <router-view></router-view>
  </div>

  <!-- 模板抽离 -->
  <template id="login">
    <div class="login">
      {{msg}}
    </div>
  </template>

  <template id="home">
    <div class="home">
      {{msg}}
    </div>
  </template>

  <template id="news">
    <div class="news">
      {{msg}}
    </div>
  </template>

  <template id="com404">
    <div class="com404">
      {{msg}}
    </div>
  </template>

  <script type="text/javascript">
    // 1.定义路由组件
    const Login = {
      data() {
        return {
          msg: "这里是login组件"
        }
      },
      template: "#login"
    };

    const Home = {
      data() {
        return {
          msg: "这里是home组件"
        }
      },
      template: "#home"
    };

    const News = {
      data() {
        return {
          msg: "这里是news组件"
        }
      },
      template: "#news"
    };

    const Com404 = {
      data() {
        return {
          msg: "这里是com404组件"
        }
      },
      template: "#com404"
    };

    // 2.定义路由
    const routes = [
      {//匹配根路径
        path: '/',
        name: 'Login',
        component: Login,
      },
      {
        path: '/home',
        name: 'Home',
        component: Home,
      },
      {
        path: '/news',
        name: 'News',
        component: News,
      },
      {//捕获所有路由或 404 Not found 路由
        path: '*',
        name: 'Com404',
        component: Com404,
      }
    ];

    // 3. 创建 router 实例,然后传 `routes` 配置
    const router = new VueRouter({
      // mode: 'history',
      routes // (缩写)相当于 routes: routes
    });

    // 4. 创建和挂载根实例。
    const app = new Vue({
      el: "#box",
      data() {
        return {}
      },
      methods: {
        jump(path) {
          // 字符串
          // this.$router.push(path);

          // 对象
          // this.$router.push({ path: path })

          // 命名的路由
          this.$router.push({ name: path[0].toLocaleUpperCase() + path.slice(1) })
        },
        replaceJump(path) {
          // 字符串
          this.$router.replace(path);

          // 对象
          // this.$router.replace({ path: path })

          // 命名的路由
          // this.$router.push({ name: path[0].toLocaleUpperCase() + path.slice(1) })
        },
        goJump(speed) {
          this.$router.go(speed);
        }
      },
      router
    });
  </script>
</body>
</html>


这篇关于Vue-Router-编程式导航的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程