vue-router基本使用步骤

<!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>
    <style>
      .router-link-exact-active,
      .router-link-active {
        font-size: 30px;
        color: green;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h1>{{ msg }}</h1>
      <ul>
        <li><router-link to="/index">首页</router-link></li>
        <li><router-link to="/user">用户管理</router-link></li>
        <li><router-link to="/login">登录页</router-link></li>
      </ul>

      <hr />
      <router-view></router-view>
    </div>

    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
    <script>
      // 组件
      const Index = {
        template: `
          <div>我是首页</div>
        `
      }
      const User = {
        template: `
          <div>
            我是用户管理
            <router-link to="/user/add">添加</router-link>
            <router-link to="/user/delete">删除</router-link>
            <hr>
            <router-view />
          </div>
        `
      }
      const UserAdd = {
        template: `
          <div>我是用户添加管理</div>
        `
      }
      const UserDelete = {
        template: `
          <div>我是用户删除管理</div>
        `
      }
      const Login = {
        template: `
          <div>我是登录页</div>
        `
      }

      const router = new VueRouter({
        routes: [
          // 路由规则
          { path: '/', redirect: '/index' },
          { path: '/index', component: Index },
          {
            path: '/user',
            component: User,
            children: [
              { path: 'add', component: UserAdd },
              { path: 'delete', component: UserDelete }
            ]
          },
          { path: '/login', component: Login }
        ]
      })

      const vm = new Vue({
        el: '#app',
        data: {
          msg: 'hello vue'
        },
        router
      })
    </script>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值