欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue学习之路由vue-router传参及嵌套小结(十)

程序员文章站 2023-01-01 13:03:47
一、路由传递参数: 1、使用query传值: 2、使用params 传参: 地址上需要设置下第二个参数ID 二、嵌套路由children: ......

一、路由传递参数:

1、使用query传值:

<!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>路由</title>
    <script src="./lib/vue.js"></script>
    <!-- 1.安装vue-router路由模块 -->
    <script src="./lib/vue-router.js"></script>
  </head>
  <body>
    <div id="app">
      <!-- 如果在路由中,使用 查询字符串,给路由传递参数,则不需要修改路由规则的path属性 -->
      <router-link to="/login?id=10&name=zs">登录</router-link>
      <router-link to="/register">注册</router-link>
      <router-view></router-view>
    </div>
    <script>
      var login = {
        template:
          "<h1>登录--{{ $route.query.id }}--{{ $route.query.name }}</h1>",
        data() {
          return {
            msg: "123"
          };
        },
        creadtd() {
          //组件的生命周期钩子函数
          // console.log(this.$route);
          console.log(this.$route.query.id);
        }
      };
      var register = {
        template: "<h1>注册组件</h1>"
      };
      var routerobj = new vuerouter({
        routes: [
          { path: "/login", component: login },
          { path: "/register", component: register }
        ]
      });
      var vm = new vue({
        el: "#app",
        data: {},
        methods: {},
        router: routerobj
      });
    </script>
  </body>
</html>

2、使用params 传参:

地址上需要设置下第二个参数id

<!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>路由</title>
    <script src="./lib/vue.js"></script>
    <!-- 1.安装vue-router路由模块 -->
    <script src="./lib/vue-router.js"></script>
  </head>
  <body>
    <div id="app">
      <!-- 如果在路由中,使用 查询字符串,给路由传递参数,则不需要修改路由规则的path属性 -->
      <router-link to="/login/12">登录</router-link>
      <router-link to="/register">注册</router-link>
      <router-view></router-view>
    </div>
    <script>
      var login = {
        template: "<h1>登录--{{ $route.params.id }}</h1>",
        data() {
          return {
            msg: "123"
          };
        },
        creadtd() {
          //组件的生命周期钩子函数
          console.log(this.$route.params.id);
        }
      };
      var register = {
        template: "<h1>注册组件</h1>"
      };
      var routerobj = new vuerouter({
        routes: [
          // 除了上面的字母的不同外,这里还需要设置下添加的第二个参数id
          { path: "/login/:id", component: login },
          { path: "/register", component: register }
        ]
      });
      var vm = new vue({
        el: "#app",
        data: {},
        methods: {},
        router: routerobj
      });
    </script>
  </body>
</html>

二、嵌套路由children:

<!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>路由</title>
    <script src="./lib/vue.js"></script>
    <!-- 1.安装vue-router路由模块 -->
    <script src="./lib/vue-router.js"></script>
  </head>
  <body>
    <div id="app">
      <router-link to="/account">account</router-link>
      <router-view></router-view>
    </div>
    <template id="tmp1">
      <div>
        <h1>这是account组件</h1>
        <router-link to="/account/login">登录</router-link>
        <router-link to="/account/register">注册</router-link>
        <router-view></router-view>
      </div>
    </template>
    <script>
      var account = {
        template: "#tmp1"
      };
      var login = {
        template: "<h3>登录组件</h3>"
      };
      var register = {
        template: "<h3>注册组件</h3>"
      };
      var router = new vuerouter({
        routes: [
          {
            path: "/account",
            component: account,
            // 使用children 属性,实现子路由,同时子路由的path前面,不要带/,
            // 否则永远以根路径开始请求,这样不方便我们用户去理解url地址
            children: [
              {
                path: "login",
                component: login
              },
              {
                path: "register",
                component: register
              }
            ]
          }
        ]
      });
      var vm = new vue({
        el: "#app",
        data: {},
        methods: {},
        router
      });
    </script>
  </body>
</html>