• vue路由-基础


    安装

    1.直接下载 / CDN

    https://unpkg.com/vue-router/dist/vue-router.js

    在 Vue 后面加载 vue-router,它会自动安装的:

    <script src="/path/to/vue.js"></script>
    <script src="/path/to/vue-router.js"></script>

    2.NPM

    npm install vue-router

    如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能:

    在你的文件夹下的 src 文件夹下的 main.js 文件内写入以下代码

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    
    Vue.use(VueRouter)

    如果使用全局的 script 标签,则无须如此(手动安装)。

    构建开发版

    如果你想使用最新的开发版,就得从 GitHub 上直接 clone,然后自己 build 一个 vue-router

    git clone https://github.com/vuejs/vue-router.git node_modules/vue-router
    cd node_modules/vue-router
    npm install
    npm run build

    开始

    举例1

    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>
        <!-- 使用 router-link 组件来导航. -->
        <!-- 通过传入 `to` 属性指定链接. -->
        <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
        <router-link to="/foo">Go to Foo</router-link>
        <router-link to="/bar">Go to Bar</router-link>
      </p>
      <!-- 路由出口 -->
      <!-- 路由匹配到的组件将渲染在这里 -->
      <router-view></router-view>
    </div>

    JavaScript

    // 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')
    
    // 现在,应用已经启动了!

    举例2:

    HTML

    <div id="box"> 
    
    </div>
    <!--定义模版-->
    <template id="a">
        <div>
            第一个router
        </div>
    </template>
    <template id="b">
        <div>
            第二个router
        </div>
    </template>

    JavaScript

    var routes = [
        {
            path:"/one",
    
            component:{template:"#a"}
        },
        {
            path:"/two",
            component:{template:"#b"}
        },
    ];
    // 定义路由组件
    var router = new VueRouter({
        routes
    });
    // 定义路由
    new Vue({
        el:"#box",
        router
    });
    // 创建和挂载实例

    将模版增添链接

    <div id="box"> 
        <router-link to="/one">One</router-link>
        <router-link to="/two">Two</router-link>
        <router-view></router-view>
    </div>

    < router-link > 默认会被渲染成一个 <a> 标签 >>>to=""为我们定义的路由

    < router-view > 路由匹配到的组件将渲染在这里

  • 相关阅读:
    Azure Messaging-ServiceBus Messaging消息队列技术系列6-消息回执
    Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务
    Azure Messaging
    消息队列技术之基本概念
    Azure IoT 技术研究系列1
    Azure IoT 技术研究系列3
    Azure IoT 技术研究系列2
    Azure IoT 技术研究系列4
    memset()
    C++ GetComputerName()
  • 原文地址:https://www.cnblogs.com/ctztake/p/9023899.html
Copyright © 2020-2023  润新知