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

2019-08-14 10:02:37来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

一、路由传递参数:

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>

 


原文链接:https://www.cnblogs.com/21-forever/p/11108925.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Babel是什么?

下一篇:剑指前端(前端入门笔记系列)——DOM(元素节点)