zoukankan      html  css  js  c++  java
  • vue-router教程二(要素篇之新手入门)

    注意,我们将在指南中使用es 2015代码样本。此外,所有示例都将使用VUE的完整版本来使在线模板编译成为可能。请参阅这里的更多细节。 

    用vue路由器创建单页应用程序是非常简单的。使用vue.js,我们已经在用组件组合我们的应用程序。当将vue路由器添加到混合时,我们所需要做的就是将组件映射到路由,让vue路由器知道在哪里呈现它们。下面是一个基本示例:

    HTML

    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    
    <div id="app">
      <h1>Hello App!</h1>
      <p>
        <!-- use router-link component for navigation. -->
        <!-- specify the link by passing the `to` prop. -->
        <!-- `<router-link>` will be rendered as an `<a>` tag by default -->
        <router-link to="/foo">Go to Foo</router-link>
        <router-link to="/bar">Go to Bar</router-link>
      </p>
      <!-- route outlet -->
      <!-- component matched by the route will render here -->
      <router-view></router-view>
    </div>
    

     

    JavaScript

    // 0. If using a module system (e.g. via vue-cli), import Vue and VueRouter
    // and then call `Vue.use(VueRouter)`.
    
    // 1. Define route components.
    // These can be imported from other files
    const Foo = { template: '<div>foo</div>' }
    const Bar = { template: '<div>bar</div>' }
    
    // 2. Define some routes
    // Each route should map to a component. The "component" can
    // either be an actual component constructor created via
    // `Vue.extend()`, or just a component options object.
    // We'll talk about nested routes later.
    const routes = [
      { path: '/foo', component: Foo },
      { path: '/bar', component: Bar }
    ]
    
    // 3. Create the router instance and pass the `routes` option
    // You can pass in additional options here, but let's
    // keep it simple for now.
    const router = new VueRouter({
      routes // short for `routes: routes`
    })
    
    // 4. Create and mount the root instance.
    // Make sure to inject the router with the router option to make the
    // whole app router-aware.
    const app = new Vue({
      router
    }).$mount('#app')
    
    // Now the app has started!
    

    通过注入路由器,在任何组件的内部,我们可以访问它。this.$router,以及当前路由。

    // Home.vue
    export default {
      computed: {
        username () {
          // We will see what `params` is shortly
          return this.$route.params.username
        }
      },
      methods: {
        goBack () {
          window.history.length > 1
            ? this.$router.go(-1)
            : this.$router.push('/')
        }
      }
    }
    

    在整个文档中,我们经常使用路由器实例。记住这一点。$router与使用路由器完全一样。我们使用它的原因是。$router是因为我们不想在每个需要操作路由的组件中导入路由器。您也可以查看这个示例livevee。注意,当一个<router-link> 的目标路由匹配时,它会自动获得.router-link-activeclass。在它的API参考中了解更多有关它的信息。

  • 相关阅读:
    客户端回调 Watcher ?
    Container 在微服务中的用途是什么?
    什么是持续集成(CI)?
    Zookeeper 的典型应用场景 ?
    Java 中 Semaphore 是什么?
    elasticsearch 了解多少,说说你们公司 es 的集群架构,索 引数据大小,分片有多少,以及一些调优手段 。
    Dubbo 用到哪些设计模式?
    简述 Memcached 内存管理机制原理?
    ACL 权限控制机制 ?
    一般使用什么注册中心?还有别的选择吗?
  • 原文地址:https://www.cnblogs.com/wntd/p/9154540.html
Copyright © 2011-2022 走看看