zoukankan      html  css  js  c++  java
  • VueRouter 第一个路由

    概述

    Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:

    • 嵌套的路由/视图表
    • 模块化的、基于组件的路由配置
    • 路由参数、查询、通配符
    • 基于 Vue.js 过渡系统的视图过渡效果
    • 细粒度的导航控制
    • 带有自动激活的 CSS class 的链接
    • HTML5 历史模式或 hash 模式,在 IE9 中自动降级
    • 自定义的滚动条行为

    安装

    vue-router 是一个插件包,所以我们还是需要用 npm/cnpm 来进行安装的。打开命令行工具,进入你的项目目录,输入下面命令。

    1 npm install vue-router --save-dev --registry=https://registry.npm.taobao.org
    2 # 输出如下
    3 npm WARN ajv-keywords@3.4.1 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.
    4 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modulesfsevents):
    5 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
    6 + vue-router@3.0.7
    7 added 1 package from 1 contributor and audited 11766 packages in 11.33s
    8 found 10 vulnerabilities (6 moderate, 4 high)
    9   run `npm audit fix` to fix them, or `npm audit` for details
    View Code

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

    1 import Vue from 'vue'
    2 import VueRouter from 'vue-router'
    3 Vue.use(VueRouter);
    View Code

    使用

    以下案例在 vue-cli 项目中使用 vue-router

    创建组件页面

    创建一个名为 src/components 的目录专门放置我们开发的 Vue 组件,在 src/components 目录下创建一个名为 Content.vue 的组件,代码如下:

     1 <template>
     2     <div>
     3       我是内容页
     4     </div>
     5 </template>
     6 <script>
     7     export default {
     8         name: "Content"
     9     }
    10 </script>
    11 <style>
    12   #app {
    13     font-family: 'Avenir', Helvetica, Arial, sans-serif;
    14     -webkit-font-smoothing: antialiased;
    15     -moz-osx-font-smoothing: grayscale;
    16     text-align: center;
    17     color: #2c3e50;
    18     margin-top: 60px;
    19   }
    20 </style>
    View Code

    安装路由

    创建一个名为 src/router 的目录专门放置我们的路由配置代码,在 src/router 目录下创建一个名为 index.js 路由配置文件,代码如下:

     1 import Vue from 'vue'
     2 // 导入路由插件
     3 import Router from 'vue-router'
     4 // 导入上面定义的组件
     5 import Content from '@/components/Content'
     6 // 安装路由
     7 Vue.use(Router);
     8 // 配置路由
     9 export default new Router({
    10   routes: [
    11     {
    12       // 路由路径
    13       path: '/content',
    14       // 路由名称
    15       name: 'Content',
    16       // 跳转到组件
    17       component: Content
    18     }
    19   ]
    20 });
    View Code

    配置路由

    修改 main.js 入口文件,增加配置路由的相关代码

     1 import Vue from 'vue'
     2 import App from './App'
     3 // 导入上面创建的路由配置目录
     4 import router from './router'
     5 Vue.config.productionTip = false;
     6 new Vue({
     7   el: '#app',
     8   // 配置路由
     9   router,
    10   components: { App },
    11   template: '<App/>'
    12 });
    View Code

    使用路由

    修改 App.vue 组件,代码如下:

    <template>
      <div id="app">
        <router-link to="/">首页</router-link>
        <router-link to="/content">内容</router-link>
        <router-view></router-view>
      </div>
    </template>
    <script>
    export default {
      name: 'App'
    }
    </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;
      }
    </style>
    View Code

    说明:

    • router-link: 默认会被渲染成一个 <a> 标签,to 属性为指定链接
    • router-view: 用于渲染路由匹配到的组件
    博客园:https://www.cnblogs.com/xianquan
    Copyright ©2020 l-coil
    【转载文章务必保留出处和署名,谢谢!】
查看全文
  • 相关阅读:
    [GDOI2018]滑稽子图
    单位根反演学习笔记
    ODOO/OPENERP的网页模块QWEB简述
    odoo中的QWeb模板引擎
    项目管理)沟通管理
    从vc6升级到vc7的一些问题及解决方法
    vc++ 2005 发布程序
    颜色取反
    几个VC6.0到VC9.0的错误解决方案
    测试计划测试用例
  • 原文地址:https://www.cnblogs.com/xianquan/p/12491437.html
  • Copyright © 2011-2022 走看看