zoukankan      html  css  js  c++  java
  • vue 之 vue-router

    官方文档

    // 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
    
    // 1. 定义(路由)组件。
    // 可以从其他文件 import 进来
    const Foo = { template: '<div>foo</div>' }
    const Bar = { template: '<div>bar</div>' }
    
    // 2. 定义路由
    // 每个路由应该映射一个组件。 其中"component" 可以是
    // 通过 Vue.extend() 创建的组件构造器,
    // 或者,只是一个组件配置对象。
    // 我们晚点再讨论嵌套路由。
    const routes = [
      { path: '/foo', component: Foo },
      { path: '/bar', component: Bar }
    ]
    
    // 3. 创建 router 实例,然后传 `routes` 配置
    // 你还可以传别的配置参数, 不过先这么简单着吧。
    const router = new VueRouter({
      routes // (缩写)相当于 routes: routes
    })
    
    // 4. 创建和挂载根实例。
    // 记得要通过 router 配置参数注入路由,
    // 从而让整个应用都有路由功能
    const app = new Vue({
      router
    }).$mount('#app')
    
    // 现在,应用已经启动了!
    开始

    通过注入路由器,我们可以在任何组件内通过 this.$router 访问路由器,也可以通过 this.$route 访问当前路由:

    // Home.vue
    export default {
      computed: {
        username () {
          // 我们很快就会看到 `params` 是什么
          return this.$route.params.username
        }
      },
      methods: {
        goBack () {
          window.history.length > 1
            ? this.$router.go(-1)
            : this.$router.push('/')
        }
      }
    }
    Home.vue
    1:先下载路由
    npm install vue-router --save
    2:导入
    import VueRouter from "vue-router"
    //  定义(路由)组件。
    
    3:
     如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
    Vue.use(VueRouter)
    1定制路由(组件)
    // 导入路由组件
    import Index from './Index'
    import Luffy from './Luffy'
    
    
     // 2、创建 router 实例,然后传 `routes` 配置
    
     const router = new VueRouter({
         mode: 'history',
         routes:[
              { path: '/', component: Index },
               { path: '/luffy', component: Luffy }
         ]
     })
    
    3
    new Vue({
      el: '#app',
      router,   //  挂载router实例
      render: h => h(App)
    })
    
    4,在主主件写出口
        <!-- 路由出口 所有路由匹配的组件都会被渲染到这里 -->
        <router-view></router-view> 
    
    5:a标签要换成router-link
           <router-link  v-for='(item,index) in urls'  :to="item.href"  :class='{active:currentIndex==index}' @click.native='clickHandler(index)' >{{item.name}}</router-link>
    
            <!--  给router-link添加事件 会阻止click事件的触发,需要加上.navtive就可以了。加上.navtive 才是真正点击了a标签的click事件,在组件中不加.native 不会触发原生的事件。注意了注意了 -->
            <!-- <router-link to="/luffy">路飞学城</router-link> -->
          

    主主件

    <template>
      <div id="app">
        <img src="./assets/logo.png">
        <h1>{{ msg }}</h1>
        <h2>Essential Links</h2>
        <ul>
    
            <router-link  v-for='(item,index) in urls'  :to="item.href"  :class='{active:currentIndex==index}' @click.native='clickHandler(index)' >{{item.name}}</router-link>
    
            <!--  给router-link添加事件 会阻止click事件的触发,需要加上.navtive就可以了。加上.navtive 才是真正点击了a标签的click事件,在组件中不加.native 不会触发原生的事件。注意了注意了 -->
            <!-- <router-link to="/luffy">路飞学城</router-link> -->
          
        </ul>
    
        <!-- 路由出口 所有路由匹配的组件都会被渲染到这里 -->
        <router-view></router-view> 
       
      </div>
    </template>
    
    <script>
    export default {
      name: 'app',
      data () {
        return {
          msg: 'Welcome to Your Vue.js App',
          urls:[
            {href:'/',name:"首页"},
            {href:'/luffy',name:"路飞学城"}
    
          ],
          currentIndex:0
        }
      },
      methods:{
        clickHandler(index){
          console.log(index)
          this.currentIndex = index;
    
        }
      }
    }
    </script>
    
    <style>
    #app {
      font-family: 'Avenir', Helvetica, Arial, sans-serif;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      text-align: center;
      color: #2c3e50;
      margin-top: 60px;
    }
    
    h1, h2 {
      font-weight: normal;
    }
    
    ul {
      list-style-type: none;
      padding: 0;
    }
    
    li {
      display: inline-block;
      margin: 0 10px;
    }
    
    a {
      color: #42b983;
    }
    a.active{
      color: yellow;
    }
    </style>
    app。vue

    子主件

    <template>
        <div class="luffy">
            <h4>我是路飞</h4>
        </div>
    </template>
    <script>
        export default{
            name:'luffy',
            data(){
                return {
    
                }
            }
        }
    </script>
    <style>
        
    </style>
    luffy.vue
    <template>
        <div class="index">
            <h3>我是首页</h3>
        </div>
    </template>
    <script>
        export default{
            name:'index',
            data(){
                return {
    
                }
            }
        }
    </script>
    <style>
        
    </style>    
    index。vue
  • 相关阅读:
    黄聪:C#多线程教程(1):BeginInvoke和EndInvoke方法,解决主线程延时Thread.sleep柱塞问题(转)
    黄聪:C#类似Jquery的html解析类HtmlAgilityPack基础类介绍及运用
    黄聪:国内com域名转移到Godaddy详细教程(转)
    黄聪:Navicat for MySQL的1577错误解决
    黄聪:VPS配置Filezilla Server支持FTP的Passive被动模式(FTP连接不上怎么办?有详细教程)
    黄聪:Microsoft office 2013版下载、安装及破解工具下载破解教程(Windows Toolkit)
    黄聪:mysql搬家,直接复制data文件夹(*.MYD,*.MYI,innodb)出错,无法正常显示
    黄聪:WordPress默认编辑器可视化切换不见了,非插件导致消失问题
    黄聪:自定义WordPress顶部管理工具条的技巧(转)
    黄聪:VS2010中“新建项目”却没有“解决方案”节点,如何调出来
  • 原文地址:https://www.cnblogs.com/jassin-du/p/8734169.html
Copyright © 2011-2022 走看看