zoukankan      html  css  js  c++  java
  • vue2.0路由写法、传参和嵌套

    前置知识请戳这里

    vue-routerCDN地址:https://unpkg.com/vue-router@3.0.1/dist/vue-router.js

    vue-router下载地址:https://github.com/vuejs/vue-router/tree/dev/dist

    vue2.0路由基本写法

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>
        <script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
      </head>
      <body>
        <div id="app">
          <router-link to="/login">登录</router-link>
          <router-link to="/register">注册</router-link>
          <!--组件的显示占位域-->
          <router-view></router-view>
        </div>
    
        <script>
          //1.0 准备组件
          var App = Vue.extend({});
    
          var login = Vue.extend({
            template: '<div><h3>登录</h3></div>'
          });
    
          var register = Vue.extend({
            template: '<div><h3>注册</h3></div>'
          })
    
          //2.0 实例化路由规则对象
          var router = new VueRouter({
            routes: [{
                path: '/login',
                component: login
              },
              {
                path: '/register',
                component: register
              },
              {
                  path:'/',
                  //当路径为/时,重定向到/login
                  redirect:'/login'
              }
            ]
          });
    
          //3.0 开启路由对象
          new Vue({
            el: '#app',
            router: router
          })
        </script>
      </body>
    </html>

    vue2.0路由传参

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>
        <script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
      </head>
      <body>
        <div id="app">
          <router-link to="/login">登录</router-link>
          <router-link to="/register/value">注册</router-link>
          <!--组件的显示占位域-->
          <router-view></router-view>
        </div>
    
        <script>
          //1.0 准备组件
          var App = Vue.extend({});
    
          var login = Vue.extend({
            template: '<div><h3>登录</h3></div>'
          });
    
          var register = Vue.extend({
            template: '<div><h3>注册{{name}}</h3></div>',
            data:function(){
                return {
                    name:''
                }
            },
            created:function(){
                this.name = this.$route.params.name;
            }
          });
    
          //2.0 实例化路由规则对象
          var router = new VueRouter({
            routes: [{
                path: '/login',
                component: login
              },
              {
                path: '/register/:name',
                component: register
              },
              {
                  path:'/',
                  //当路径为/时,重定向到/login
                  redirect:'/login'
              }
            ]
          });
    
          //3.0 开启路由对象
          new Vue({
            el: '#app',
            router: router
          })
        </script>
      </body>
    </html>

    vue2.0路由嵌套

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://cdn.jsdelivr.net/npm/vue"></script>
        <script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
      </head>
      <body>
        <div id="app">
          <router-link to="/account/login">登录</router-link>
          <router-link to="/account/register">注册</router-link>
          <!--组件的显示占位域-->
          <router-view></router-view>
        </div>
    
        <script>
          //1.0 准备组件
          var App = Vue.extend({});
    
          var account = Vue.extend({
            template: '<div><h1>账号组件</h1><router-view></router-view></div>'
          });
    
          var login = Vue.extend({
            template: '<div><h3>登录</h3></div>'
          });
    
          var register = Vue.extend({
            template: '<div><h3>注册</h3></div>'
          });
    
          //2.0 实例化路由规则对象
          var router = new VueRouter({
            routes: [{
              path: '/account',
              component: account,
              children: [{
                  path: 'login',
                  component: login
                },
                {
                  path: 'register',
                  component: register
                }
              ]
            }]
          });
    
          //3.0 开启路由对象
          new Vue({
            el: '#app',
            router: router
          })
        </script>
      </body>
    </html>
  • 相关阅读:
    Centos或Windows中部署Zookeeper集群及其简单用法
    Linux中使用sendmail发送邮件,指定任意邮件发送人
    使用log4net将C#日志发送到Elasticsearch
    在Centos6或者7上安装Kafka最新版
    最简单的配置Centos中JAVA的环境变量的方法
    JAVA通过oshi获取系统和硬件信息
    JAVA代码中获取JVM信息
    使用JavaCV播放视频、摄像头、人脸识别
    JAVA中通过JavaCV实现跨平台视频/图像处理-调用摄像头
    Linux中使用Vim快速更换文档中Windows换行符为Linux平台
  • 原文地址:https://www.cnblogs.com/fengxiongZz/p/8074177.html
Copyright © 2011-2022 走看看