zoukankan      html  css  js  c++  java
  • Vue-cli3如何添加路由(router)

    之前使用的vue-cli2的是否可以在创建项目时直接引入vue-router,但是现在Vue-cli3新建项目后却少了很多东西,我们需要自己来安装使用路由。具体方案如下:

    安装路由

    npm install vue-router

    创建router.js与main.js同级

    router.js中的内容为

    复制代码
    import Vue from 'vue'
    import Router from 'vue-router'
     
    //组件模块
    import Main from './components/content/main'
    import Header from './components/header/header'
    import Admin from './components/admin/admin'
     
    Vue.use(Router)
     
    export default new Router({
      routes: [
        { path: '/', name: 'home', component: Main },
        { path: '/main', name: 'Main', component: Main },
        { path: '/header',  name: 'Header', component: Header},
        { path: '/admin',  name: 'Admin', component: Admin}
      ]
    })
    复制代码

    在main.js中添加如下内容

    接下里就可以开始使用路由了,在需要使用路由的地方加入以下内容

    <router-link to="/">主页</router-link>     //切换到指定的组件
    <router-link to="/main">主页</router-link>
    <router-link to="/admin">管理页</router-link>
            
    <router-view/>  //组件显示的地方

    也可通过事件的方法来使用路由

    复制代码
    methods:{
      toMain() {
        this.$router.push('./main');  //跳转到指定组件
      },
     
      //使用路由返回上一级
      goBack() {
        window.history.length > 1 ? this.$router.go(-1) : this.$router.push("/");
      },
    }
    复制代码
  • 相关阅读:
    Intellij IDEA使用姿势
    款阿里开源的 Java 诊断工具Arthas
    Spring Boot Runner启动器
    Spring Boot 2.x 启动全过程源码分析
    Spring Boot自动配置原理
    vue包部署在tomcat上,解决资源路径问题
    输入回车 回显换行
    session和cookie
    WebStorage——SessionStorage、LocalStorage与cookie
    HTML5 cache
  • 原文地址:https://www.cnblogs.com/nuoruo/p/14328578.html
Copyright © 2011-2022 走看看